Learning Journey Based on Android NDK ----- jni log Printing

Source: Internet
Author: User

Learning Journey Based on Android NDK ----- jni log Printing
All programs are called.
The following describes how to use the log printing method on the JNI layer, similar to the log printing method provided by the Android sdk.
1. Android Application Layer MainActivity. java
Main Function Code
A) load the So library statically
B) Declare the local method
C) call the local method
The code is annotated as follows:
Public class MainActivity extends Activity {
// LOCAL_MODULE: = NDK_02 in your mk configuration file
Private static final String libSoName = "NDK_02 ";
Private Context mContext = null;
Private Button btnClick = null;
Private String mStrMSG = null;
Private EditText etContext = null;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MContext = this;
EtContext = (EditText) findViewById (R. id. et_content );
BtnClick = (Button) findViewById (R. id. btn_click );
BtnClick. setOnClickListener (new OnClickListener (){

Public void onClick (View v ){
MStrMSG = setParamToJNI (getContent ());
If (mStrMSG = null ){
MStrMSG = "JNI call failed ";
}
LogUtils. toastMessage (mContext, mStrMSG );
}
});

}

/**
* Get the content of the input box
* @ Return indicates the content of the input box.
*/
Private String getContent (){
String str = etContext. getText (). toString ();
Return str. trim (). length ()> 0? Str: "default value ";
}

/**
* This method is native.
*
* Implemented by C
*
* @ Return
*/
Public native String setParamToJNI (String msg );

/**
* 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_LDLIBS + =-L $ (SYSROOT)/usr/lib-llog
LOCAL_MODULE: = NDK_02
LOCAL_SRC_FILES: = \
PrintLog. c
Include $ (BUILD_SHARED_LIBRARY)
 
To print logs, you must add
LOCAL_LDLIBS + =-L $ (SYSROOT)/usr/lib-llog
 
 
3. Implementation of the JNI-layer PrintLog. c file
# Include <string. h>
# Include <android/log. h>
# Include <jni. h>
 
Jstring Java_com_duicky_MainActivity_setParamToJNI (JNIEnv * env, jobject thiz,
Jstring msg)
{
 
If (msg = NULL ){
// This method is the printing method.
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "Your params is null ");
Return (* env)-> NewStringUTF (env, "Your params is null ");
}
 
Char data [128];
Memset (data, 0, sizeof (data ));
Char * c_msg = NULL;
C_msg = (* env)-> GetStringUTFChars (env, msg, 0 );
// This method is the printing method.
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "Get Param: % s From Java", c_msg );
 
Return (* env)-> NewStringUTF (env, "success ");
}
I. header files must be referenced # include <android/log. h>
Ii, _ android_log_print (ANDROID_LOG_INFO, "JNIMsg", "Log Content ");
JNIMsg filters the tags you want to output
Log Content indicates the information you want to output. The usage is the same as that of C's Printf.
 
4. Run the program
Input content

Click to send

 
View console printing information


 
 
 
 
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,
 
Click to download the source code NDK to print the Log example
 
This article is from the "Duicky" blog

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.