First of all, if this repository or content violate any law or rules from the original owner, please let me know
in the GitHub Issue section. I will take down this repository as soon as possible.
- Clone or download this repository and use the pre-build files inside
android-buildfollow How To Use section. If you prefer latest build, please proceed to the following guide. Thank you.
- Clone OpenSSL repository from GitHub: https://github.com/openssl/openssl.git
git clone https://github.com/openssl/openssl.git- Navigate to
opensslfolder that you already cloned and createandroid-build.shshell script file, put below code in and save.-D__ANDROID_API__=21isminSdkVersioncurrently set to 21. You can change base on the project you build for:
build_one() {
local arch=$1
local abi=$2
make clean
./Configure "$arch" -D__ANDROID_API__=21 no-shared no-asm -fPIC --prefix="$(pwd)/android-build/$abi"
make -j$(sysctl -n hw.ncpu)
make install_sw
mv "$(pwd)/android-build/$abi/lib/libssl.a" "$(pwd)/android-build/$abi/"
mv "$(pwd)/android-build/$abi/lib/libcrypto.a" "$(pwd)/android-build/$abi/"
rm -rf "$(pwd)/android-build/$abi/bin"
rm -rf "$(pwd)/android-build/$abi/lib"
}
build_one "android-arm64" "arm64-v8a"
build_one "android-arm" "armeabi-v7a"
build_one "android-x86_64" "x86_64"
build_one "android-x86" "x86"
echo "========== ALL DONE =========="- Run below command to grand execute permission to the script file you've just created:
chmod +x build_openssl.sh- Now run the script and wait until it finish. This will take a while depends on your machine performance:
./android-build.sh- When all build complete, you can see folder named
android-buildinside currentopenssldirectory we're working on.
- Copy all the content inside
android-buildfolder and place inside your project'scppfolder. I recommend to put undercpp → libs → opensslfolder so it's easy to copy belowCMakeListwithout making any change. Directory look like this:
cpp/
└── libs/
└── openssl/
├── arm64-v8a/
│ ├── include/
│ │ └── openssl/
│ ├── libcrypto.a
│ └── libssl.a
├── armeabi-v7a/
│ ├── include/
│ │ └── openssl/
│ ├── libcrypto.a
│ └── libssl.a
├── x86/
│ ├── include/
│ │ └── openssl/
│ ├── libcrypto.a
│ └── libssl.a
└── x86_64/
├── include/
│ └── openssl/
├── libcrypto.a
└── libssl.a
├── CMakeLists.txt
└── native-lib.cpp
- Add these below line to your
CMakeLists.txtand make sure it abovetarget_link_libraries. If your main lib is notnative-lib.cppyou can update it according to your project naming:
# OpenSSL Configuration ========================================================
# Hide symbols to prevent conflicts in the final SDK
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
# Path to headers
target_include_directories(native-lib PRIVATE ${CMAKE_SOURCE_DIR}/libs/openssl/${ANDROID_ABI}/include)
# Import libssl
add_library(ssl STATIC IMPORTED)
set_target_properties(ssl PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/openssl/${ANDROID_ABI}/libssl.a)
# Import libcrypto
add_library(crypto STATIC IMPORTED)
set_target_properties(crypto PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/openssl/${ANDROID_ABI}/libcrypto.a)
# ==============================================================================
- Finally, add
sslandcryptototarget_link_libraries. Make sure thatsslisbeforecrypto and those 2 are at the end oftarget_link_libraries. Here is the example of what it looks like:
target_link_libraries(native-lib PRIVATE ${log-lib} android ssl crypto)
- I'll drop a snipped code to my
CMakeLists.txtto show you what is look like:
cmake_minimum_required(VERSION 3.18.1)
project(CPP-Android-OpenSSL)
add_library(native-lib SHARED native-lib.cpp)
find_library(log-lib log)
# OpenSSL Configuration ========================================================
# Hide symbols to prevent conflicts in the final SDK
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
# Path to headers
target_include_directories(native-lib PRIVATE ${CMAKE_SOURCE_DIR}/libs/openssl/${ANDROID_ABI}/include)
# Import libssl
add_library(ssl STATIC IMPORTED)
set_target_properties(ssl PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/openssl/${ANDROID_ABI}/libssl.a)
# Import libcrypto
add_library(crypto STATIC IMPORTED)
set_target_properties(crypto PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/openssl/${ANDROID_ABI}/libcrypto.a)
# ==============================================================================
target_link_libraries(native-lib PRIVATE ${log-lib} android ssl crypto)
- The library size is over 50MB and almost 600 items during I make this document. I would suggest you to at the
main
opensslfolder to.gitignoreso you don't push all this files to your repository. - Some files are move and remove during each
build_one()run. If you wish to keep original build folder, you can remove the last 4 lines from thebuild_one()function in result of below script:
build_one() {
local arch=$1
local abi=$2
make clean
./Configure "$arch" -D__ANDROID_API__=21 no-shared no-asm -fPIC --prefix="$(pwd)/android-build/$abi"
make -j$(sysctl -n hw.ncpu)
make install_sw
}
build_one "android-arm64" "arm64-v8a"
build_one "android-arm" "armeabi-v7a"
build_one "android-x86_64" "x86_64"
build_one "android-x86" "x86"
echo "========== ALL DONE =========="