Learning Journey Based on Android NDK ----- HelloWorld

Source: Internet
Author: User

Learning Journey Based on Android NDK ----- HelloWorld
Hello World, as the initial stage of all programming languages, occupies an unchangeable position. All Chinese/English/French/German/American ...... In the version programming textbooks, hello world is always recorded in books as the first TEST. This is the first step in all programming! Classic in classic! Hello world!
The following describes the Hello World developed by NDK.
1. Android Application Layer MainActivity. java
Main Function Code
A) load the So library statically
Static {
System. loadLibrary (libSoName );
}
 
B) Declare the local method
Public native String getStringFromJNI ();
 
C) call the local method
String mStrMSG = getStringFromJNI ();
 
 
The code is annotated as follows:
Public class MainActivity extends Activity {
// LOCAL_MODULE: = NDK_01 in your mk configuration file
Private static final String libSoName = "NDK_01 ";

Private Context mContext = null;
Private Button btnClick = null;
Private String mStrMSG = null;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MContext = this;
BtnClick = (Button) findViewById (R. id. btn_click );
BtnClick. setOnClickListener (new OnClickListener (){

Public void onClick (View v ){

// Call the JNI Implementation Method
MStrMSG = getStringFromJNI ();
If (mStrMSG = null ){
MStrMSG = "JNI call failed ";
}
LogUtils. toastMessage (mContext, mStrMSG );
}
});

}

/**
* Declare local methods
*
* This method is implemented by C as the native method.
*
* @ Return the information given by JNI
*/
Public native String getStringFromJNI ();

/**
* Load the so library file generated by JNI
*/
Static {
System. loadLibrary (libSoName );
}

}
 
2. Configure the Android. mk File
I have introduced related content before. If the configuration is unclear, read the introduction to the Android. mk file.
 
LOCAL_PATH: = $ (call my-dir)
 
Include $ (CLEAR_VARS)
 
LOCAL_C_INCLUDES: = $ (LOCAL_PATH)/include
 
LOCAL_MODULE: = NDK_01
LOCAL_SRC_FILES: = \
HelloWorld. c
 
Include $ (BUILD_SHARED_LIBRARY)
 
3. JNI-layer HelloWorld. c file
# Include <string. h>
# Include <jni. h>
 
Jstring
Java_com_duicky_MainActivity_getStringFromJNI (JNIEnv * env, jobject thiz)
{
// Return a string to the Java Layer
Return (* env)-> NewStringUTF (env, "HelloWorld from JNI! ");
}

4. Run the program
Click the button to call the method declared as native to display the information obtained from the JNI layer.
 
As follows:

 
 
5. Notes

D) The keyword native must be added to declare a local method.
For example, public native String getStringFromJNI ();
E) C naming rules for local laws in the file
For example, jstring Java_com_duicky_MainActivity_getStringFromJNI (JNIEnv * env, jobject thiz)
Jstring is the return value, which can also be void, jint, and so on.
Java_com_duicky_MainActivity_getStringFromJNI is the method name, which is separated by an underscore (_). Java is the default method, com_duicky is the package name, MainActivity is the class name, And getStringFromJNI is
 
 
If you have any questions, please leave a message. Your personal skills are limited. If you have any mistakes, please point out that you are not comprehensive enough. Thank you,
 
 
This article is from the "Duicky" blog

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.