Skip to content

channdara/cpp-openssl-for-android

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

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.

Quick Use

  • Clone or download this repository and use the pre-build files inside android-build follow How To Use section. If you prefer latest build, please proceed to the following guide. Thank you.

Build CPP OpenSSL for Android

git clone https://github.com/openssl/openssl.git
  • Navigate to openssl folder that you already cloned and create android-build.sh shell script file, put below code in and save. -D__ANDROID_API__=21 is minSdkVersion currently 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-build inside current openssl directory we're working on.

How To Use

  • Copy all the content inside android-build folder and place inside your project's cpp folder. I recommend to put under cpp → libs → openssl folder so it's easy to copy below CMakeList without 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.txt and make sure it above target_link_libraries. If your main lib is not native-lib.cpp you 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 ssl and crypto to target_link_libraries. Make sure that ssl is before crypto and those 2 are at the end of target_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.txt to 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)

NOTE

  • The library size is over 50MB and almost 600 items during I make this document. I would suggest you to at the main openssl folder to .gitignore so 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 the build_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 =========="

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages