Android 中Native方法是怎样调用的1
发布网友
发布时间:2023-10-11 22:38
我来回答
共3个回答
热心网友
时间:2024-11-23 18:58
通过jni接口调用native
步骤如下:
1.创建一个 android project, 名字叫Why
2 在工程Why中添加一个Java类,class名为Jni。这个类是一个JNI接口的Java类,文件名为Jni.java。
package com.yarin.android.Why;
public class Jni {
public native int getCInt();
public native String getCString();
}
3.将工程Why下的 "src\com\yarin\android\Why" 目录下的Jni.java文件copy到“Why\bin\classes”下.
4.Generate Jni.class file via the command below:
javac jni.java
Then copy the Jni.class file generated to cover and replace the original Jni.class file in directory “Why\bin\classes\com\yarin\android\Why”.
------The original Jni.class file, you must build the java project first to generate it.
5.Generate c style header file
javah –classpath C:\android-ndk-r6b\myproject\Why\bin\classes com.yarin.android.Why.Jni
com.yarin.android.Why is my package name appeared in Jni.java file.
com_yarin_android_Why_Jni.h is like below:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_yarin_android_Why_Jni */
#ifndef _Included_com_yarin_android_Why_Jni
#define _Included_com_yarin_android_Why_Jni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_yarin_android_Why_Jni
* Method: getCInt
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_yarin_android_Why_Jni_getCInt
(JNIEnv *env, jobject object); ---you need to supplement the parameter name yourself
/*
* Class: com_yarin_android_Why_Jni
* Method: getCString
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_yarin_android_Why_Jni_getCString
(JNIEnv *env, jobject object);
#ifdef __cplusplus
}
#endif
#endif
6.Add com_yarin_android_Why_Jni.c file corresponding to the above c style header file, then add implemented code.
#include <stdio.h>
#include <stdlib.h>
#include "com_yarin_android_Why_Jni.h"
int add()
{
int x,y;
x = 111;
y = 22;
x += y;
return x;
}
JNIEXPORT jint JNICALL Java_com_yarin_android_Why_Jni_getCInt
(JNIEnv *env, jobject object)
{
return add();
}
JNIEXPORT jstring JNICALL Java_com_yarin_android_Why_Jni_getCString
(JNIEnv *env, jobject object)
{
(*env)->NewStringUTF(env, " Why is ok ^_^ ----->> ");
}
7.然后实现在工程Why下创建目录jni,并且copy com_yarin_android_Why_Jni.c /h 到jni目录下。
8.在"Why\jni"目录下编写Android.mk ,在"android-ndk-r6b\jni"下编写Application.mk , 然后在NDK环境下编译native code,生成动态库libWhy.so。
9.在java工程中加入调用native 动态库的代码:
package com.yarin.android.Why;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class WhyActivity extends Activity {
static
{
System.loadLibrary("Why");
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Jni jni = new Jni();
TextView view = new TextView(this);
view.setText(jni.getCString() + Integer.toString(jni.getCInt()));
setContentView(view);
}
}
10 编译Why工程,然后 run as Android Application, 就能看到native code的调用结果了。
Tip:
Usage of the command javah.
javah -d <outputdir> -classpath <classpath> <fully_qualified_class>
where:
'outputdir' is the directory where to put the generated header file
'classpath' contains an absolute path to the directory containing your root package (as mentionned by Glen)
'fully_qualified_class' is the name of the class containing native methods without .class extension
-jni option is not required (set by default)
热心网友
时间:2024-11-23 18:58
1. Power.java--> find corresponding native cfile(查找对应的具体用C实现的C文件)
android.os.Power.java -------- native file ---->.../jni/android_os_Power.c
2. in android_os_Power.c, you canfind:
static JNINativeMethod method_table[]= // Native functiontable{
{"acquireWakeLock", "(ILjava/lang/String;)V", (void*)acquireWakeLock},
{"releaseWakeLock", "(Ljava/lang/String;)V", (void*)releaseWakeLock},
{"setLastUserActivityTimeout", "(J)I",(void*)setLastUserActivityTimeout },
{"setScreenState", "(Z)I", (void*)setScreenState },
{"shutdown", "()V", (void*)android_os_Power_shutdown },
{ "reboot","(Ljava/lang/String;)V", (void*)android_os_Power_reboot },
};
int register_android_os_Power(JNIEnv *env)// function to register mapping tablefrom name to function
{
returnAndroidRuntime::registerNativeMethods(
env, "android/os/Power",
method_table, NELEM(method_table));
}
3. in /framework/base/core/jni , a file named:AndroidRuntime.cpp
3.1) a global register function array
static const RegJNIRec gRegJNI[] =
{
...
register_android_os_Power,
}
3.2) Register native function process
int AndroidRuntime::startReg(JNIEnv* env)
or
Java_com_android_internal_util_WithFramework_registerNatives(...)
or
Java_LoadClass_registerNatives(....)
---> register_jni_procs(gRegJNI, NELEM(gRegJNI),env)
---> foreach(member of gRegJNI) call register_XXX_XXX_XXX..XXX() //so here register_android_os_power() will becalled
---> AndroidRuntime::registerNativeMethods(env, class_namelike "android/os/Power", method table like method_table,size)
---> jniRegisterNativeMethods(env, className,gMethods, numMethods)
-->pEnv->RegisterNatives(env, clazz, gMethods,numMethods) ;
--> foreach(method) calldvmRegisterJNIMethod(ClassObject* clazz, const char*methodName,
constchar* signature, void* fnPtr)
--> calldvmSetNativeFunc(method, dvmCallSynchronizedJNIMethod, fnPtr); //for sycn method
or
call dvmSetNativeFunc(method, dvmCallJNIMethod,fnPtr);
--> ((Method*)method)->insns = insns; // set actual codespace to be executed for a native function
4.calling a native method ( JNI method)
void dvmPlatformInvoke(void* pEnv,ClassObject* clazz, int argInfo, int argc,
const u4*argv, const char* shorty, void* func, JValue*pReturn)
dvmCallMethod() /dvmInvokeMethod
---> if(dvmIsNativeMethod(method))
{
(*method->nativeFunc)(self->curFrame,&retval, method, self);
}
热心网友
时间:2024-11-23 18:58
1.Power.java--> find corresponding native cfile(查找对应的具体用C实现的C文件) android.os.Power.java:native file ---->.../jni/android_os_Power.cpp2. in android_os_Power.c, you canfind: 点击(此处)折叠或打开static JNINativeMethod method_table[]=// Native functiontable{ {"acquireWakeLock","(ILjava/lang/String;)V",(void*)acquireWakeLock}, {"releaseWakeLock","(Ljava/lang/String;)V",(void*)releaseWakeLock}, {"setLastUserActivityTimeout","(J)I",(void*)setLastUserActivityTimeout }, {"setScreenState","(Z)I",(void*)setScreenState }, {"shutdown","()V",(void*)android_os_Power_shutdown }, {"reboot","(Ljava/lang/String;)V",(void*)android_os_Power_reboot },};int register_android_os_Power(JNIEnv *env)//functionto register mapping tablefrom name //tofunction{ returnAndroidRuntime::registerNativeMethods( env,"android/os/Power", method_table, NELEM(method_table));}3. in /framework/base/core/jni , a file named:AndroidRuntime.cpp 1) a global register function array 点击(此处)折叠或打开static const RegJNIRec gRegJNI[]= { // ... register_android_os_Power, } 2) Register native function process 点击(此处)折叠或打开int AndroidRuntime::startReg(JNIEnv* env)orJava_com_android_internal_util_WithFramework_registerNatives(...)orJava_LoadClass_registerNatives(....) ---> register_jni_procs(gRegJNI, NELEM(gRegJNI),env) ---> foreach(member of gRegJNI)call register_XXX_XXX_XXX..XXX() //so here register_android_os_power() will becalled---> AndroidRuntime::registerNativeMethods(env, /*class_namelike */"android/os/Power", /*method table like*/method_table, size) ---> jniRegisterNativeMethods(env, className,gMethods, numMethods) ---> pEnv->RegisterNatives(env, clazz, gMethods,numMethods); ---> foreach(method) call dvmRegisterJNIMethod(ClassObject* clazz, const char*methodName, constchar* signature, void* fnPtr) ---> call dvmSetNativeFunc(method, dvmCallSynchronizedJNIMethod, fnPtr); //for sycn methodor call dvmSetNativeFunc(method, dvmCallJNIMethod,fnPtr); --->((Method*)method)->insns = insns; //set actual codespace to be executed for a native function4.calling a native method ( JNI method) 点击(此处)折叠或打开void dvmPlatformInvoke(void* pEnv,ClassObject* clazz,int argInfo,int argc, const u4*argv,const char* shorty, void* func, JValue*pReturn) dvmCallMethod()/dvmInvokeMethod