JNI Cross compilation first output HelloWorld (Android Studio Edition)

Source: Internet
Author: User

I. Introduction to NDK1.JNI

1.JNI is the abbreviation for Java Native Interface, the Java local interface. It provides a number of APIs that enable the communication of Java and other languages (mainly C + +). Starting with Java1.1, the JNI standard becomes part of the Java platform, which allows Java code to interact with code written in other languages.
2.ndk:android NDK is in front of the SDK and added the word "native", that is, native development Kit, so Google is called the "NDK."
3..so: Shared function library, loaded when the executable program starts, all programs can automatically load functions in the shared function library when they rerun.

4. Why should I use the NDK?

     1. 代码的保护,由于apk的java层代码很容易被反编译,而C/C++库反汇难度较大。     2. 在NDK中调用第三方C/C++库,因为大部分的开源库都是用C/C++代码编写的。     3. 便于移植,用C/C++写的库可以方便在其他的嵌入式平台上再次使用。
Configuring the NDK environment under Android Studio

1. Download the NDK package first
2. Go to the top right corner of the menu File->project Structure: Enter the following screen, at the bottom of the Android NDK location select the local ndk directory click OK


3. After creating the project, select File->new->floder->jni Folder:
A JNI folder will appear in the Java directory
4. Configure the Build.gradle of the local project, select the Build.gradle under Module, and add the following:

Apply plugin:' Com.android.application 'Android {Compilesdkversion atBuildtoolsversion"23.0.3"Defaultconfig {ApplicationID"Com.example.lancao.myhellojni"Minsdkversion theTargetsdkversion atVersioncode1Versionname"1.0"        //Add a configuration herendk{ModuleName"Lancao"Ldlibs"Log","Z","M"Abifilters"Armeabi","armeabi-v7a","x86"}} buildtypes {release {minifyenabledfalseProguardfiles Getdefaultproguardfile (' Proguard-android.txt '),' Proguard-rules.pro '}}}dependencies {Compile Filetree (dir:' Libs ', include: [' *.jar ']) Testcompile' junit:junit:4.12 'Compile' com.android.support:appcompat-v7:23.3.0 '}

5. Static load dynamic library, writing naive method, and ordinary Java method basically no difference.

 PackageCom.example.lancao.myhellojni;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public  class mainactivity extends appcompatactivity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        TextView TextView = (TextView) Findviewbyid (R.id.text);    Textview.settext (Getstringfromnative ()); }//static load dynamic library    Static{System.loadlibrary ("Lancao"); }//Affirm local method     Public   nativeStringgetstringfromnative();}

6. Generate the header file. In the command line interface of Android Studio, go to the/app/src/main/java directory and execute the command:

javah -d  com.example.lancao.myhellojni.MainActivity

This adds the JNI directory and the Com_example_lancao_myhellojni_mainactivity.h header file to the src/main/directory.

The specific content of 7.com_example_lancao_myhellojni_mainactivity.h is

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_example_lancao_myhellojni_MainActivity */#ifndef _Included_com_example_lancao_myhellojni_MainActivity#define _Included_com_example_lancao_myhellojni_MainActivity#ifdef __cplusplus"C" {#endif#ifdef __cplusplus}#endif#endif

8. Create a new C local file

//导入本地的库文件,必须添加#include <android/log.h>#include <jni.h>JNIEXPORT jstring JNICALL Java_com_example_lancao_myhellojni_MainActivity_getStringFromNative(JNIEnv * env , jobject j){//返回一个字符串“hello from jni !”    return (*env)->NewStringUTF(env,"Hello From JNI!");}

9. This completes the cross-compilation output HelloWorld

JNI Cross compilation first output HelloWorld (Android Studio Edition)

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.