Basic use of Android Studio NDK

Source: Internet
Author: User

Basic use of Android Studio NDK
What is NDK?

The Android platform is implemented based on java and runs on the Virtual Machine Dalvik. Therefore, to create an application using the Android SDK, you must use the java language for programming. However, it is not limited to the use of java. google claimed to support JNI programming at the beginning of android release, that is, third-party applications can call their own C dynamic libraries through JNI, that is, on the Android platform, the "Java + C" programming method can always be implemented. NDK is a collection of tools that call local C/C ++ when creating an application.
The NDK is a toolset that allows you to implement parts of your app using native-code syntax ages such as C and C ++. for certain types of apps, this can be helpful so you can reuse existing code libraries written in these ages, but most apps do not need the Android NDK.

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

When to use NDK?

Before downloading the NDK, you shoshould understand that the NDK will not benefit most apps. as a developer, you need to balance its benefits against its drawbacks. notably, using native code on Android generally does not result in a noticable performance improvement, but it always increases your app complexity. in general, you shoshould only use the NDK if it is essential to your app-never because you simply prefer to program in C/C ++.
Officially, it clearly states that most applications will not achieve significant performance gains due to the use of NDK, but may lead to an increase in application complexity and poor compatibility. Google recommends that you use NDK to improve computing efficiency when processing tasks that require a large amount of computing/high CPU consumption, such as game engines, signal processing, and image processing.

The actual use of NDK often takes into account the following:
1. Code protection, because the java-Layer Code of apk is easily decompiled, it is difficult to reverse the C/C ++ library.
2. To facilitate the use of existing open-source libraries, most of the existing open-source libraries are written in C/C ++ code.
3. For cross-platform consideration, you can use the C/C ++ library to conveniently use it on other embedded platforms.

Examples of using NDK on

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

The NDK path of the configuration item target:

Check whether the NDK path is defined in local. properties:

Project Gradle NDK Configuration
Change the gradle. properties file of the project and add android. useDeprecatedNdk = true to make the project support NDK:

Modify the defaultConfig of build. gradle in the app and configure native c/c ++ to compile the local so Library:

Declare the JNI function and write the native function that needs to implement JNI in java code:

Package com. example. jokerlee. myapplication;/*** Created by jokerlee on 16-3-31. */public class JniTest {// use jni to load the locally compiled so library. The name is the ndk moduleName static {System. loadLibrary ("jniTest");} public static native String getStringFromJni ();}

After the Rebuild Project, the NDK tool will generate a classses folder in the build/intermediates directory of the app:

Run the AS terminal command to convert the JNI method declared by java class to the C \ C ++ header file.
For more information about javah commands can refer to: http://blog.csdn.net/zzhays/article/details/10514767

Generate com_example_jokerlee_myapplication_JniTest.h in the current debug directory: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> /* DO NOT EDIT THIS FILE - it is machine generated */#include /* 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/String;*/JNIEXPORT jstring JNICALL Java_com_example_jokerlee_myapplication_JniTest_getStringFromJni(JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif

Then, create the c/cpp file TestJni. cpp Based on the header file to implement the function declaration in the header file, and move these header files and the c/cpp file to the main/src directory:

/// Created by jokerlee on 16-3-31. // # include "com_example_jokerlee_myapplication_JniTest.h" JNIEXPORT jstring JNICALL encode (JNIEnv * env, jclass) {return env-> NewStringUTF ("here is the string from c ");}

Use JNI Functions

After completing various configurations and compiling the c/c ++ file, you must first load the so library compiled by native code to call the native function in the application if you need to implement JNI:

// JniTest is the library name, that is, moduleNameSystem. loadLibrary ("jniTest") configured in app build. gradle defaultConfig ");
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() );    }}

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.