Exploring the NDK for Android Studio

Source: Internet
Author: User

Implementing NDK Development in Android Studio is a lot easier than the previous eclipse+cygwin.

This article takes the simplest of strings from a C program and shows the basic flow of NDK development as an example on the TextView of Mainactivity.

Get ready

NDK Development environment

On the installed Android studio, download and install the NDK in SDK Manager or go directly to https://developer.android.com/ndk/downloads/ index.html download the corresponding installation package and unzip it to the ndk-bundle of the SDK.

Setting the relevant environment variables, it is recommended to add tools such as ANDROID-V7, V4 and so on to the environment variables.

Implementation steps

Create Project

Create a directory named JNI under the app (click: New Folder Jni-folder)

Added in gradle_properties file: android.usedeprecatedndk=true

Create a class Ndkutil, in which you declare the native method

      public class Ndkuitls {
Public native String getclanguagestring ();
}

 

Use the command line in terminal, under App\build\intermediates\classes\debug, use Javah-jni lab.example.ndkutil[according to the actual situation]. A. h file is generated next to the directory, and after the file is opened, the contents of it are copied.

  

  

Create a new JNI directory (new folder Jni-folder) in the app directory and a C file to paste the contents of. H. and make the corresponding changes.

In the Build.gradle of the App module directory, locate the defaultconfig of the Gradle file and add the NDK configuration information
ndk{
ModuleName "Yourjnilibname"
Abifilters "Armeabi", "armeabi-v7a", "x86"
}

  

In code ndkutil, load to the class library Yournilibname:
static{
System.loadlibrary ("Yourjnilibname");
}

Compile, run:

Exploring the NDK for Android Studio

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.