Android NDK development basics and androidndk Development

Source: Internet
Author: User

Android NDK development basics and androidndk Development
Native Development Kit download NDK Development Kit

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

Select NDK r10e
Android-ndk-r10e-windows-x86_64.exe
The size of the Self-extracted file is 400 mb, and 3 GB + after decompression.

Configure NDK Environment Variables


Add the ndk directory to the path of the system environment variable, and then enter ndk-build verification in the newly opened cmd:

HelloJni

Android Native Development Tools are installed during the installation of ADT in Eclipse,
Import the example located in android-ndk-r10e \ samples \ hello-jni to Eclipse:

Initial directory structure:

Open cmd and switch to the HelloJni project directory:
Run ndk-build to compile:

Go back to Eclipse and refresh the HelloJni directory. Find that the obj folder is added, which contains the. so library files corresponding to each architecture, such:

Then run the project:

Note that many architectures are compiled when ndk-build is executed, which takes a long time. If you only want to compile the arm we need, you can compile the Application under the jni directory of the project. the following content of the mk file is commented out:

#APP_ABI := all

Clear the previous compilation:

ndk-build clean

The next ndk-build will be completed soon:

The first NDK Project

1. Create an Android Project
Create an native method in MainActivity:

    public static native String getStringFromC();

2. Create a jni directory
3. Compile the native method on the java Layer
4. Generate the JNI header file

  • Javah


Run cmd to switch to the project directory, and then execute:
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. You can also add android. jar to the environment variable: D: \ Android \ sdk \ platforms \ android-22 \ android. jar.

The. h header file is generated under the jni directory:

The format is as follows:

Then, create our own c file in 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 (*env)->NewStringUTF(env, "Hello from JNI !");}

Then, create the Android. mk file in the jni directory:
Android. mk

LOCAL_PATH: = $ (call my-dir) include $ (CLEAR_VARS) LOCAL_MODULE: = HelloNdkLOCAL_SRC_FILES: = hello. c # which c file to compile include $ (BUILD_SHARED_LIBRARY)

Then go to cmd and switch to the project directory for compilation:

Note: Add loadLibrary to MainActivity. The Module name is enclosed in quotation marks. The Code declared in the static area will be executed before the onCreate method:

package com.zms.hellondk;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;public class MainActivity extends Activity {    public static native String getStringFromC();    @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 occurs:

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

Concepts related to NDK
  • Application scenarios of NDK
    • Code Protection (Java code is easily decompiled, C/C ++ Library)
    • Code reusability: most open-source libraries are written in C/C ++ (such as OpenCV and FFmpeg)
    • Ease of transplantation (re-use on other embedded platforms, such as iOS)
  • Cross-Compilation

    Compile the code that can be executed on another platform using tools.

  • JNI (Java Native Interface)

    Is part of the Java platform standard, allowing Java code to interact with other language code

  • Link Library (static and dynamic)

    • Static Link Library
    • Dynamic Link Library
  • Mk configuration file (make) Parameters
  • Ndk Development Kit directory

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.