Android NDk环境配置

来源:互联网 发布:linux缺少iso 9660图像 编辑:程序博客网 时间:2024/06/05 15:54

概论

NDK全称是Native Development Kit,NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk。NDK集成了交叉编译器(交叉编译器需要UNIX或LINUX系统环境),并提供了相应的mk文件隔离CPU、平台、ABI等差异,开发人员只需要简单修改mk文件(指出“哪些文件需要编译”、“编译特性要求”等),就可以创建出so。

JNI的全称是Java Native Interface,它提供了若干的API实现了Java和其他语言的通信(主要是C和C++)。

联系和区别:

为什么使用NDK?

1、代码的保护。由于apk的java层代码很容易被反编译,而C/C++库反汇难度较大。

2、可以方便地使用现存的开源库。大部分现存的开源库都是用C/C++代码编写的。

3、提高程序的执行效率。将要求高性能的应用逻辑使用C开发,从而提高应用程序的执行效率。

4、便于移植。用C/C++写得库可以方便在其他的嵌入式平台上再次使用。

为什么使用JNI?

JNI的目的是使java方法能够调用c实现的一些函数。

所以从这里可以看出,是先有NDK开发,然后才有了JNI的调用。


环境

  • 主机:WIN10
  • 开发环境:Android Studio2.2.2
首先要电脑安装了NDK环境,如果没有可以在studio安装

配置环境变量

  • 增加一项:NDK_ROOT,如:D:\android\sdk\ndk-bundle(这里是sdk的路径)
  • 在path中增加%NDK_ROOT%

新建hello-jni.c 

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /*  
  2.  * Copyright (C) 2009 The Android Open Source Project  
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License");  
  5.  * you may not use this file except in compliance with the License.  
  6.  * You may obtain a copy of the License at  
  7.  *  
  8.  *      http://www.apache.org/licenses/LICENSE-2.0  
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" BASIS,  
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.  * See the License for the specific language governing permissions and  
  14.  * limitations under the License.  
  15.  *  
  16.  */  
  17. #include <string.h>  
  18. #include <jni.h>  
  19.   
  20. /* This is a trivial JNI example where we use a native method  
  21.  * to return a new VM String. See the corresponding Java source  
  22.  * file located at:  
  23.  *  
  24.  *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java  
  25.  */  
  26. //jstring  
  27. //Java_com_bazhangkeji_MainActivity_stringFromJNI( JNIEnv* env,  
  28. //                                                  jobject thiz )  
  29. //{  
  30. //    return (*env)->NewStringUTF(env, "Hello from JNI !");  
  31. //}  
  32.   
  33. JNIEXPORT jstring JNICALL  
  34. Java_com_xzh_ndkdemo_MainActivity_stringFromJNI(JNIEnv *env, jobject instance) {  
  35.   
  36. //    return (*env)->NewStringUTF(env, returnValue);  
  37.     return (*env)->NewStringUTF(env, "Hello from JNI !");  
  38. }  
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="1971608" snippet_file_name="blog_20161107_2_1840276" name="code" class="html"># Copyright (C) 2009 The Android Open Source Project  
  2. #  
  3. # Licensed under the Apache License, Version 2.0 (the "License");  
  4. # you may not use this file except in compliance with the License.  
  5. # You may obtain a copy of the License at  
  6. #  
  7. #      http://www.apache.org/licenses/LICENSE-2.0  
  8. #  
  9. # Unless required by applicable law or agreed to in writing, software  
  10. # distributed under the License is distributed on an "AS IS" BASIS,  
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  12. # See the License for the specific language governing permissions and  
  13. # limitations under the License.  
  14. #  
  15. LOCAL_PATH := $(call my-dir)  
  16.   
  17. include $(CLEAR_VARS)  
  18.   
  19. LOCAL_MODULE    :hello_jni  
  20. LOCAL_SRC_FILES :hello_jni.c  
  21.   
  22. include $(BUILD_SHARED_LIBRARY)</pre>  

注意,这里的.c的写法必须遵循如下规则:函数需按照规则命名:Java_包名类名方法名
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. LOCAL_MODULE    :hello_jni  
  2. LOCAL_SRC_FILES :hello_jni.c  
必须和.c文件对应,然后我们在build.gradle中加入如下语句:
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. externalNativeBuild {  
  2.         ndkBuild {  
  3.             path file("src\\main\\jni\\Hello.mk")  
  4.         }  
  5.     }  
然后rebuild 。
测试Activity
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.         init();  
  8.     }  
  9.   
  10.     private void init() {  
  11.         System.out.println("hello ndkdemo"+stringFromJNI());  
  12.     }  
  13.     public native String stringFromJNI();  
  14.     static {  
  15.         System.loadLibrary("hello_jni");  
  16.     }  
  17. }  



如果我们.so文件,则需要在local.properties文件中 配置好本地NDK的路径。
[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. ndk.dir=/Users/cinba/Library/android-ndk-r10e  
  2. sdk.dir=/Users/cinba/Library/android-sdk  
然后重新build,就可以看到.so文件了
0 0