Android Studio NDK Foundation uses

Source: Internet
Author: User

What is the NDK?

Android platform is based on Java implementation, running on virtual machine Dalvik, so using the Android SDK to create an application requires the use of the Java language to write the implementation. However, it is not limited to the use of java,google at the beginning of the release of Android to support the JNI programming method, that is, third-party applications can fully invoke their own C-dynamic library through JNI, that is, on the Android platform, "java+c" programming is always possible. The NDK is designed to make it easy for you to invoke a collection of tools from your local C + + when creating an app.
The NDK is a toolset this allows you to implement parts of the your app using Native-code languages such as C and C + +. For certain types of apps, this can is helpful so you can reuse existing code libraries written in these languages, but MO St Apps do not need the Android NDK.

Official NDK Document: http://wear.techbrood.com/tools/sdk/ndk/

When do I use the NDK?

Before downloading the NDK, you should understand the NDK won't benefit most apps. As a developer, you need to balance it benefits against its drawbacks. Notably, using native code on Android generally does does result in a noticable performance improvement, but it always incr Eases your app complexity. In general, you should only use the NDK if it's essential to your app-never because you simply prefer .
The official clearly describes that most applications do not gain significant performance gains from using the NDK, but may lead to increased application complexity and poor compatibility. Google recommends using the NDK when dealing with work that requires a lot of computing/high CPU consumption, such as game engines, signal processing, image processing, and more.

The actual use of the NDK often takes into account:
1. Protection of the code, because the Java layer Code of the APK is easy to decompile, and the C + + library is difficult to reverse the sink.
2. In order to conveniently use the existing open source libraries, most of the existing open source libraries are written in C/s code.
3. Cross-platform considerations, the library can be used to write in C + + easily on other embedded platforms.

Using the NDK example on as

With the NDK, of course, download the latest NDK tool first: http://developer.android.com/ndk/downloads/index.html

To configure the NDK path for the project:

To see if Local.properties has a good NDK path defined:

    • Project Gradle NDK configuration
      Change project's gradle.properties file to include android.usedeprecatedndk=true to enable the NDK:

To modify the defaultconfig of Build.gradle under the app, configure native/C + + to compile the local so library:

Declare the JNI function to write the native function in Java code that requires the implementation of JNI:

package com.example.jokerlee.myapplication;/** * Created by jokerlee on 16-3-31. */publicclass JniTest {    //使用jni需要加载本地编译出来的so库,名称即为上一步配置的ndk moduleName    static {        System.loadLibrary("jniTest");    }    publicstatic  nativegetStringFromJni();}

After Rebuild project, the NDK tool generates a classses folder in the app's Build/intermediates directory:

Open as terminal execute using the Javah command to convert the Jni method of the Java class declaration into a c\c++ header file
For more information about the Javah command, refer to: http://blog.csdn.net/zzhays/article/details/10514767

Generate Com_example_jokerlee_myapplication_jnitest.h in the current debug directory:

/* Don't EDIT this file-it are machine generated * /#include <jni.h>/ * Header for class com_example_jokerlee_myapplication_jnitest * /#ifndef _included_com_example_jokerlee_myapplication_jnitest#define _included_com_example_jokerlee_myapplication_jnitest#ifdef __cplusplusextern"C"{#endif/* * class:com_example_jokerlee_myapplication_jnitest * METHOD:GETSTRINGFROMJNI * Signature: () ljava/lang/str ing */Jniexport jstringJnicallJava_com_example_jokerlee_myapplication_jnitest_getstringfromjni (jnienv *, jclass);#ifdef __cplusplus}#endif#endif

Then you create a C/cpp file based on the header file TestJni.cpp the function declarations in the header file, and move the header files and c/cpp files to the MAIN/SRC directory:

//// Created by jokerlee on 16-3-31.//"com_example_jokerlee_myapplication_JniTest.h"JNIEXPORT jstring JNICALL Java_com_example_jokerlee_myapplication_JniTest_getStringFromJni  (JNIEnv * env, jclass ){    return env->NewStringUTF("这里是来自c的string");  }

Using the JNI function

After completing the various configurations and the writing of C + + files, if you need to implement the JNI call native function within the application, you need to first load the so library native code compiled:

//jnitest for the name of the library, which is configured in the app build .gradle  defaultconfig Modulenamesystem "jnitest" )   
package com.example.jokerlee.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("test", JniTest.getStringFromJni() ); }}

Android Studio NDK Foundation use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.