在源码中编译jni

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)
相关推荐
叶羽西3 小时前
Android Overlay Priority Rules
android
文韬_武略5 小时前
android 混淆
android
raychongzi5 小时前
The option ‘android.enableAapt2‘ is deprecated and should not be used anymore.
android
Tongsr8 小时前
Compose 仿微信朋友圈九宫格控件以及背后的原理
android·前端
海绵宝宝_8 小时前
【HarmonyOS NEXT】ArkTs数据类型解析与使用
android·前端·华为·harmonyos·鸿蒙
明天就是Friday8 小时前
Android Binder 进程间通信
android·binder
lansus9 小时前
智能座舱进阶-应用框架层-Jetpack主要组件
android·智能座舱
Zender Han9 小时前
Flutter 中的 Flexible 与 Expanded 的介绍、区别与使用
android·flutter·ios
xuexizhe7712 小时前
iPhone恢复技巧:如何从 iPhone 恢复丢失的照片
android·安全·ios·智能手机·电脑·手机·iphone
小菜琳12 小时前
应用如何借用manifestxml追加gid权限
android