当前位置:首页 » 安卓系统 » androidnative调用

androidnative调用

发布时间: 2022-10-04 08:03:52

⑴ Unity在Android和iOS中如何调用Native API

首先unity支持在C#中调用C++ dll,这样可以在Android和iOS中提供C++接口在unity中调用。利用这一特性,可以扩展unity的功能。例如集成和调用第三方库。同时为了满足对unity接口的一致性,可以考虑在android和iOS上提供相同的接口供C#调用。这里列举以下两个例子。本例对应的接口声明如下:public class CallNativeAPI { #if UNITY_EDITOR public static void OpenWebView(string url) { return; } public static void SumNum(int v1, int v2) { TestUnityEditor.SumNum(v1, v2); return; } #elif UNITY_IPHONE [DllImport ("__Internal")] public static extern void OpenWebView(string url); [DllImport ("__Internal")] public static extern void SumNum(int v1, int v2); #elif UNITY_ANDROID [DllImport ("libtestunity", CallingConvention = CallingConvention.Cdecl)] public static extern void OpenWebView(string url); [DllImport ("libtestunity", CallingConvention = CallingConvention.Cdecl)] public static extern void SumNum(int v1, int v2); #endif public static void SumNumForResult(int v1, int v2, CallbackManager.ResultCallback callback) { TestCallbackManager.sumNumCallback.SetResultCallBack(new CallbackManager.ResultCallback(callback)); SumNum(v1, v2); return; } } namespace CallbackManager { public delegate void ResultCallback(int result); public class SumNumManager{ public SumNumManager() { } private ResultCallback resultCallback; public void SetResultCallBack(ResultCallback callback) { resultCallback = callback; } public void SendResult(int result) { resultCallback(result); } } } public class TestCallbackManager { public static CallbackManager.SumNumManager sumNumCallback = new CallbackManager.SumNumManager(); }

⑵ android 的native方法,在哪使用System.loadLib()方法

不是loadLib,是

java">static{
System.loadLibrary("你的库名");
}

这个是在类被加载的时候加载的,也就是你的Test。你说的有些没有使用load,是因为在系统启动的时候so已经被加载了。你可以通过命令行看到系统里面的很多so:

如果so被加载了,你就不需要再使用加载代码,可以直接用native接口

⑶ Android 中Native方法是怎样调用的

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);
}

⑷ native.js android 怎么调用不开源的动态库

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<script type="text/javascript">
//java代码调用js方法
function javacalljs(){
document.getElementById("content").innerHTML +=
"<br\>java调用了js函数";
}
</script>
</head>
<body>
this is my html <br/>
//js调用java方法,所有js全局对象、函数以及变量均自动成为window对象的成员
<a onClick="window.wst.jsCallAndroid()">点击调用java代码</a><br/>
<br/>
<div id="content">内容显示</div>
</body>
</html>

⑸ Android很多java 文件调用native函数为何没有System.loadLibrary或System.load

android不是标准的jvm,只是使用了java的语法,其他基本都是自己发明的
连字节码格式都不一样

另外,你去看标准javase里面也有很多本地方法没有用那些加载
因为那些本地方法是jvm实现的,不是用第三方动态库实现的

比如 Math.sin 方法,都是jvm自己的

⑹ Unity在Android和iOS中如何调用Native API

首先unity支持在C#中调用C++ dll,这样可以在Android和iOS中提供C++接口在unity中调用。利用这一特性,可以扩展unity的功能。例如集成和调用第三方库。同时为了满足对unity接口的一致性,可以考虑在android和iOS上提供相同的接口供C#调用。
这里列举以下两个例子。
2. 2. 简单的C# - C++ - Java/ObjC - C#的异步回调实现(会在下一期中给出实现)
由于android和iOS平台加载库的方式不同(android为动态加载,iOS为静态加载),在C#中针对不同平台对dll 接口的引用声明是不一样的。本例对应的接口声明如下:publicclassCallNativeAPI{
#ifUNITY_(stringurl){return;}publicstaticvoidSumNum(intv1,
intv2){TestUnityEditor.SumNum(v1,v2);return;}#elifUNITY_IPHONE
[DllImport(
"__Internal")](stringurl);[DllImport(
"__Internal")]publicstaticexternvoidSumNum(intv1,
intv2);#elifUNITY_ANDROID
[DllImport(
"libtestunity"
,CallingConvention=CallingConvention.Cdecl)](stringurl);[DllImport(
"libtestunity"
,CallingConvention=CallingConvention.Cdecl)]publicstaticexternvoidSumNum(intv1,
intv2);#(intv1,
intv2,CallbackManager.ResultCallbackcallback){

⑺ 如何调试Android Native Framew

半年前写了一篇文章,介绍 如何调试Android Framework。但是只提到了Framework中Java代码的调试办法,但实际上有很多代码都是用C++实现的;无奈当时并并没有趁手的native调试工具,无法做到像Java调试那样简单直观(gdb+eclipse/ida之流虽然可以但是不完美),于是就搁置下了。
Android Studio 2.2版本带来了全新的对Android Native代码的开发以及调试支持,另外LLDB的Android调试插件也日渐成熟,我终于可以把这篇文章继续下去了!本文将带来Android Framework中native代码的调试方法。
在正式介绍如何调试之前,必须先说明一些基本的概念。调试器在调试一个可执行文件的时候,必须知道一些调试信息才能进行调试,这个调试信息可多可少(也可以没有)。最直观的比如行号信息,如果调试器知道行号信息,那么在进行调试的时候就能知道当前执行到了源代码的哪一行,如果调试器还知道对应代码的源文件在哪,那么现代IDE的调试器一般就能顺着源码带你飞了,这就是所谓的源码调试。相反,如果没有行号和源码信息,那么只能进行更低级别的调试了,调试器只能告诉你一些寄存器的值;而当前运行的代码也只是PC寄存器所指向的二进制数据,这些数据要么是虚拟机指令,要么是汇编指令;这就是所谓的无源码调试。显然无源码调试相比源码级别的调试要麻烦的多;接下来将围绕这两个方面分别介绍。
http://blog.csdn.net/sinat_29384657/article/details/76685742

⑻ Android 中Native方法是怎样调用的

通过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文件到“Why\bin\classes”下.
4.Generate Jni.class file via the command below:
javac jni.java
Then 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,并且 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)

⑼ Android 中Native方法是怎样调用的

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

热点内容
vivoz6手机存储设备在哪里 发布:2025-03-13 08:08:36 浏览:53
emc存储模拟器下载 发布:2025-03-13 08:06:54 浏览:756
粒子群算法流程 发布:2025-03-13 08:04:44 浏览:290
pythonjsonkey 发布:2025-03-13 08:04:32 浏览:621
php删除数组元素 发布:2025-03-13 07:58:08 浏览:827
怎么编辑电脑按键脚本 发布:2025-03-13 07:57:26 浏览:753
ubuntu编辑python 发布:2025-03-13 07:56:32 浏览:529
服务器如何配置接口域名 发布:2025-03-13 07:52:41 浏览:318
oracle物理存储结构 发布:2025-03-13 07:43:00 浏览:821
大型ftp 发布:2025-03-13 07:41:20 浏览:20