Android Studio uses the new Gradle build tool to configure the NDK environment and gradlendk

Source: Internet
Author: User

Android Studio uses the new Gradle build tool to configure the NDK environment and gradlendk

Android Studio 1.3 Beta1 supports NDK. However, it uses a brand new and experimental gradle build tool. Official Address: http://tools.android.com/tech-docs/new-build-system/gradle-experimental.

Before configuration, update Android Studio to the latest version. The latest version is 1.4 Bata4.

Use the Android Studio built-in SDK manager to download the NDK.

You can also right-click the Project to open the Project Structure and switch to the SDK Location page for installation.

Note that after this NDK is installed, its directory is under the ndk-bundle directory under the SDK Directory, which is different from the previous one.

For the file to be modified, see

First, you must change the gradle in the root directory to an experimental gradle.

classpath 'com.android.tools.build:gradle-experimental:0.2.0'

In the end

And the support for gradle 2.5 is required to change the version in gradle/gradle-wrapper.properties to 2.5

distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip

In the end

Then modify the original app/build. gradle. Note that all configurations now have equal signs and use com. android. model. application is no longer the original com. android. application. If it is a Library, it is com. android. model. library. Then the android peripheral contains a layer of model. Previously, buildTypes in android was moved out. Wait. Check the following content and pay attention to the changes.

Apply plugin: 'com. android. model. application 'model {android {compileSdkVersion = 22 buildToolsVersion = "23.0.1" defaultConfig. with {applicationId = "com. k201788.car. jnidemo "minSdkVersion. apiLevel = 14 targetSdkVersion. apiLevel = 22 versionCode = 1 versionName = "1.0"} tasks. withType (JavaCompile) {// specify the JDK version sourceCompatibility = JavaVersion. version_00007 targetCompatibility = JavaVersion. version_00007} android. ndk {moduleName = "test" ldLibs + = "log" abiFilters + = "armeabi" abiFilters + = "armeabi-v7a" abiFilters + = "x86"} android. buildTypes {release {minifyEnabled = false proguardFiles + = file ('proguard-rules. pro') }}dependencies {compile fileTree (dir: 'libs', include :['*. jar ']) compile 'com. android. support: appcompat-v7: 22.2.1 '}

Configure the ndk in android. ndk. The module name is required. Then I used log and supported three platforms, armeabi, armeabi-v7a, and x86.

Add the jni directory and right-click the main to create the jni directory.

In the pop-up window, you can modify the jni Directory, which is in main/jni by default.

Create a Test class, load the jni library, and declare the native method.

public class Test {    static {        System.loadLibrary("test");    }    public native void test();}

At this time, you will find that the test method is red.

How to generate c layer by pressing alt + enter

Modify the code below

#include <jni.h>#include "android/log.h"JNIEXPORT void JNICALLJava_com_kltz88_car_jnidemo_Test_test(JNIEnv *env, jobject instance) {    __android_log_write(ANDROID_LOG_ERROR,"TAG","TEST");}

To generate a header file, use the command line interface.

The corresponding header file will be generated in the jni directory.

Then, at the java layer, call

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Test t=new Test();        t.test();    }}

The log output is displayed.

Note: If the corresponding java version is not specified in gradle, an error may be reported during runtime.

Tasks. withType (JavaCompile) {// specify the JDK version sourceCompatibility = JavaVersion. VERSION_1_7 targetCompatibility = JavaVersion. VERSION_1_7}

The error message is as follows. When this error occurs, add the above Code.

Finally, the debugging problem occurs. In theory, debugging is supported, but I did not test it successfully on windows. The steps are shown below.

Add native Running Method

Change

Next, the breakpoint can be debugged theoretically. However, I encountered the following problems and failed to continue. I also checked the problem and couldn't find the problem. That's all, try again in linux another day.

You will find that the original Android. mk and Application. mk are no longer needed, and all configurations can be configured in gradle.

Official github sample code
Https://github.com/googlesamples/android-ndk

My sample code
Http://download.csdn.net/detail/sbsujjbcy/9108775

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.