Android 中Native方法是怎样调用的

 我来答
huanglenzhi
2016-03-19 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
通过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)
千锋教育
2016-03-22 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
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 JNINativeMeth
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
撕烂你的贝塔cx
2016-03-31 · TA获得超过3943个赞
知道大有可为答主
回答量:2061
采纳率:70%
帮助的人:1269万
展开全部
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);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式