https://blog.youkuaiyun.com/Hi_Red_Beetle/article/details/78994767
https://blog.youkuaiyun.com/zeqiao/article/details/77893167
在AS中进行NDK开发之前,我们先来简单的介绍几个大家都容易搞懵的概念:
1. 到底什么是JNI,什么是NDK?
2. 何为“交叉编译”?
先看什么是JNI?JNI的全称就是Java Native Interface,即java本地开发接口。可能大家和我一样,一听到接口什么的就犯懵:“我也知道这是java本地开发接口的意思,但它具体是个什么意思我还是搞不明白。”其实JNI它就是一种协议,一说协议,那它就是对某种东西的一个规范和约束,说的好听一点就是标准化。如果你想用我这个东西,那你必须要遵守我这边的规范。像http协议一样,http作为超文本传输协议,它规范了我们上网时从客户端到服务器端等一系列的运作流程。正因为如此,我们才能畅通无阻的上网。那么换做JNI也一样,只不过JNI这个协议是用来沟通java代码和外部的本地代码(c/c++)。也就是说有了JNI这个协议,我们才能够随意的让java代码调用C/C++的代码,同样C/C++的代码也可以调用java的代码。如果没有这个协议作为支撑,那么java和C/C++代码想要相互调用是不可能的。下面通过两个图简单看一下JNI协议在系统架构中处于什么位置:

在上图中,上层绿色的部分一般都是用Java代码写的,下层橘黄色的部分一般都是用C/C++代码写的。可以看出,正式由于有了中间JNI的存在我们才可以在Application层通过JNI调用下层中的一些东西。了解了JNI的概念后,我们再看看NDK,NDK(Native Development Kit)就比较好理解了,它就是一个本地开发的“工具包”。Java开发要用到JDK,Android开发要用到SDK,那我们在Android中要进行native开发,也要用到它对应的工具包,即NDK。通俗的来讲,NDK就是帮助我们可以在Android应用中使用C/C++来完成特定功能的一套工具。 NDK的作用有很多,我们简单的列举两个,比如:
1.首先NDK可以帮助开发者“快速”开发C(或C++)的动态库。
2.其次,NDK集成了“交叉编译器”。使用NDK,我们可以将要求高性能的应用逻辑使用C开发,从而提高应用程序的执行效率。
上面提到了“交叉编译”,我们最后再解释一下什么是交叉编译。大家都知道编译器在将中间代码连接成当前计算机可执行的二进制程序时,连接程序会根据当前计算机的CPU、操作系统的类型来转换。而根据运行的设备的不同,CPU的架构也是不同,大体有如下三种常见的CUP架构:
- arm结构 :主要在移动手持、嵌入式设备上。我们的手机几乎都是使用的这种CUP架构。
- x86结构 : 主要在台式机、笔记本上使用。如Intel和AMD的CPU 。
- MIPS架构:多用在网关、猫、机顶盒等设备。


-
```
-
<?xml version= "1.0" encoding= "utf-8"?>
-
<LinearLayout
-
xmlns:android= "http://schemas.android.com/apk/res/android"
-
android:layout_width= "match_parent"
-
android:layout_height= "match_parent"
-
android:orientation= "vertical">
-
-
<TextView
-
android: id= "@+id/textview"
-
android:layout_width= "wrap_content"
-
android:layout_height= "wrap_content"
-
android:text= "Hello World!"
-
/>
-
<Button
-
android: id= "@+id/button"
-
android:layout_width= "match_parent"
-
android:layout_height= "wrap_content"
-
android:text= "button"/>
-
</LinearLayout>
-
```
-
```
-
public class MainActivity extends AppCompatActivity {
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
final TextView textview = findViewById(R.id.textview);
-
Button button = findViewById(R.id.button);
-
-
button.setOnClickListener( new View.OnClickListener() {
-
-
public void onClick(View v) {
-
textview.setText(JNIUtils.sayHelloFromJNI());
-
}
-
});
-
}
-
}
-
```
上面代码中的JNIUtils.sayHelloFromeJNI()就是我们在与MainActivity相同的包中新建JNIUtils类后在里面编写的native方法。如下所示:

可以看到我们上面的sayHelloFromJNI()方法显示的是警告红色。把鼠标放到上面,它会提示我们对应的JNI头文件没有查找到。那么接下来我们要做的就是去生成与这个sayHelloFromJNI()方法所对应的头文件。

-
```
-
/* DO NOT EDIT THIS FILE - it is machine generated */
-
-
/* Header for class com_example_zhangxudong_jnidemo_JNIUtils */
-
-
-
-
-
extern "C" {
-
-
/*
-
* Class: com_example_zhangxudong_jnidemo_JNIUtils
-
* Method: sayHelloFromJNI
-
* Signature: ()Ljava/lang/String;
-
*/
-
JNIEXPORT jstring JNICALL Java_com_example_zhangxudong_jnidemo_JNIUtils_sayHelloFromJNI
-
(JNIEnv *, jclass);
-
-
}
-
-
-
```

输入要新建的C/C++文件名称JNIHello,这里我们用C++来编写,所以Type为.cpp,如果你选择用C来编写,那么Type选为.c,点击ok。这里说一下,在我们进行NDK开发的时候,选择用C还是C++,在编写代码的时候除了C和C++基本的语法不同外,还是有许多不同地方需要注意。我们后续会慢慢介绍。这里先默认跟着我的步骤来。

-
-
JNIEXPORT jstring JNICALL Java_com_example_zhangxudong_jnidemo_JNIUtils_sayHelloFromJNI
-
(JNIEnv *env, jclass jclass){
-
return env->NewStringUTF( "Hello World From JNI!!!!!");
-
}
可以看到我们首先需要把原来生成的JNIUtlis对应的头文件引入进来,下面的代码基本都是从com_example_zhangxudong_jnidemo_JNIUtils.h中复制粘贴过来的一部分,然后稍加修改。修改的地方主要有sayHelloFromJNI的两个参数和里面的简单实现,参数方面就是加了env和jclass两个字段。函数里面的实现呢,就是简单的返回一个字符串“Hello World From JNI!!!!!”,至于为什么这么写,我会在下一篇文章进行讲解,大家现在就需要知道如果要在这里返回一个字符串就必须要通过env->NewStringUTF("xxxxxx");这行代码。

-
```
-
public class JNIUtils {
-
static {
-
System.loadLibrary( "JNIHello");
-
}
-
public static native String sayHelloFromJNI();
-
}
-
```
-
```
-
Error:Execution failed for task ':app:compileDebugNdk'.
-
> 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:\JNIDemo\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= 1515317190556 in gradle.properties
-
```

那我们生成的动态库(.so文件)都在哪里呢?点开app--->build--->intermediates--->ndk--->debug--->libs,可以看到各个平台对应的动态库都已经生成了。

