Android NDK Development Basics

Source: Internet
Author: User

Native Development Kit Download NDK Development Kit

Links: http://developer.android.com/ndk/downloads/index.html

Select Ndk r10e
Android-ndk-r10e-windows-x86_64.exe
Self-extracting file size 400M, after decompression 3g+.

Configuring NDK Environment variables


Add the NDK directory to the System environment variable path, and then verify the Ndk-build in the newly opened CMD input:

Hellojni

When installing ADT, Eclipse installs Android Native development Tools,
Import the example in Android-ndk-r10e\samples\hello-jni to eclipse:

The initial directory structure:

Then open cmd and switch to the HELLOJNI project directory:
Execute ndk-build to compile:

When finished, go back to eclipse to refresh the Hellojni directory and find that the obj folder is added with the. So library files for each schema, such as:

Then run the project:

Note that when you execute ndk-build above, you compile a lot of schemas, the time will be longer, if you just want to compile the arm we need, you can comment the following contents of the Application.mk file under the project JNI directory:

#APP_ABI := all

Clear the last compilation:

ndk-build clean

Again Ndk-build, soon completed:

The first NDK project

1. New Android Project
Create a native method in Mainactivity:

    publicstaticnativegetStringFromC();

2. Create a JNI directory
3. Writing Java Layer native methods
4. Generating JNI header files

  • Javah


CMD switches to the project directory and then executes:
Javah-classpath bin/classes-d JNI com.zms.hellondk.MainActivity


Javah-classpath bin/classes;d:\android\sdk\platforms\android-22\android.jar-d JNI com.zms.hellondk.MainActivity
This is troublesome, or you can: add Android.jar to environment variable: D:\Android\sdk\platforms\android-22\android.jar

You can see that the. h header file is generated under the JNI directory:

The format is as follows:

Then create our own C files under the JNI directory:
hello.c

#include<stdio.h>#include<stdlib.h>#include"com_zms_hellondk_MainActivity.h"JNIEXPORT jstring JNICALL Java_com_zms_hellondk_MainActivity_getStringFromC(        JNIEnv env, jclass jclass) {    return"Hello from JNI !");}

Then create the Android.mk file under the JNI directory:
Android.mk

LOCAL_PATH:$(call my-dir)include$(CLEAR_VARS)LOCAL_MODULE    :HelloNdkLOCAL_SRC_FILES:#对哪个c文件进行编译include$(BUILD_SHARED_LIBRARY)

Then go to cmd to switch to the project directory to compile:

Note that the mainactivity is written with LoadLibrary, the module name in the quotation marks, and the code for the static zone declaration is executed before the OnCreate method:

 PackageCOM.ZMS.HELLONDK;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.TextView; Public  class mainactivity extends Activity {     Public Static nativeStringGETSTRINGFROMC();@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        TextView Textfromjni = (TextView) Findviewbyid (R.ID.TEXTFROMJNI);    Textfromjni.settext (GETSTRINGFROMC ()); }Static{System.loadlibrary ("HELLONDK"); }}

Otherwise, the following error will occur:

(--Don't ask me how I know)

NDK Related Concepts
    • The NDK application Scenario
      • Code Protection (Java code is easy to decompile, C + + library)
      • Code reusability: Most open-source libraries are written in C + + (e.g. opencv,ffmpeg, etc.)
      • Portable (reusable on other embedded platforms, such as iOS)
    • Cross-compiling

      Use the tool to build another platform executable code in one platform compilation.

    • JNI (Java Native Interface)

      is part of the Java platform standard that allows Java code to interact with other language code

    • link library (static, dynamic)

      • Static link library
      • Dynamic Link Library
    • MK configuration file (make) parameters
    • NDK Development Kit Catalog

Android NDK Development Basics

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.