在Android studio2.2以前大部分用的是用javah的方式来编译jni,在编译之前首先确保有安装ndk,如果没下载,则下载ndk,下载到默认目录就好。如图所示:
下面具体介绍一下流程:
1、新建项目JniDemo,打开local.properties,在最下面加上(此处根据你的sdk和ndk目录而定):
ndk.dir=E\:\\AndroidStudio\\sdk\\ndk-bundle
sdk.dir=E\:\\AndroidStudio\\sdk
如图:
2、新建类JniUtil.java,添加native方法和指定的lib库,如图:
public class JniUtil {
public native String getStringFromJni();
public native int getSum(int i,int j);
static {
System.loadLibrary("Jni_lib");
}
}
3、打开app目录下的build.gradle文件,添加如下配置,注意moduleName后面的要和System.loadLibrary(“Jni_lib”)保持一致:
ndk {
moduleName "Jni_lib" //编译生成so库的名字,注意不要lib,和.so加进来 ,而且要和上面loadLibrary里面的参数一致
abiFilters "armeabi","armeabi-v7a","x86"//编译支持的平台
}
4、在gradle.properties文件的最后一行添加一句:android.useDeprecatedNdk=true
5、此时点击build -> Make Project,然后在app->build->intermediates->classes->debug->com下面会看到编译的文件,JniUtil.class就是刚才编译的文件,如图:
6、利用cd命令进入到main目录下:
然后利用javah -d jni -classpath命令,javah -d jni -classpath E:\AndroidStudioProjects\ndk\app\build\intermediates\classes\debug com.sonic.jnidemo.JniUtil
如下图:
注意:红框里面的是debug文件夹的绝对路径(项目的存放目录),后面以包名的形式对应JniUtil这个类,此时会 在main目录下看到自动生成一个jni文件夹,里面有一个.h文件,如图:
7、新建main.c文件,然后导入刚才生成的.h头文件,并且将头文件里面的方法复制过来实现即可:
#include "com_sonic_jnidemo_JniUtil.h"
JNIEXPORT jstring JNICALL Java_com_sonic_jnidemo_JniUtil_getStringFromJni
(JNIEnv *env, jobject j){
return (*env)->NewStringUTF(env,"I am jni!");
}
JNIEXPORT jint JNICALL Java_com_sonic_jnidemo_JniUtil_getSum
(JNIEnv *env, jobject j, jint m, jint n){
return m + n;
}
8、在MainActivity里面调用:
至此,如果你的Android studio在2.3以下,现在运行就可以看到结果了,但是如果你升级到了3.0以上,会发现运行出错,提示如下:
Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio. Please switch to a supported build system.
Consider using CMake or ndk-build integration. For more information, go to:
https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile
To get started, you can use the sample ndk-build script the Android
plugin generated for you at:
E:\androidproject\JniDemo2\app\build\intermediates\ndk\debug\Android.mk
Alternatively, you can use the experimental plugin:
https://developer.android.com/r/tools/experimental-plugin.html
To continue using the deprecated NDK compile for another 60 days, set
android.deprecatedNdkCompileLease=1511489294355 in gradle.properties
是什么原因导致的呢?就是useDeprecatedNdk这个东西不再支持了,请切换到 CMake or ndk-build来编译Jni的C换C++代码,解决方式如下,打开app目录下的build.gradle,继续添加配置:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.sonic.jnidemo"
minSdkVersion 18
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk {
moduleName "Jni_lib" //编译生成so库的名字,注意不要lib,和.so加进来 ,而且要和上面loadLibrary里面的参数一致
abiFilters "armeabi","armeabi-v7a","x86"//编译支持的平台
}
}
sourceSets.main {
// jniLibs.srcDir 'libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
然后在main文件下面新建
CMakeLists.txt文件,类容如下:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
Jni_lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/main.c )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
Jni_lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
此时再去编译运行就没问题了,运行截图:
demo地址:点击打开链接