1.配置Anroid Studio(这步是关键)
使用[command+,] 打开Preferences,选择External Tools,点击加号框如下图:
Paste_Image.png
点击+号之后,打开Macros 配置宏命令界面,如下图:
Paste_Image.png setting.png javah.png ndk-build.png ndk-build clean.png2.配置完成就可以使用javah 、ndk-build、ndk-build clean这三个命令了,那么在哪里使用这些命令,请看下图
tools.png3.配置完成,我们开始新建工程MyNdk,然后新建jni目录
create jni.png4.修改app下的build.gradle文件, 如下图
ndk{
moduleName "MyLibrary"
}
sourceSets.main{
jni.srcDirs = []
jniLibs.srcDir "src/main/libs"
}
app build.gradle.png
5.修改MyNdk下的gradle.properties文件(如果没有此文件,自己新建一个),如下图
android.useDeprecatedNdk=true
MyNdk gradle.properties.png
6.新建MyNdk.java
public class MyNdk {
static {
System.loadLibrary("MyLibrary");
}
public native String getString();
}
MyNdk.java.png
注:先build下整个工程,然后执行第7步
7.这步开始创建头文件,操作如下图(注意在MyNdk.java文件上点击右键)
8.执行完成后jni目录下就创建了com_zhangyp_higo_myndk_MyNdk.h文件,然后我们在jni目录下编写 MyLibrary.cpp、Android.mk、Application.mk这三个文件,代码如下:
jni files.png
com_zhangyp_higo_myndk_MyNdk.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_zhangyp_higo_myndk_MyNdk */
#ifndef _Included_com_zhangyp_higo_myndk_MyNdk
#define _Included_com_zhangyp_higo_myndk_MyNdk
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_zhangyp_higo_myndk_MyNdk
* Method: getString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_zhangyp_higo_myndk_MyNdk_getString
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
MyLibrary.cpp
#include "com_zhangyp_higo_myndk_MyNdk.h"
JNIEXPORT jstring JNICALL Java_com_zhangyp_higo_myndk_MyNdk_getString
(JNIEnv * env, jobject obj){
return (*env).NewStringUTF("This is mylibrary !!!");
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyLibrary
LOCAL_SRC_FILES =: MyLibrary.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_MODULES := MyLibrary
APP_ABI := all
9.如何执行ndk-build,请看下图:(注意在jni目录上点击右键)
make ndk-build .png10.得到so文件,如下图
so file.png12.在MainActivity中调用,代码如下
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(new MyNdk().getString());
}
}
12.大功告成,可以运行了,oh yet!
补充:app的build.gradle 加入如下脚本,C/C++ 代码就有自动提示了,也可以方法间跳转
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
sourceSets.main{
jni.srcDirs = ['src/main/jni']
jniLibs.srcDir "src/main/libs"
}
MyLibrary.png
13.如果需要使用Android Studio调试c/c++请看这篇文章:Android Studio NDk调试(基于gradle-experimental插件与LLDB)
1.Android端使用retrofit上传多张图片,后端使用Php接收图片(包含前后端代码)
2.Android Studio NDk调试(基于gradle-experimental插件与LLDB)
3.让Android开发者相见恨晚的软件及插件
4.GitHub上一些超炫的Android开源项目推荐
网友评论
改成这样:
-classpath "$Classpath$" -v -jni $FileClass$
Error:(6, 0) Could not find property 'android' on repository container.
<a href="openFile:C:\Users\Administrator\Desktop\1607\17day\MyJniDemo\build.gradle">Open File</a>
Error:(15, 0) Gradle DSL method not found: 'ndk()'
Possible causes:<ul><li>The project 'MyJNI' may be using a version of Gradle that does not contain the method.
<a href="openGradleSettings">Gradle settings</a></li><li>The build file may be missing a Gradle plugin.
<a href="apply.gradle.plugin">Apply Gradle plugin</a></li>
求定位
"C:\Program Files\Java\jdk1.7.0_80\bin\javah" -bootclasspath E:\android-sdk\platforms\android-21\android.jar -o E:\AndroidSource\Demos\TestNDK\app\src\main\jni com.tpg.rcs.testndk.MyNdk
Exception in thread "main" java.lang.IllegalArgumentException: directories not supported
at com.sun.tools.javac.file.RegularFileObject.<init>(RegularFileObject.java:68)
at com.sun.tools.javac.file.RegularFileObject.<init>(RegularFileObject.java:62)
at com.sun.tools.javac.file.JavacFileManager.getJavaFileObjectsFromFiles(JavacFileManager.java:805)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:468)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:335)
at com.sun.tools.javah.Main.main(Main.java:46)
Process finished with exit code 1
加一个引号就可以了,参考链接:http://stackoverflow.com/questions/40158542/i-am-trying-to-generate-h-file-in-android-studio-for-ndk
但是在android N 以下(测试5.1 sdk22)生可以生成…why
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:129)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:107)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:64)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:503)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:329)
at com.sun.tools.javah.Main.main(Main.java:46)
这是什么原因啊。楼主,有没有demo,给发个呗
加一个引号就可以了,参考链接:http://stackoverflow.com/questions/40158542/i-am-trying-to-generate-h-file-in-android-studio-for-ndk
请指教