在源码中编译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)
相关推荐
xvch29 分钟前
Kotlin 2.1.0 入门教程(七)
android·kotlin
望风的懒蜗牛44 分钟前
编译Android平台使用的FFmpeg库
android
浩宇软件开发1 小时前
Android开发,待办事项提醒App的设计与实现(个人中心页)
android·android studio·android开发
ac-er88882 小时前
Yii框架中的多语言支持:如何实现国际化
android·开发语言·php
苏金标3 小时前
The maximum compatible Gradle JVM version is 17.
android
zhangphil3 小时前
Android BitmapShader简洁实现马赛克,Kotlin(一)
android·kotlin
iofomo7 小时前
Android平台从上到下,无需ROOT/解锁/刷机,应用级拦截框架的最后一环,SVC系统调用拦截。
android
我叫特踏实8 小时前
SensorManager开发参考
android·sensormanager
五味香10 小时前
Java学习,查找List最大最小值
android·java·开发语言·python·学习·golang·kotlin
graceyun15 小时前
C语言进阶习题【1】指针和数组(4)——指针笔试题3
android·java·c语言