Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of Android Jni Code

2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you to the example analysis of Android Jni code, the content is very detailed, interested friends can refer to, hope to be helpful to you.

In the Android simulator, the Android mobile phone operating system can be modified and various functions can be added to meet the needs of users. So today we will bring you the relevant use of Android Jni, in order to facilitate your interpretation of this system. JNI (Java Native Interface) is used in Android's application (Dalvik VM) to call the shared library developed by CumberCraft +.

Development environment

(eclipse + Android ADT)

WindowsXP SP3

JDK 1.6.0.12

VMWARE+Ubuntu Linux 8.04

Android Jni usage step (1) create a new Android project in Eclipse

Project name JNItest

Package name com.ura.test

Activity name JNItest

Application name JNItest

Android Jni usage step (2) Editing main.xml

< ?xml version="1.0" encoding="utf-8"?>

< LinearLayout xmlns:android= "http://schemas.Android.com/apk/res/Android" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" >

< TextView Android:id="@+id/JNITest" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="@string/JNITest" />

< /LinearLayout>

Android Jni usage step (3) Editing the java file

Package com.ura.test; import Android.app.Activity; import Android.os.Bundle; import Android.widget.TextView; public class JNITest extends Activity {/ * * Called when the activity is first created. * / static {System.loadLibrary ("JNITest");} public native String GetTest (); @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main); String str = GetTest (); TextView JNITest = (TextView) findViewById (R.id.JNITest); JNITest.setText (str);}}

Android Jni uses method step (4) to generate head file

Compile the class file claimed by the above project, and then use the javah tool to generate the cmax cymbal + header file.

Javah-classpath bin-d jni com.ura.test.JNItest

The generated header file is as follows

/ * DO NOT EDIT THIS FILE-it is machine generated * / # include

< jni.h>

/ * Header for class com_ura_test_JNITest * / # ifndef _ Included_com_ura_test_JNITest # define _ Included_com_ura_test_JNITest # ifdef _ _ cplusplus extern "C" {# endif / * * Class: com_ura_test_JNITest * Method: GetTest * Signature: () Ljava/lang/String; * / JNIEXPORT jstring JNICALL Java_com_ura_test_JNITest_GetTest (JNIEnv *, jobject) # ifdef _ _ cplusplus} # endif # endif

The steps of Android Jni usage (5) are as follows.

# include "com_ura_test_JNITest.h"

# define LOG_TAG "JNITest"

# undef LOG

# include

< utils/Log.h>

JNIEXPORT jstring JNICALL Java_com_ura_test_JNITest_GetTest

(JNIEnv * env, jobject obj)

{

Return (* env)-> NewStringUTF (env, (char *)

"JNITest Native String")

LOGD ("Hello LIB!\ n")

}

Android Jni usage step (6) write Android.mk file

LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:=\ com_ura_test_JNITest.c LOCAL_C_INCLUDES: =\ $(JNI_H_INCLUDE) LOCAL_SHARED_LIBRARIES: = libutils LOCAL_PRELINK_MODULE: = false LOCAL_MODULE: = libJNITest include $(BUILD_SHARED_LIBRARY)

Android Jni usage step (7) compile and generate dynamic library

New folder

~ / mydroid/external/libJNITest

Copy the header file written above, the source file, and the make file into the directory above.

* Note that PRELINK_MOUDULE is set to false

Otherwise, you need to re-make the img file and then burn it.

Execute in ubuntu

Cd cd mydroid/build/ envsetup.sh cd ~ / mydroid cd external/libJNITest/ mm

After the compilation is successful, the libJNITest.so file will be generated in the following directory

~ mydroid/out/target/product/generic/system/lib/

Android Jni uses method step (8) to execute the program in the simulator

The first step is to copy the dynamic library into / system/lib.

Start the simulator

Adb shell adb remount adb push libJNITest.so / system/lib

Confirm that the copy is successful.

Cd / system/lib ls

Then do not turn off the simulator (turn off and then turn on the dynamic library, because the simulator rom is read-only)

Execute the java program JNITest

You'll see it printed on the screen.

JNITest Native String

This is the end of the sample analysis of Android Jni code. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report