谢谢 叶俊杰 网友的分享,我自己也做了些总结。
我使用的是android studio 2.2版本。把编写过程留下来,便于日后翻阅。
1. 创建一个新的工程
2. 创建一个新的类 JniTest.java 点击Build--Make Project 后
package com.jnitest; public class jniTest { static { System.loadLibrary("jnitest"); } public native static String getCLangString(); }
配置ndk开发包,File->Project Structure...
或者,选中工程 点击F4键 sdk location 中 Android ndk location 选择配置ndk 的路径
保险起见 点击查看 local.properties 文件
ndk.dir = C \:\\ Users \\ Administrator \\ AppData \\ Local \\ Android \\ Sdk \\ ndk-bundle
sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk下一步
查看 项目目录下的 E:\appdemo\app\build\intermediates\classes\debug 生成了class文件
接下来 点击底下的Terminal窗口 cd 到当前项目src目录
E:\appdemo>cd E:\appdemo\app\src\main\java
使用javah 生成头文件
E:\appdemo\app\src\main\java>javah -jni com.jnitest.jniTest
在 E:\appdemo\app\src\main\java
下可以看到生成的 com_jnitest_jniTest.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_csht_jniTest */ #ifndef _Included_com_csht_jniTest #define _Included_com_csht_jniTest #ifdef __cplusplus extern "C" { #endif /* * Class: com_csht_jniTest * Method: getCLangString * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_csht_jniTest_getCLangString (JNIEnv *, jclass); #ifdef __cplusplus } #endif #endif
在项目的src 文件下的 创建jni文件夹 注意 (右键main - new - Folder - JniFolder )
将生成的文件com_jnitest_jniTest.h 剪切到jni文件夹下,(否则可能出现以下错误)
Error:Execution failed for task ':app:compileDebugNdk'.
> Error: Your project contains C++ files but it is not using a supported native build system.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
http://tools.android.com/tech-docs/new-build-system/gradle-experimental.
在jni 目录下创建一个com_jnitest_jniTest.c 文件
生成 .so 库
此时报了 Error:(13, 0) 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.
<a href="openFile:E:\text\MyJniDemo\app\build.gradle">Open File</a>
没关系
此时 补全 .appdemo下MainActivity的代码
D:\Work\appDemo\app\build\intermediates\ndk\debug\lib\armeabi\libjnitest.so
> Error: Your project contains C++ files but it is not using a supported native build system.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
http://tools.android.com/tech-docs/new-build-system/gradle-experimental.
在jni 目录下创建一个com_jnitest_jniTest.c 文件
#include "com_jnitest_jniTest.h" JNIEXPORT jstring JNICALL Java_com_csht_jniTest_getCLangString(JNIEnv *env, jobject obj) { return (*env)->NewStringUTF(env,"NDK JNI developer!"); }
下一步, 在app的build.gradle;
1 defaultConfig下添加 2 ndk{ 3 moduleName "jnitest" //生成的so名字 4 abiFilters "armeabi", "armeabi-v7a", "x86" //输出指定三种abi体系结构下的so库 5 }
点击build-> make project
生成 .so 库
此时报了 Error:(13, 0) 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.
<a href="openFile:E:\text\MyJniDemo\app\build.gradle">Open File</a>
没关系
我们在 工程下的gradle.properties 添加属性
android.useDeprecatedNdk=true
此时 补全 .appdemo下MainActivity的代码
1 package com.appdemo.jnitest; 2 3 import android.app.Activity; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.widget.TextView; 7 8 public class MainActivity extends Activity { 9 11 private TextView txtView; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 txtView= (TextView) findViewById(R.id.txt_jnitext); 18 JniText jniText=new JniText(); 19 txtView.setText(jniText.getCLangString()); 20 } 21 }
D:\Work\appDemo\app\build\intermediates\ndk\debug\lib\armeabi\libjnitest.so