[toc]
NDK独立链接库
解压后,在加压目录1
- shell脚本
xx/android-ndk-r13b/build/tools/make_standalone_toolchain.sh –platform=android-21 –toolchain=aarch64-linux-android-4.9 –arch=arm64 –stl=stlport –install-dir=/Users/30san/Downloads/ndk_toolc/Users/30san/Downloads/ndk_toolchain_r13_arm641
-py 脚本
/Users/30san/Downloads/android-ndk-r13b/build/tools/make_standalone_toolchain.py –arch arm64 –api 21 –stl=stlport –install-dir=/Users/30san/Downloads/ndk_toolchain_a641
2
3
4
5
6
7
8
9
10
参数说明:
* --arch 指的是要编译的版本 cpu指令版本
* api 指的是android 对应的api版本 api21是支持64位的最低版本
* install-dir 指的是生成独立链接库的路径(不需要创建文件夹,脚本会自己生成)
## make 环境变量
https://www.gnu.org/software/make/manual/html
_node/Implicit-Variables.html
常用参数说明
CFLAGS:表示用于 C 编译器的选项
如指定头文件(.h文件)的路径,如:CFLAGS=-I/usr/include -I/path/include。同样地,安装一个包时会在安装路径下建立一个include目录,当安装过程中出现问题时,试着把以前安装的包的include目录加入到该变量中来
CXXFLAGS:表示用于 C++ 编译器的选项
LDFLAGS:gcc 等编译器会用到的一些链接参数,也可以在里面指定库文件的位置。用法:LDFLAGS=-L/usr/lib -L/path/to/your/lib。每安装一个包都几乎一定的会在安装目录里建立一个lib目录。如果明明安装了某个包,而安装另一个包时,它却是说找不到,可以将那个包的lib路径加入的LDFALGS中试一下。
LIBS:告诉链接器要链接哪些库文件,如LIBS = -lpthread -liconv -llibz -llog
LDFLAGS是告诉链接器从哪里寻找库文件,而
LIBS是告诉链接器要链接哪些库文件
1 |
|
params.make 文件
SO_VER=1.0.1
SO_NAME_VER=1.0
CPPFLAGS=
LIBS=
#r13 arm64 这里是ndk独立链接库地址
BASE_PATH = /Users/30san/Downloads/ndk_toolchain_r13_arm64/bin/
ndk r10 arm64
BASE_PATH = /Users/30san/Downloads/ndk_toolc/ndk_toolchain_r10_arm64/bin/
#ndk r20 arm64
BASE_PATH = /Users/30san/Downloads/ndk_toolchain_arm64/bin/
ARCH_FLAGS = –no-undefined
ARCH_LINK =
ANDROID_CC = $(BASE_PATH)/aarch64-linux-android-gcc
ANDROID_CXX = $(BASE_PATH)/aarch64-linux-android-g++
CFLAGS += -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing
1 |
|
独立工具链–arm参数参开这里
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh –arch=arm –install-dir=./toolchain
TOOL=arm-linux-androideabi
ARCH_FLAGS=”-mthumb”
ARCH_LINK=1
- armeabi-v7a
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh –arch=arm –install-dir=./toolchain
TOOL=arm-linux-androideabi
ARCH_FLAGS=”-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16”
export ARCH_LINK=”-march=armv7-a -Wl,–fix-cortex-a8”1
- arm64-v8a
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh –arch=arm64 –install-dir=./toolchain
TOOL=aarch64-linux-android
ARCH_FLAGS=
ARCH_LINK=
1 |
|
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh –arch=x86 –install-dir=./toolchain
TOOL=i686-linux-android
ARCH_FLAGS=”-march=i686 -msse3 -mstackrealign -mfpmath=sse”
ARCH_LINK=1
2
- x86_64
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh –arch=x86_64 –install-dir=./toolchain
TOOL=”x86_64-linux-android”
ARCH_FLAGS=”-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel”
ARCH_LINK=””1
- mips
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh –arch=mips –install-dir=./toolchain
TOOL=mipsel-linux-android
ARCH_FLAGS=
ARCH_LINK=1
2
- mips64
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh –arch=mips64 –install-dir=./toolchain
export TOOL=mips64el-linux-android
export ARCH_FLAGS=
export ARCH_LINK=1
2
3[NDK 交叉编译常用变量] (https://fucknmb.com/2017/06/22/NDK%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91%E5%B8%B8%E7%94%A8%E5%8F%98%E9%87%8F/)
## make 脚本
#written by Frank,westone Inc.
include ../params.make #这个指的就是定义make常量的文件
PROJ = SOF_Interface
CXXFLAGS += -Wall -D_REENTRANT -MMD -I./
LIBS += -L../lib/Linux -L../lib_third/Linux -lSOFApiWithHandle -lwcmUtil # 依赖的本地库,不需要带lib和后缀so,只需要-l+库名
CXXFLAGS += -I../include/SOF -I../include/mwUtil # 依赖的头文件名
CC = g++
CC = $(ANDROID_CC)
CXX = $(ANDROID_CXX)
CXXFLAGS += ${CFLAGS} -frtti -fexceptions
LDFLAGS += ${ARCH_LINK}
ARFLAGS =
LOCAL_LDFLAGS += -fuse-ld=bfd
SOURCES = $(wildcard .c .cpp)
OBJS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES)))
DEPS = $(patsubst %.o,%.d,$(OBJS))
MISS_DEPS = $(filter-out $(wildcard $(DEPS)), $(DEPS))
$(PROJ):$(MISS_DEPS) $(OBJS)
需要链接的命令在这里补充
$(CXX) -fPIC -fexceptions -shared -o ../lib/linux/lib$@.so.$(SO_VER) $(OBJS) $(LIBS)
-ln -s lib$@.so.$(SO_VER) ../lib/linux/lib$@.so && \
cp -f ./com_westone_wvcm_jni_SOFInterface.h ../include/SOF
clean:
rm -f .o .d lib$(PROJ).so core ../lib/Linux/lib$(PROJ).so*
$(MISS_DEPS):
rm -f $(patsubst %.d,%.o,$@)
-include $(DEPS)
1 |
|
adb shell
cd proc
cat cpuinfo1
2
## 查看生成so库的函数
nm -D xx.so1
2
## makefile中常见的错误—missing separator. Stop.—原因命令行缺少tab键
删除原来的空格 输入tab1
2
3
4
## openssl编译
这个是wst改过的源码,目前编译成功需要注释两个文件,目前不知道有啥后遗症
crypto/evp/c_all.c
crypto/evp/evp_acnf.c
1 |
|
LOCAL_LDFLAGS := -Wl,-Bsymbolic1
2
3
4
https://www.jianshu.com/p/1b60337991f9
- 编译脚本
ndk-build NDK_PROJECT_PATH=../ APP_BUILD_SCRIPT=../Android.mk1
2
3
4
5- NDK_PROJECT_PATH 参数 在shell脚本指定项目根目录路径
不存在的.h文件移动到includ目录
- crypto 模块32位会报错
注释了```bn/asm/armv4-mont.S \ ``` 这个汇编文件
arm_src_files := \
aes/asm/aes-armv4.S \
bn/asm/armv4-mont.S \
sha/asm/sha1-armv4-large.S \
sha/asm/sha256-armv4.S \
sha/asm/sha512-armv4.S
non_arm_src_files := aes/aes_core.c1
- crypto 编译静态库.a
include $(BUILD_STATIC_LIBRARY)1
2
3
4
## java 生成jni 头文件
- 生成class 文件
//在终端
javac /Users/30san/Desktop/SOFInterface/app/src/main/java/com/westone/wvcm/jni/SOFInterface.java1
2
- 生成jni的c头文件
javah -classpath /Users/30san/Desktop/SOFInterface/app/src/main/java -d jh com.westone.wvcm.jni.SOFInterface1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23* 1. -classpath 该工程目录下包含com.xx.xx 包名的目录
* -d 头文件的输出文件夹名称(该文件出现在命令中设定的·classpath路径下)如上面的:jh
执行上面的命令后生成头文件所在路径:/Users/30san/Desktop/SOFInterface/app/src/main/java/com/westone/wvcm/jni/jh/
* class类的全部包名
com.westone.wvcm.jni.SOFInterface
将头文件拷贝到jni所在的cpp目录,在cpp中includ头文件
## android 打包arr
android studio 右边的gradle->模块名->build-> assemble
[Jni打包及引用aar
](https://www.jianshu.com/p/fbbad39a9b57)
## android 打包jar
- 在android项目 ```file->new->new moudle```新建一个module,包名必须跟之前生成so库里方法名的包名一致
- so放置到libs下
- 在module下的gradle文件中配置打包任务(文件最后边补,在}外)
task makeJar(type: Copy) {
delete ‘build/libs/mylibrary.jar’ //删除已经存在的jar包
from(‘build/intermediates/bundles/release/‘)//从该目录下加载要打包的文件
into(‘build/libs/‘)//jar包的保存目录
include(‘classes.jar’)//设置过滤,只打包classes文件
rename(‘classes.jar’, ‘mylibrary.jar’)//重命名,mylibrary.jar 根据自己的需求设置
}
makeJar.dependsOn(build)1
2
- 在android studio的终端运行
gradlew makeJar1
2
## 编译c、c++ android可执行文件
/Users/30san/Desktop/ndk_toolchain/ndk_toolchain_r15_arm64/bin/aarch64-linux-android-clang -pie -fPIE /Users/30san/Desktop/ctest/app/src/main/jni/test.c -o test1
2
在android运行可执行文件
adb shell
cd 到 sdcard,然后找到可执行文件的 将文件cp 到/data/local/tmp
给可执行文件权限
chmod 777 test.o
运行
./test.o
1 |
/Users/30san/Desktop/ndk_toolchain/ndk_toolchain_r15_arm64/bin/aarch64-linux-android-clang -Wl,-rpath=/Users/30san/Desktop/middleware/main/lib/linux/ -L/Users/30san/Desktop/middleware/main/lib/linux/ -lSKFApi -pie -fPIE /Users/30san/Desktop/middleware/main/testCpp/src/ctest.c -o test1
2
## 查看so库依赖库
/Users/30san/Desktop/ndk_toolchain/android-ndk-r13b/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin/aarch64-linux-android-readelf -d1
2
## 加载libs的so库
sourceSets {
main {
jniLibs.srcDirs = [‘libs’]
}
}1
2
3
4
## 软模块编译
- 编译脚本
#!/bin/bash
android_abi=arm64-v8a
build_type=Release
build_dir=build_${android_abi}
toolchain=/Users/30san/Desktop/ndk_toolchain/android-ndk-r15c/build/cmake/android.toolchain.cmake
android_ndk=/Users/30san/Desktop/ndk_toolchain/android-ndk-r15c/
build_type=Release
gernerator=”Ninja”
rem Android 可选的各平台架构包括(本机安装模拟器用的是x86架构)
rem armeabi-v7a
rem arm64
rem arm64-v8a
rem x86
rem x86_64
rem mips
rem mips64
android_api=android-21
#remove the old project
if [ -e ${build_dir} ]
then
rm -rfv ${build_dir}
fi
#create build dir
mkdir ${build_dir}
cd ${build_dir}
cmake -DCMAKE_BUILD_TYPE=${build_type} ../../ -DCMAKE_TOOLCHAIN_FILE=${toolchain} -DANDROID_NDK=${android_ndk} -DCMAKE_BUILD_TYPE=${build_type} -DANDROID_ABI=${android_abi} -DCMAKE_GENERATOR=${gernerator} -DANDROID_PLATFORM=${android_api}
ninja
ninja install
#返回上一级目录
cd ..1
2
在软模块中间件新建```build_android```目录,然后运行该脚本,注意:ndk独立工具链在这里是写死路径的,可以更换,根目录添加make常量
SO_VER=1.0.1
SO_NAME_VER=1.0
CPPFLAGS=
LIBS=
#r13 arm64
BASE_PATH = /Users/30san/Downloads/ndk_toolchain_r13_arm64/bin/
#r15 arm32
BASE_PATH = /Users/30san/Desktop/ndk_toolchain/ndk_toolchain_r15_arm32/bin
BASE_PATH = /Users/30san/Desktop/ndk_toolchain/ndk_toolchain_r15_arm64/bin
ndk r10 arm64
BASE_PATH = /Users/30san/Downloads/ndk_toolc/ndk_toolchain_r10_arm64/bin/
#ndk r20 arm64
BASE_PATH = /Users/30san/Downloads/ndk_toolchain_arm64/bin/
#r13 arm64
ARCH_FLAGS = –no-undefined
ARCH_LINK =
#r13 arm32
ARCH_FLAGS = -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16
ARCH_LINK = -march=armv7-a -Wl,–fix-cortex-a8
#r13 arm64
ANDROID_CC = $(BASE_PATH)/aarch64-linux-android-gcc
ANDROID_CXX = $(BASE_PATH)/aarch64-linux-android-g++
#r13 arm32
ANDROID_CC = $(BASE_PATH)/aarch64-linux-android-clang
ANDROID_CXX = $(BASE_PATH)/aarch64-linux-android-clang++
CFLAGS += -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing1
2
3src下每一级的makefile文件修改编译器
## 生成公私钥以及签名证书
- 生成私钥
openssl ecparam -genkey -name SM2 -out sm2privkey.pem
- 生成公钥
openssl ec -in sm2privkey.pem -pubout -out sm2pubkey
- 证书请求
openssl req -new -key sm2privkey.pem -out certreq.csr
- 生成证书
openssl req -x509 -key sm2privkey.pem -in certreq.csr -out CASm2.cer -days 36501
2
## c 打印android日志
#include <android/log.h>
__android_log_print(ANDROID_LOG_INFO, “jni”,”creat_app temp = %s \n “,temp);
//查看打印的日志在终端执行
adb logcat -s jni //jni指的是上边打印的tag`