ubuntu 20.04 ndk 编译64位 openssl

概述

之前本人在Ubuntu14.04上用ndk16b编译了openssl1.1.1g,32位的库,文章链接https://blog.csdn.net/shenweihong/article/details/107985593
现在因为android13,必须要用64位的库,本文主要描述64位openssl静态库的编译

软件环境

ubuntu:版本20.04
openssl:版本1.1.1k
ndk版本:20b

编译准备

下载ndk,https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip?hl=zh-cn,解压ndk:android-ndk-r20b-linux-x86_64.zip
解压到:/home/shenweihong/android-ndk-r20b
下载openssl源码,http://distfiles.macports.org/openssl/openssl-1.1.1g.tar.gz,解压源码openssl-1.1.1k.tar.gz,
并进入源码路径:/work1/OpensslBuild/openssl-1.1.1k

配置命令

export ANDROID_NDK_HOME=/home/shenweihong/android-ndk-r20b
export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuillinux-x86_64/bin:$PATH
./Configure android-arm64 -D__ANDROID_API__=24 no-threads no-shared --prefix=/work1/OpensslBuild/opensslLib

关键参数:
android-arm64,表示按照arm64位编译
-D__ANDROID_API__=24,编译目标平台,后面的jni编译要和这里一致
no-shared,静态库方式

编译命令

make
make install

JNI使用

这是Android.mk文件

LOCAL_PATH := $(call my-dir)

#APP_ABI := armeabi armeabi-v7a
#APP_PLATFORM := android-24

include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto
LOCAL_LDLIBS := -lm -lz -llog -fPIE -ldl
LOCAL_CFLAGS += -fPIE -D__ANDROID_API__=24
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libssl
LOCAL_LDLIBS := -lm -lz -llog -fPIE -ldl
LOCAL_CFLAGS += -fPIE -D__ANDROID_API__=24
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/libssl.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/lib
LOCAL_LDLIBS := -lm -lz -llog -fPIE -ldl -fPIC
LOCAL_CFLAGS += -fPIE -D__ANDROID_API__=24 -fPIC
LOCAL_STATIC_LIBRARIES := libssl libcrypto
LOCAL_MODULE := OpensslClient
LOCAL_SRC_FILES := OpensslClient.cpp SSLClient.cpp

include $(BUILD_SHARED_LIBRARY)

这是在build.gradle配置:


android {
    namespace 'com.example.sslfota'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.sslfota"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters 'arm64-v8a'
            ldLibs "log"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        ndkBuild {
            path file("src/main/jni/Android.mk")
        }
    }
}

注意关键的参数arm64-v8a:
ndk {
abiFilters ‘arm64-v8a’
ldLibs “log”
}
结果在arm64-v8目录下生成文件libOpensslClient.so,即表示成功

你可能感兴趣的:(android-studio,ssl)