环境
- sys-os : Vmware - ubuntu-20.04.1-desktop-amd64
- ndk :
- openssl : openssl-1.1.1h
- sdk : android-sdk_r24.4.1-linux.tgz
- download :
- Vmware - https://www.vmware.com
- Ubuntu - http://releases.ubuntu.com
- ndk - https://dl.google.com
- openssl : https://www.openssl.org
- sdk : https://dl.google.com
安装
虚拟机和Ubuntu安装这里就不细说了。(Ubuntu更换软件源参考:点这里)
下载并解压sdk使用
wget
命令和tar
命令1
2
3
4
5
6
7
8
9
10在指定目录下执行 指定目录(/home/yooking/Public/android)
wget 下载命令 也可以使用 -P 命令安装到指定目录(/home/yooking/Public/android)
wget -P /home/yooking/Public/android https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz
下载完毕后解压(进入目标文件夹下执行)
-z或--gzip或--ungzip 通过gzip指令处理备份文件。
-x或--extract或--get 从备份文件中还原文件。
-v或--verbose 显示指令执行过程。
-f<备份文件>或--file=<备份文件> 指定备份文件。(也可以在-zxvf后面输入目标地址)
tar -zxvf android-sdk_r24.4.1-linux.tgz下载并解压ndk
1
2
3
4
5
6
7
8新建文件夹 /home/yooking/Public/android/android-sdk/ndk
在ndk文件夹中使用wget命令
wget https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip
和
wget https://dl.google.com/android/repository/android-ndk-r21d-linux-x86_64.zip
使用unzip解压这两个压缩包
unzip android-ndk-r15c-linux-x86_64.zip
unzip android-ndk-r21d-linux-x86_64.zip下载并解压openssl
1
2
3
4新建文件夹 /home/yooking/Downloads/openssl
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1h.tar.gz
使用tar命令解压
tar -zxvf openssl-1.1.1h.tar.gz
创建sh脚本文件并编译
r21和r15有不同
从NDK r19开始,由于gcc兼容clang的编译方式有问题,该版本已经移除了相关gcc文件,所以用老方法交叉编译Openssl的时候,会提示找不到gcc文件。
r15下的sh脚本
参考:https://blog.csdn.net/zoujin6649
1 | 在 /home/yooking/Downloads/openssl/openssl-1.1.1h 目录下执行 |
build-android-single.sh
脚本文件及注释如下
1 | 设定openssl路径 |
执行脚本
1 | 第一次执行脚本时需要执行config文件,生成Makefile文件 |
修改脚本为循环编译,编译所有需要的文件
1 | 生成循环编译脚本 |
build-android-all.sh
脚本文件内容如下
1 | !/bin/bash |
执行脚本方法同上,如为第一次执行(即没有Makefile文件),仍需要执行./config
循环编译无需执行make clean
r21下的sh脚本
参考:https://blog.csdn.net/iamadk
1 | 创建用于生成toolchain路径的Python脚本 |
Python
脚本内容
1 | #!/usr/bin/env python |
创建编译执行脚本
1 | !/bin/bash |
执行编译脚本同r15,第一次需要执行./config
生成Makefile,第二次或以上需要清除make记录即make clean
,接下来依旧是创建循环编译脚本
1 | !/bin/bash |
引用
配置
- Android Studio - gradle:3.5.2
- ndk - android-ndk-r21d
使用
将自动生成的cpp下的CMakeLists.txt移动到app下(个人习惯)
同时也要修改app/build.gradle
1
2
3
4
5
6externalNativeBuild {
cmake {
path "CMakeLists.txt"
version "3.10.2"
}
}将
sh脚本文件
执行后生成的libs文件夹复制到app/src/main/cpp
下将
sh脚本文件
执行后生成的include下的openssl文件夹复制到app/src/main/cpp
下配置CMakeLists
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82# 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.
# 表示把src/main/cpp加入到include目录,这样在代码中使用:#include <...>就能访问到头文件
# openssl 中 .h 文件的引用方式是 include <openssl/symhacks.h> 而事实上这些是在同一个文件夹下,并不需要openssl/,为使能正常使用 所以添加 include_directories
include_directories(src/main/cpp)
# 这是原有的 无需修改
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).
src/main/cpp/native-lib.cpp)
# 添加两个预编译库 - 1
add_library(# Sets the name of the library.
openssl-crypto
# Sets the library as a static library.
STATIC
IMPORTED)
set_target_properties(
# Specifies the target library.
openssl-crypto
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
# 完整路径
${CMAKE_SOURCE_DIR}/src/main/cpp/libs/${ANDROID_ABI}/libcrypto.a)
# 添加两个预编译库 - 2
add_library(# Sets the name of the library.
openssl-ssl
# Sets the library as a static library.
STATIC
IMPORTED)
set_target_properties(
# Specifies the target library.
openssl-ssl
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
# 完整路径
${CMAKE_SOURCE_DIR}/src/main/cpp/libs/${ANDROID_ABI}/libssl.a)
# 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)
# 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
# Links the target library to the log library
# included in the NDK.
${log-lib} openssl-ssl openssl-crypto)