How to use the Android NDK gracefully

Source: Internet
Author: User

Using the NDK for some time on Android studio, feeling that the official plugin com.android.tools.build.gradle-experimental is still not stable enough, there are some problems, but Google also stated that the plugin was in the experimental phase. Fortunately, the official has provided another way to use the NDK on Android studio, and this blog is about the way I feel so good about it so far.

Development environment
Basic Android Development environment: Android Studio, SDK, JDK
Download the NDK, download it yourself manually or download it using the SDK tool.
I'm using the version: Android Studio 1.5.1

Configure common commands
NDK development more commonly used command is to generate header files, build so package, in Android Studio we can first configure the command, it is very convenient to use, this is the best place I feel.

As shown, I added three commands and added by the plus sign

Ndk

The configuration details for one of these commands are as follows:

Javah

Command Configuration parameters:

Javah for generating header files
Program: $JDKPath $/bin/javah
Parameters:-encoding UTF-8-D. /jni-jni $FileClass $
Note: This command I added the-encoding UTF-8 The specified code, you can change to the code of your project.
Working directory: $SourcepathEntry $.. \java

Ndk-build for building so packages
Program: Your NDK directory \build\ndk-build.cmd
Note: Windows uses Ndk-build.cmd,mac/linux with Ndk-build
Parameters: No need to fill in anything
Working directory: $ModuleFileDir $\src\main

Ndk-build Clean Clear So pack
Program: Your NDK directory \build\ndk-build.cmd
Note: Windows uses Ndk-build.cmd,mac/linux with Ndk-build
Parameters:clean
Working directory: $ModuleFileDir $\src\main

After configuring three commands, you can find these commands in the right-click menu, which you can use directly. As shown in the header file that generated the Ndktest class

Javah

Configuration Engineering
Making the project use the NDK requires some configuration work
Fill in the NDK directory used in the Local.properties file:

1
Ndk.dir= the absolute path to your NDK directory
Add the following code to the Gradle.properties file

1
ANDROID.USEDEPRECATEDNDK = True
Add the following code to the module's Build.gradle:

Android {

defaultConfig {    ndk {        moduleName "NdkTest"//定义NDKlibrary的名字        //ldLibs "log" 添加log库,看自己需求    }}//这里设置目录,默认就这样写就可以了sourceSets {    main {        jni.srcDir "src/main/jni"        jniLibs.srcDir "src/main/libs"    }}

}
Hello Word
You can enjoy the NDK on Android studio after you've configured all of the steps above.
Write a simple program that returns a string of Hello Word.

First, declare a local method in Java and load the local library as follows:

public class Ndktest
{
static {
System.loadlibrary ("Ndktest");
}
public static native String getString ();
}
Generate its header file (the automatically generated name is the package name + class name Me_majiajie_ndktest_ndktest.h), and create a C + + file (NdkTest.cpp), as follows:

#include "Me_majiajie_ndktest_ndktest.h"

Jniexport jstring Jnicall java_me_majiajie_ndktest_ndktest_getstring
(JNIEnv *env, Jclass JC)
{
Return Env->newstringutf ("Hello word!!!");
}
Create a file in the JNI directory named Android.mk, fill in the following content

Local_path: = $ (call My-dir)
Include $ (clear_vars)

Local_module: = Ndktest
Local_src_files: = NdkTest.cpp
Include $ (build_shared_library)
Then create a file in the JNI directory named Application.mk, fill in the following content

App_modules: = Ndktest

App_abi: = All
Note: Android.mk and application.mk are the NDK default profiles, and a detailed description of these two files can be crossing Web.

Finally, the so package is generated, so remember that every time you modify the C file, you need to regenerate the so package:

How to use the Android NDK gracefully

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.