NDK Development using the Android Studio 1.3 release

Source: Internet
Author: User

It is well known that the Android Studio 1.3 version of the new NDK support, it is easy to develop C + + or C code on the above, but the preview version of the function is not very perfect, need a lot of steps, there are some strange bug, the whole day can finally use Android Studio is perfect for NDK development. Look First:

Method: First update Android studio to version 1.3:

1. Check the Android Studio update.

Some people may encounter problems: their version is less than 1.3 check the update but prompt is the latest version?
That's because the update channel is not a beta channel, you need to set up the channel as a beta channel before you can update to 1.3:


Then you can update to 1.3 by checking for updates again.

Two. Switch the Gradle to the experimental version.

Modify the Gradle file in accordance with Google's official method. (The official version of the test is not perfect, you need to manually modify the Gradle file to be compatible with Gradle experimental version, they will be in release version of the program to implement automatic modification Gradle)
Google original link: http://tools.android.com/tech-docs/new-build-system/gradle-experimental
1. Update the Gradle to version 2.5.

Changing the gradleversion to 2.5 Click OK here will start to update the gradle automatically.
2.Android ndk r10e, update ndk to this version
3.BuildTools version requires more than 19.0.0

4. The 3 files marked in red are files that need to be manually modified.

5. Modify the contents of the Gradle-wrapper.properties file as follows:

distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdistributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip

6. Modify the Gradle file under Project (non-module) to change the Gradle version to the experimental version (the Red Callout section):
(Classpath ' com.android.tools.build:gradle-experimental:0.1.0 ')

7. Modify the Gradle file under module (the new version gradle syntax has changed a lot, red is labeled as the part that needs to be modified, note dependencies outside the model):
Outside of android{} to include with model{}, all assignments are changed to =, additions to +=,buildtypes{},productflavors{} and sources{} are moved outside of android{}.

One more copy of my module's configuration:

Apply plugin:' Com.android.model.application 'model{Android {compilesdkversion= +buildtoolsversion="22.0.0"Defaultconfig. with{ApplicationID ="Com.example.stronghope.jnitest"Minsdkversion.apilevel =9Targetsdkversion.apilevel = +Versioncode =1Versionname ="1.0"} tasks.withtype (Javacompile) {//Specify compile JDK versionsourcecompatibility = javaversion.version_1_7 targetcompatibility = javaversion.version_1_7}} an DROID.NDK {modulename="Jnitest"Ldlibs + ="Log"Ldlibs + ="Z"Ldlibs + ="M"Abifilters + ="Armeabi"Abifilters + ="armeabi-v7a"Abifilters + ="x86"} android.buildtypes {release {//isminifyenabled = False//Proguardfiles + = file (' Proguard-rules.pro ')}}}dependencies {}

Experimental version gradle may have a bug on some of the machines, if the compile time reported the following error, you need to add Tasks.withtype in android{} to specify a lower JDK compiled version (1.6 or 1.7):
Bad class file Magic (Cafebabe) or version
Finally, specifying the NDK directory in the Local.properties file will make the NDK development enjoyable:
Ndk.dir= "Your NDK directory"

Three. NDK Development

1. Configure the NDK compilation parameters in Gradle (only need to be configured in Gradle, you can completely separate from writing android.mk and application.mk files to compile):
The Ldlibs item is equivalent to the Local_ldlibs parameter in Android.mk.
The abifilters corresponds to the App_abi parameter in the APPLICATION.MK.

2. Create a new JNI folder under the main folder (default directory, you can specify Jni.srcdirs in Gradle if you want to modify)

3. Define the JNI Java class:

publicclass JNI {    static{        System.loadLibrary("JniTest");    }    publicstaticvoidtestLog();}

4. Use the Javah command to generate the header file (do not generate header files can also, when there is no method implementation in C + +, the method name is red, directly on the method Alt+enter select Create XXXX can automatically generate methods in C + + files, very convenient AH):
Open CMD,CD to the Java directory, using the command:
Javah Com.example.stronghope.jnitest.JNI
Generate the header file and cut the header file into the JNI directory.
Automatic Generation Method:

5. Create a new C + + file under the JNI directory:
JniTest.cpp

6. Introduce the header file and write the C + + code:

7. Run the Android project and get the following output from the console:

Okay, a simple JNI program is implemented, and using Android Studio for NDK development is really handy! Wait for release version out O (∩_∩) o

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

NDK Development using the Android Studio 1.3 version

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.