eclipse 如何在 Android 代码中使用 .so 文件来使用本机方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28366263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:23:33  来源:igfitidea点击:

How to use .so file in Android code to use the native methods

javaandroideclipseandroid-ndkjava-native-interface

提问by Vinay

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.

我有 libmath.so 文件,其中包含本机方法。我需要在我的 android 代码中调用本机方法。我创建了一个示例 android 应用程序并在 libs/armeabi 文件夹中添加了 libmath.so,然后运行“右键单击鼠标”-> Android 工具-> 添加本机支持。现在在应用程序的 jni 文件夹中创建了具有以下内容的以下文件。

Android.mk

安卓.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := libmath
LOCAL_SRC_FILES := libmath.cpp
include $(BUILD_SHARED_LIBRARY)

libmath.cpp

libmath.cpp

#include <jni.h>

When I run the Project as android application it shows as follows in the console :-

当我将项目作为 android 应用程序运行时,它在控制台中显示如下:-

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)

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

    }

Once after running the android application when I launch the application through emulator I am getting the below error.

当我通过模拟器启动应用程序运行 android 应用程序后,我收到以下错误。

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.

此错误的可能原因是什么。

采纳答案by ph0b

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.

如果您没有编写 C++ 代码,而只是直接从 Java 中使用预构建的 .so 文件,则不必使用 NDK。

Simply drop the .so file inside your project, under libs/<abi>for an eclipse project - under jniLibs/<abi>for a gradle project.

只需将 .so 文件放在您的项目中,在libs/<abi>eclipse 项目下 - 在jniLibs/<abi>gradle 项目下。

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 NDK 模块。当您构建您的项目时,NDK 生成了一个新的 libmath.so 文件,您的初始库中没有任何内容。因此,删除所有 jni 文件和文件夹,将以前的 .so 文件复制回libs/<abi>,然后再次运行您的项目。

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).

如果您遇到其他问题,请验证您的 libmath.so 是否实现了jstring com_example_math_MathJni_calc(JNIEnv* env, jobject* obj),或者检查您应该在 Java 端声明什么才能使用您的 lib,以及正确的包名和签名(如果您的 . so 文件,它应该说明)。