问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

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
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
临沂比较有名的男装品牌 呼伦贝尔市悦动网络科技有限公司怎么样? 呼伦贝尔中汇实业有限公司怎么样? 呼伦贝尔油玉不绝电子商务有限公司怎么样? 如何避免wps卡顿? 属鼠的男人找对象是属什么,属鼠的人和什么属相合 96年鼠的姻缘在哪年 属相相合年份运势提升 2024属鼠找对象属什么最佳 黑客攻击网站能报案吗 黑客攻击报案有用吗 里面穿的毛衣高领好看还是低领好看??本人属于高壮型的6 如何在Android源码里查找Java中native方法对应... 黑色风衣搭配高领还是低领的毛衣?8 酒红色冬季风衣外套里面配什么衣服更好看呢?高领低领还是长毛衣... 寻找以兄弟或朋友情谊为主题的电影,催泪、感人的,最好是国语片。_百度... 广州周景川有几个孩子 “鹤”这个字的意思.1 吉林省卓远石材再利用有限公司怎么样? 吉林市茂森商贸有限公司怎么样? 吉林市凯尔维特机电设备有限责任公司吉顺分公司怎么样? 衢州市吉顺生物质能源有限公司怎么样? 笔记本电脑开不起机 插上电源灯不亮531 炖鸡肉的时候放了两勺白糖,后面又放了土豆会不会中毒? ...有时候人物卡这不动半天,,,玩战争机器这卡,别的单机游戏都卡屏... ...不卡《我玩的是外国游戏,有的时候会一顿一顿的》? feak到底是什么意思?1 岑 和岺 怎么读889 原物璧还的意思14 证券公司四大业务是什么?24 为什么可以用特勒根定理?结构不一样 光的折射与什么有关? 就是说当入射角相同的时候,折射角的大小... 公司招聘,10人面试。三个优秀,三个一般,四个差!需招四人!...1 为什么在北上广打拼的人会有一种价值上的优越感? 在北上广深打拼的单身年轻人的生活状态是怎样的?6 彻底看书看不进去了怎么办? 不喜欢读书,迫于父母,我无法退学...1 百度钱包登录27 中药阿魏怎样配饵钓鲤鱼9 怎么找回原来注册的? Win10怎么截图 三种Win10截图方法 用WORD怎么打出矩阵?48 我是女生经常用手机上网,和拍照,哪个牌子,哪款智能手机比较好... 神探夏洛克第三季 莫里亚蒂真的死了吗? 电视上播放莫里亚蒂M...271 十三五规划是什么意思75 十三五规划中的预备项目什么意思2 笔记本电脑开不了机充电也没反应?7 广州dj林琳主持老公叫什么 水浒传上哪一集宋江装疯11 水浒传宋江提反诗后被谁救了12 优酷视频为什么搜索视频刷选功能都没有了、我想安年份搜索下老片... 宋江浔阳楼题反诗的主要内容是什么?236