普通方法调用与本机方法调用有什么区别?
What is the difference between normal method call from native method call?
//my class
class NDKSupport {
// Loding library
static {
System.loadLibrary("NDKTest");
}
// Stuff in NDKSupport
public native String getMessageFromNDK(NDKSupport support);
public String getMessage() {
// doing some stuff over here
return "SOME RESULT";
}
}
//Native code
JNIEXPORT jstring JNICALL
Java_com_afbb_ndkcheck_NDKSupport_getMessageFromNDK(
JNIEnv* env, jobject thiz, jobject ndkSupport) {
jclass ndkSupportClz = env->GetObjectClass(ndkSupport);
jmethodID getInstallationSource =
env->GetMethodID(ndkSupportClz,"getMessage", "()Ljava/lang/String;");
return (jstring)env->CallObjectMethod(mainActivity, getInstallationSource);
}
现在在另一个 class 中,我正在创建对象并调用 2 个方法,两者都将执行相同的操作。这两个调用在性能方面或其他方面有什么区别。
NDKSupport support = new NDKSupport();
// what is the difference between these two calls
String d = support.getMessage();
String d2 = support.getMessageFromNDK(support);
给我一些关于这个的信息或者是否有可用的教程。
请原谅我的英语..
NDK 允许您使用 C/C++ 编写代码,然后 link 它进入您的 Java 应用程序。您可能会提高应用程序的速度。
NDK 的缺点是,它只能编译到特定的 CPU(而停留在 Java 领域意味着它可以在 Android 的任何目标版本上运行)。
本机 C 代码将使用底层操作系统的 (linux) API(系统调用),因此比 java 代码必须通过 JVM.Most 游戏将原生 C 代码库用于 2D/3d 图形、输入、声音等...
所以没有什么区别,但是你可以提高你的代码性能,如果你想为一些特定的代码 cpu 你可以使用本地方法,但如果你想制作一个与所有兼容的应用程序cpu 的类型,您可以使用简单的 java 方法。
您可以根据您的应用要求使用任何方法。
你可以按照一些视频教程 here。这是很好的解释和很好的教程。
这里有很多教程-
firstsecondthird
本机方法是特定于平台的代码。它们通常用 C 或 C++ 等语言编写并包含在库 (dll) 中。可以创建受益于此类库的混合 Java 应用程序。
使用本机方法的原因
- 获得对您设备的特殊功能的访问权或Android
OS
- 获得额外的速度
- 获得对大量现有遗留代码的访问权限
通常,NDK 的良好用例是 CPU 密集型应用程序,例如游戏引擎、信号处理和物理模拟
本机方法的主要缺点是您不具备跨平台能力。
现在,如果您不知道什么是本机代码,那么您可能不需要使用本机代码。 Android NDK 文档解释得很好:
..., you should understand that the NDK will not benefit most apps. As
a developer, you need to balance its benefits against its drawbacks.
Notably, using native code on Android generally does not result in a
noticable performance improvement, but it always increases your app
complexity. In general, you should only use the NDK if it is essential
to your app—never because you simply prefer to program in C/C++. When
examining whether or not you should develop in native code, think
about your requirements and see if the Android framework APIs provide
the functionality that you need.
//my class
class NDKSupport {
// Loding library
static {
System.loadLibrary("NDKTest");
}
// Stuff in NDKSupport
public native String getMessageFromNDK(NDKSupport support);
public String getMessage() {
// doing some stuff over here
return "SOME RESULT";
}
}
//Native code
JNIEXPORT jstring JNICALL
Java_com_afbb_ndkcheck_NDKSupport_getMessageFromNDK(
JNIEnv* env, jobject thiz, jobject ndkSupport) {
jclass ndkSupportClz = env->GetObjectClass(ndkSupport);
jmethodID getInstallationSource =
env->GetMethodID(ndkSupportClz,"getMessage", "()Ljava/lang/String;");
return (jstring)env->CallObjectMethod(mainActivity, getInstallationSource);
}
现在在另一个 class 中,我正在创建对象并调用 2 个方法,两者都将执行相同的操作。这两个调用在性能方面或其他方面有什么区别。
NDKSupport support = new NDKSupport();
// what is the difference between these two calls
String d = support.getMessage();
String d2 = support.getMessageFromNDK(support);
给我一些关于这个的信息或者是否有可用的教程。 请原谅我的英语..
NDK 允许您使用 C/C++ 编写代码,然后 link 它进入您的 Java 应用程序。您可能会提高应用程序的速度。
NDK 的缺点是,它只能编译到特定的 CPU(而停留在 Java 领域意味着它可以在 Android 的任何目标版本上运行)。
本机 C 代码将使用底层操作系统的 (linux) API(系统调用),因此比 java 代码必须通过 JVM.Most 游戏将原生 C 代码库用于 2D/3d 图形、输入、声音等...
所以没有什么区别,但是你可以提高你的代码性能,如果你想为一些特定的代码 cpu 你可以使用本地方法,但如果你想制作一个与所有兼容的应用程序cpu 的类型,您可以使用简单的 java 方法。 您可以根据您的应用要求使用任何方法。 你可以按照一些视频教程 here。这是很好的解释和很好的教程。 这里有很多教程- firstsecondthird
本机方法是特定于平台的代码。它们通常用 C 或 C++ 等语言编写并包含在库 (dll) 中。可以创建受益于此类库的混合 Java 应用程序。
使用本机方法的原因
- 获得对您设备的特殊功能的访问权或Android OS
- 获得额外的速度
- 获得对大量现有遗留代码的访问权限
通常,NDK 的良好用例是 CPU 密集型应用程序,例如游戏引擎、信号处理和物理模拟
本机方法的主要缺点是您不具备跨平台能力。
现在,如果您不知道什么是本机代码,那么您可能不需要使用本机代码。 Android NDK 文档解释得很好:
..., you should understand that the NDK will not benefit most apps. As a developer, you need to balance its benefits against its drawbacks. Notably, using native code on Android generally does not result in a noticable performance improvement, but it always increases your app complexity. In general, you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++. When examining whether or not you should develop in native code, think about your requirements and see if the Android framework APIs provide the functionality that you need.