概论

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安装

Android NDk环境配置_android

配置环境变量

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


Android NDk环境配置_android_02



新建hello-jni.c  




/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <string.h>
#include <jni.h>

/* This is a trivial JNI example where we use a native method
* to return a new VM String. See the corresponding Java source
* file located at:
*
* apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
*/
//jstring
//Java_com_bazhangkeji_MainActivity_stringFromJNI( JNIEnv* env,
// jobject thiz )
//{
// return (*env)->NewStringUTF(env, "Hello from JNI !");
//}

JNIEXPORT jstring JNICALL
Java_com_xzh_ndkdemo_MainActivity_stringFromJNI(JNIEnv *env, jobject instance) {

// return (*env)->NewStringUTF(env, returnValue);
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello_jni
LOCAL_SRC_FILES := hello_jni.c

include $(BUILD_SHARED_LIBRARY)


注意,这里的.c的写法必须遵循如下规则:函数需按照规则命名:Java_包名类名方法名

LOCAL_MODULE    := hello_jni
LOCAL_SRC_FILES := hello_jni.c


必须和.c文件对应,然后我们在build.gradle中加入如下语句:


externalNativeBuild {
ndkBuild {
path file("src\\main\\jni\\Hello.mk")
}
}

然后rebuild 。


测试Activity


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

private void init() {
System.out.println("hello ndkdemo"+stringFromJNI());
}
public native String stringFromJNI();
static {
System.loadLibrary("hello_jni");
}
}


Android NDk环境配置_android_03



如果我们.so文件,则需要在local.properties文件中 配置好本地NDK的路径。


ndk.dir=/Users/cinba/Library/android-ndk-r10e
sdk.dir=/Users/cinba/Library/android-sdk

然后重新build,就可以看到.so文件了