1.环境搭建
1.Android Studio中下载如下图的三个tools
2.配置ndk环境变量,path中添加ndk的路径
3.命令行输入ndk-build,出现如下图输出,代表环境变量配置成功
4.Android Studio中新建native方法并导入链接库文件
5.打开Android studio自带终端,进入项目app\src\main\java目录,执行:
javah -jni -d …/jni com.example.myapplication.MainActivity
生成对应.h文件
6…h文件只是定义了方法,还需要创建对应c文件对该方法进行实现
7.创建Android.mk和Application.mk
LOCAL_PATH :=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=hello
LOCAL_SRC_FILES :=com_example_app7_MainActivity.cpp
include $(BUILD_SHARED_LIBRARY)
APP_MODULES :=hello
APP_ABI :=all
8.还需要在build.gradle和gradle.properties添加:
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.tct.jniapplication"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk{
moduleName "hello"
abiFilters "armeabi-v7a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDirs = ['src/main/jni']
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
9.最后在jni目录下执行命令编译so库:ndk-build
以上是编译成功
然后把apk安装到设备即可,log中打印”Hello Jni”,第一个ndk程序调试成功
版权声明:本文为qq_34211365原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。