Android studio 项目支持JNI方法
步骤:
1. build.gradle 配置如下,主要两项 ndk 和 sourceSets
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.shuguo.myapplication"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
moduleName "native-lib"
ldLibs "log", "z", "m"
abiFilters "armeabi", "armeabi-v7a", "x86"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jni/libs']
}
}
}
2. 创建jni目录,file->new->folder->jnifolder
3. 配置ndk-build
4. 创建.h文件,cpp文件
5. 编写Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native-lib
LOCAL_C_INCLUDES := com_shuguo_myapplication_JniClass.h
LOCAL_SRC_FILES := com_shuguo_myapplication_JniClass.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
6. 编写Application.mk
APP_ABI := armeabi armeabi-v7a x86
7. 在cmd中使用如下命令
C:\Users\baoyi.yang\Desktop\MyApplication\app\src\main\jni>ndk-build NDK_PROJECT
_PATH=. APP_BUILD_SCRIPT=./Android.mk NDK_APPLICATION_MK=./Application.mk
8. 编译
请注明出处,此文档来自“善思善学”。