To create a new Android project, set the layout:
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " > <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Call C function" /></Relativelayout>
Steps for JNI Development
① write Java code Declaration local method used to native keyword local method not to implement
Create a JNI folder under the ② project root directory
③ create a. c file under the Jni folder
④ Import <jni.h>
#include <stdlib.h>#include<stdio.h>#include<jni.h>//jnienv* Env is a level two pointer to Jninativeinterface//JNIEnv is a first-level pointer to Jninativeinterface//A large number of function pointers are defined in the JNINATIVEINTERFACE structure, and these function pointers are very common in JNI development .//(*env), call the function pointer in the struct body//Jobject the Java object that calls the local function, in this case the example of Mainactivity//c local function naming rules Java_ package Name _ Class Name _ Local method name//jstring (*newstringutf) (jnienv*, const char*)Jstring JAVA_COM_WUYUDONG_JNIHELLO_MAINACTIVITY_HELLOFROMC (jnienv*env, Jobject thiz) { Char* str ="Hello from c!"; return(*env)Newstringutf (env, str);}
⑤ Create android.mk makefile tell the compiler where the source file for C is, what is the name of the compiled object to generate
Local_path: = $ (call my-dir) include $ (clear_vars) local_module:= Hello # Specifies the name of the generated dynamic-link library local_src_files:= hello.c #指定了C的源文件叫什么名字 include $ (build_shared_library)
⑥ call Ndk-build compile C code to generate the location of the dynamic link library. So file files lib->armeabi->.so
Path to project: F:\workspace_sdk4\01_JNIHelloworld
Open cmd
Input: cd/d F:\workspace_sdk4\01_JNIHelloworld
Input: Ndk-build
Compile quickly, the generated files in the Libs directory, refresh the project can be seen
⑦ loading the dynamic link library system.loadlibrary ("Name of the dynamic Link library") in Java code; The name specified by the Android.mklocal_module
Mainactivity.java
PackageCom.wuyudong.jnihello;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidClick (View view) { system.loadlibrary ( "Hello"); String result = HELLOFROMC (); Toast.maketext (Getapplicationcontext (), result,0). Show (); } //declaring local methods using the Native keyword local method without implementing Public nativeString HELLOFROMC ();}
Finally deploy the project to the emulator
Android JNI HelloWorld