如何使用Android中code .so文件要使用本机方法 [英] How to use .so file in Android code to use the native methods
问题描述
我有有它的本地方法libmath.so文件。我需要调用本地方法在我的Android code。我创建了一个样本Android应用程序,并添加内库/ armeabi文件夹中的libmath.so,然后跑单击鼠标右键 - > Android的工具 - >添加原生支持。现在,下面的内容了以下文件应用程序的JNI文件夹内创建。
I have libmath.so file which have native methods in it. I need to call the native methods in my android code. I have created a sample android application and added the libmath.so inside libs/armeabi folder, Then ran "Right-click mouse" -> Android Tools -> Add native support. Now the following files with below content are created inside jni folder of the application.
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmath
LOCAL_SRC_FILES := libmath.cpp
include $(BUILD_SHARED_LIBRARY)
libmath.cpp
#include <jni.h>
当我运行该项目作为Android应用它显示在控制台如下: -
When I run the Project as android application it shows as follows in the console :-
17:58:41 **** Build of configuration Default for project math ****
"F:\\Vinay\\Softwares\\android-ndk-r10d\\ndk-build.cmd" all
[armeabi] Compile++ thumb: math <= libmath.cpp
[armeabi] StaticLibrary : libstdc++.a
[armeabi] SharedLibrary : libmath.so
[armeabi] Install : libmath.so => libs/armeabi/libmath.so
17:58:53 Build Finished (took 11s.695ms)
下面是code我是如何加载库: -
Here is the code how I am loading the library:-
public class MathJni {
static {
System.loadLibrary("math");
}
public native String calc(String paramString);
}
public static final MathJni math = new MathJni();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s= math.calc("help");
}
运行Android应用程序时我通过模拟器我得到下面的错误。启动应用程序后一旦
Once after running the android application when I launch the application through emulator I am getting the below error.
02-06 07:38:36.900: D/dalvikvm(831): Trying to load lib /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848
02-06 07:38:36.900: D/dalvikvm(831): Added shared lib /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848
02-06 07:38:36.910: D/dalvikvm(831): No JNI_OnLoad found in /data/app-lib/com.example.math-2/libmath.so 0xb3cd0848, skipping init
02-06 07:38:37.540: W/dalvikvm(831): No implementation found for native Lcom/example/math/MathJni;.calc:(Ljava/lang/String;)Ljava/lang/String;
02-06 07:38:37.540: D/AndroidRuntime(831): Shutting down VM
02-06 07:38:37.550: W/dalvikvm(831): threadid=1: thread exiting with uncaught exception (group=0xb3a20ba8)
02-06 07:38:37.570: E/AndroidRuntime(831): FATAL EXCEPTION: main
02-06 07:38:37.570: E/AndroidRuntime(831): Process: com.example.math, PID: 831
02-06 07:38:37.570: E/AndroidRuntime(831): java.lang.UnsatisfiedLinkError: Native method not found: com.example.math.MathJni.calc:(Ljava/lang/String;)Ljava/lang/String;
02-06 07:38:37.570: E/AndroidRuntime(831): at com.example.math.MathJni.calc(Native Method)
02-06 07:38:37.570: E/AndroidRuntime(831): at com.example.math.MainActivity.onCreate(MainActivity.java:16)
有什么可以为这个错误的可能原因。
What can be the possible reason for this error.
推荐答案
如果你写了没有C ++ code和只有prebuilt .so文件直接从Java的使用,你不必使用NDK。
if you have written no C++ code and only prebuilt .so files to use directly from Java, you don't have to use the NDK.
只要简单地把.so文件的项目中,在库/&LT; ABI&GT;
对于Eclipse项目 - 在 jniLibs /&LT; ABI&GT ;
一个项目的gradle
Simply drop the .so file inside your project, under libs/<abi>
for an eclipse project - under jniLibs/<abi>
for a gradle project.
下面是你所做的就是创建一个libmath NDK模块与一个几乎空白的内容。当你建立你的项目中,NDK生成与您最初的库没有什么新的libmath.so文件。因此,删除所有JNI文件和文件夹,复制回你的前.so文件到库/&LT; ABI方式&gt;
,并再次运行项目
Here what you did was to create a libmath NDK module with an almost empty content. When you built your project, the NDK generated a new libmath.so file with nothing from your initial library. So delete all your jni files and folders, copy your former .so files back to libs/<abi>
, and run your project again.
如果您遇到其他问题,请确认您libmath.so工具的jstring com_example_math_MathJni_calc(JNIEnv的* ENV,jobject * OBJ)
,或检查你应该申报本土Java端使用您的lib下,用正确的包名和签名(如果有与您的.so文件走过来一个文件,它应说明)。
If you run into other issues, verify that your libmath.so implements jstring com_example_math_MathJni_calc(JNIEnv* env, jobject* obj)
, or check what you should declare native in the Java side to use your lib, with the right package name and signature (if there was a documentation that came along with your .so files, it should state that).
这篇关于如何使用Android中code .so文件要使用本机方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!