AndroidStudio jni 开发

Step 1、
编写native方法类文件,缩写完成后,
AndroidStudio中 build -> make project
Step 21)在 /build/intermediates/classes/debug/… 路径找到 类.class
2)在 Terminal 窗口中执行
javah -d [output目录] -classpath ./app/build/intermediates/classes/debug/ [包+类 完整名称]-d 后面的目录是生成 文件目录
-classpath 后面的目录是编译class目录
Step 3、
在生成的jni目录中编写c文件代码,编写完成后,
1)AndroidStudio中 build -> make project
提示
Error:Execution failed for task ':app:compileDebugNdk'.
> Error: NDK integration is deprecated in the current plugin.  
Consider trying the new experimental plugin.  
For details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental.  
Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration.

在项目根目录中的 gradle.properties 文件中增加
android.useDeprecatedNdk=true

2)AndroidStudio中 build -> make project
提示
Error:Execution failed for task ':app:compileDebugNdk'.
> NDK not configured.
  Download the NDK from http://developer.android.com/tools/sdk/ndk/.
  Then add ndk.dir=path/to/ndk in local.properties.
  (On Windows, make sure you escape backslashes, e.g. C:\ndk rather than C:
dk)
配置NDK目录到 local.properties 文件中
ndk.dir=/Users/miles/Library/Android/ndk/android-ndk-r10e
3)在AndroidStudio中 build -> make project
提示完成
Step 4、
编译APK安装,调用方法提示
java.lang.UnsatisfiedLinkError: 
    Couldn't load HelloLibs from loader dalvik.system.
    PathClassLoader[DexPathList[[zip file “/*******-1.apk"]
打开APP module中的 build.gradle,
增加代码到defaultConfig中
例如
defaultConfig {

    applicationId “***********”

    minSdkVersion 14

    targetSdkVersion 23

    versionCode 1

    versionName "1.0"

    ndk {

        moduleName "HelloLibs"   // 生成库的名称 默认前面会加 lib  ( libHelloLibs.so )
                  
    }
}
Step 5、
编译生成 执行,OK
生成的SO库在 APP module中的 build/intermediates/ndk/ 中
AndroidStudio 项目中 引用 .so 库文件
1、在main目录中创建 jniLibs目录
2、将armeabi / x86 / armeabi-v7a / 等等目录复制到jniLibs中
3、引用的java文件,包名不要变,在做库文件的时候,直接封装成jar,供项目使用 
原文地址:https://www.cnblogs.com/smile365/p/5143433.html