AS使用ndk-build进行编译so
项目目录结构如下:
- jni目录下放native代码,Android.mk和Application.mk给ndk-bundle使用
- jniLibs放生成的so
jni目录下mk脚本如下:
shell
/MyApplication/app/src/main/jni
$ cat Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmyapplication
LOCAL_SRC_FILES := \
native-lib.cpp
LOCAL_PRODUCT_MODULE := true
include $(BUILD_SHARED_LIBRARY)
shell
/d/work/code/MyApplication/app/src/main/jni
$ cat Application.mk
APP_ABI := armeabi-v7a arm64-v8a # 指定abi
APP_STL := c++_static # 指定标准库
编译生成so
通过ndk-build编译生成的so默认放在libs中,然后将so拷贝到jniLibs
cmd
\MyApplication\app\src\main\jni>D:\work\soft\Android\Sdk\ndk-bundle\ndk-build.cmd
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-16.
[armeabi-v7a] Compile++ thumb: myapplication <= native-lib.cpp
[armeabi-v7a] SharedLibrary : libmyapplication.so
[armeabi-v7a] Install : libmyapplication.so => libs/armeabi-v7a/libmyapplication.so
[arm64-v8a] Compile++ : myapplication <= native-lib.cpp
[arm64-v8a] SharedLibrary : libmyapplication.so
[arm64-v8a] Install : libmyapplication.so => libs/arm64-v8a/libmyapplication.so
AS使用cmake进行编译
现在基本都使用这种做法,用AS新建一个native c++工程即可
源码中编译项目
使用bp
可以参考下面mk脚本写法,会在/product/app和/product/lib下面生成apk和so,然后可以手动push到系统中
makefile
MyApplication$ cat Android.bp
android_app {
name: "MyApplication",
srcs: ["app/src/main/**/*.java"],
resource_dirs: ["app/src/main/res"],
manifest: "app/src/main/AndroidManifest.xml",
static_libs: [
"android-support-v4",
"android-support-v7-appcompat",
"android-support-v7-appcompat",
"android-support-design"
],
sdk_version: "current",
product_specific: true,
optimize: {
proguard_flags_files: ["proguard.flags"],
},
// 指定jni模块
jni_libs: [
"libmyapplication",
],
}
``
```makefile
MyApplication/app/src/main/jni$ cat Android.bp
cc_library_shared { // 编译生成共享库
name: "libmyapplication", // so名称
shared_libs: [ // 引入一些依赖库
"liblog",
],
sdk_version: "17",
srcs: ["native-lib.cpp"], // 指定cpp源文件
product_specific: true, // 编译产物位于/product/lib64
cflags: [
"-ffast-math",
"-O3",
"-funroll-loops",
"-Wextra",
],
arch: { // 指定abi架构
arm: {
instruction_set: "arm",
},
},
header_libs: ["jni_headers"], // 引入jni.h,高版本需要手动引入
stl: "c++_static", // 引入c++标准库
}
使用mk
makefile
MyApplication$ cat Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_USE_AAPT2 := true
LOCAL_MODULE_TAGS := optional
LOCAL_PACKAGE_NAME := MyApplication
LOCAL_PRODUCT_MODULE := true
LOCAL_MULTILIB=32
LOCAL_STATIC_ANDROID_LIBRARIES := \
android-support-v4 \
android-support-v7-appcompat \
android-support-v13 \
android-support-design \
LOCAL_SRC_FILES := $(call all-java-files-under, app/src/main)
LOCAL_MANIFEST_FILE := app/src/main/AndroidManifest.xml
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/app/src/main/res
LOCAL_JNI_SHARED_LIBRARIES := libmyapplication
LOCAL_CERTIFICATE := platform
LOCAL_DEX_PREOPT := false
LOCAL_PROGUARD_ENABLED := disabled
include $(BUILD_PACKAGE)
include $(LOCAL_PATH)/app/src/main/jni/Android.mk
makefile
MyApplication$ cat app/src/main/jni/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmyapplication
LOCAL_HEADER_LIBRARIES := jni_headers # (源码在编译时)Andorid S版本上面默认去掉了JINI_H_INCLUDE定义, 需要手动引用
LOCAL_SRC_FILES := \
native-lib.cpp
LOCAL_CPPFLAGS += -frtti -fexceptions
LOCAL_CFLAGS += -Wall -Wextra -Werror -Wno-error=constant-conversion -Wno-unused-parameter -Wno-unused-variable -Wno-sign-compare -Wno-format-extra-args
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_PRODUCT_MODULE := true
include $(BUILD_SHARED_LIBRARY)