0%

为安卓编译使用OpenSSL的注意事项

NDK

简单来讲就是一句话:不要使用高版本NDK!不要使用高版本NDK!不要使用高版本NDK!
高版本NDK废弃了gcc转而使用clang,这会导致工具链部分文件缺失。手动补上容易出各种各样奇怪的错误。无论你在Android Studio中使用的是什么版本的NDK,这里都没有影响,能正常帮你编译的NDK就是好NDK。我使用的14c,编译最新版本OpenSSL3.0,工作良好。

网络权限

如果要再加一句话就是需要为安卓应用加上网络权限。安卓10可能需要两句话。

配置文件

部分文件参考。
manifest注意permission与usesCleartextTraffic。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xx.xx.xx.xx.myapplication">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

下面的F:/Brick/Project1-ciphersuites/android下存放着编译出来的头文件与库文件等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
native-lib

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
log-lib

# Specifies the name of the NDK library that
# you want CMake to locate.
log )

set (SSL_PATH F:/Brick/Project1-ciphersuites/android)
include_directories(${SSL_PATH}/include)
set (open-ssl-libs ${SSL_PATH}/lib/libssl.a ${SSL_PATH}/lib/libcrypto.a)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
native-lib
${open-ssl-libs}
# Links the target library to the log library
# included in the NDK.
${log-lib} )

参考资料

遇到编译相关的问题可以参考这些链接,总有一款适合你,我推荐第一个。另外可以一提的是,OpenSSL3.0不需要额外修改配置文件就可以运行Configure android-arm64命令了。

openssl 1.1.1b for Android 的编译步骤
How to cross compile OpenSSL for 64 bit Android on OSX Darwin using NDK 19
Android平台openssl编译打包流程