Android Studio Ndk Programming

Source: Internet
Author: User

Android Studio Ndk Programming

Now the Android program has basically been developed from Eclipse to Android Studio. Recently, the project needs to use ndk programming, so I have a hard time.

Development Environment Android Studio 1.5.1 Android-ndk-r10e Gradle 2.8

First, create a project named NdkDemo. the directory structure of the created project is as follows:

Then open the gradle. properties file and write a sentence at the end of the file. If this sentence is not written, the compilation will fail.

android.useDeprecatedNdk=true

Configure the ndk path, click File in the upper left corner, select Project Structure (or directly click the button marked in the red box) to enter the configuration page, and configure the downloaded ndk path in the red box.

At this point, the configuration is complete, and development-related work is carried out below.
I believe that anyone who has programmed ndk in Eclipse knows that we are going to compile Android. mk, but Android Studio does not need to be written at all. We only need to write a few simple statements under gradle, and Studio will automatically help us write Android. mk, very convenient.

DefaultConfig {... ndk {moduleName "ndk-demo" // name of the referenced module ldLibs "log" // name of the referenced Library (optional) abiFilters "armeabi" // configure the so libraries of those versions to be compiled (if not written, the so libraries of all versions will be compiled )}}

The above is the configuration of build. gradle in the app directory. You only need to write the ndk-related configuration under the defaultConfig node.

Next, we create a Comman class and write a local function. The System. loadLibrary parameter is the moduleName configured in build. gradle.

public class Command {    public static native String getStringFromC();    static{        System.loadLibrary("ndk-demo");    }}

Right-click the app directory and choose create a jni directory.

Click the console Tab at the bottom and enter the command to automatically generate related. H files under the jni directory.

Explain the meaning of the two command lines. Enter the app/src/main/java directory. In the second sentence, javah is a jdk tool for generating header files.-d .. /jni indicates that the generated file should be placed under the jni folder in the previous directory, com. zwf. ndkdemo. ndk. command is the full path of the Command we just wrote.

With the header file, we can start to write the relevant c files. In the jni folder, create a c file with the same name as the header file, and write the relevant functions.

Click Run!

Where can we find the so library we generated.

Finished! Demo source code

Related Article

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.