简体   繁体   English

Android Studio .so共享库中的调用方法

[英]Calling method inside .so shared library on android studio

I need some help to use the method "callMethod()" inside ".so" (shared library) in android studio. 我需要一些帮助来使用android studio中“ .so”(共享库)中的方法“ callMethod()”。

My android studio version is 2.1.3 我的android studio版本是2.1.3

I created jniLibs folder in "src/main/" Then I created three folders inside jniLibs folder: (armeabi, armeabi-v7a, x86) Then I putted the .so library inside every mentioned folder. 我在“ src / main /”中创建了jniLibs文件夹,然后在jniLibs文件夹中创建了三个文件夹:(armeabi,armeabi-v7a,x86),然后在每个提到的文件夹中放入了.so库。 After that, I updated the build.gradle as following: 之后,我更新了build.gradle如下:

compile fileTree(dir: 'libs', include: ['*.jar','.so'])

In the MainAcrivity java class I wrote this: 在MainAcrivity java类中,我这样写:

static {
        System.loadLibrary("SharedLibAndroid");
    }

After that I don't know what should I do? 之后,我不知道该怎么办?

I need explaining how do I call the method "callMethod()" from the .so library with example please. 我需要用示例解释如何从.so库中调用方法“ callMethod()”。

I searched a lot but I did't see full example how to do that. 我进行了很多搜索,但没有看到完整的示例。

Thanks in advanced. 提前致谢。

My Update 我的更新

I've added to my project new class and native method like below: 我在项目中添加了新的类和本机方法,如下所示:

@SuppressWarnings("JniMissingFunction")
public class Test1Cpp {
static {
    System.loadLibrary("Cpp");
}

public native String callMethod();
}

Then I can call the method inside .so shared library, 然后,我可以在.so共享库中调用该方法,

I faced another problem, which is my .so library didn't work with all platform mobiles, 我遇到了另一个问题,那就是我的.so库无法在所有平台移动设备上正常工作,

The error is: 错误是:

java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.mysecondtestapp-1/lib/arm/libCpp.so" has unexpected e_machine: 3 java.lang.UnsatisfiedLinkError:dlopen失败:“ / data / app / com.mysecondtestapp-1 / lib / arm / libCpp.so”具有意外的e_machine:3

So, my question: how to convert my .so library to be worked on all platform mobiles? 所以,我的问题是:如何将我的.so库转换为可在所有平台移动设备上使用?

I assume that the library does load, otherwise you would get an exception, and I guess you would have mentioned it. 我假设该库确实已加载,否则您将获得异常,并且我想您会提到它。 So you need to declare the native method you would like to use from the library, and then call it. 因此,您需要从库中声明要使用的本机方法,然后调用它。 You can see a simple example here: Sample: hello-jni 您可以在此处看到一个简单的示例: 示例:hello-jni

The sample source code may be found here: Hello JNI . 示例源代码可以在这里找到: Hello JNI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM