java Android Studio - 包含和使用 .so 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37116921/
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-11-03 02:13:30  来源:igfitidea点击:

Android Studio - include and consume .so library

javaandroidandroid-studiogradle

提问by user19922

I need to use some native libraries(.so) in my android project. According to some answers here in StackOverflow about this topic, I created a jniLibsfolder in app/src/mainand put there my files:

我需要在我的 android 项目中使用一些本机库(.so)。根据 StackOverflow 中关于这个主题的一些答案,我创建了一个jniLibs文件夹app/src/main并将我的文件放在那里:

armeabi/my_lib.so
armeabi-v7a/my_lib.so
x86/my_lib.so

Then, in my activity class I use:

然后,在我的活动课中,我使用:

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

But when I run the app, an UnsatisfiedLinkErrorexception is generated. If this is important to be noticed, I don't have an Android.mk file, and I haven't changed anything that has to do with this in my gradlefiles. So, the only think I did is to copy-paste my .sofiles in jniLibsand to write the code above in my activity. So what might be the cause of this problem? Am I missing something?

但是当我运行该应用程序时,UnsatisfiedLinkError会生成一个异常。如果要注意这一点很重要,我没有 Android.mk 文件,而且我的文件中也没有更改与此相关的任何内容gradle。所以,我唯一的想法就是复制粘贴我的.so文件jniLibs并在我的活动中编写上面的代码。那么这个问题的原因可能是什么?我错过了什么吗?

EDIT

编辑

This is my gradle:

这是我的毕业证书:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 15
    buildToolsVersion "23.0.3"

    compileOptions.encoding = 'ISO-8859-1'

    defaultConfig {
        applicationId "my.package"
        minSdkVersion 4
        targetSdkVersion 4

        ndk {
            moduleName "my_so_lib"
        }
    }

    sourceSets {
        main {
            jni.srcDirs = ["libs"]
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
        debug {
            debuggable true
        }
    }


    splits {
        abi {
            enable true
            reset()
            include 'x86', 'armeabi-v7a', 'mips', 'armeabi'
            universalApk false
        }
    }
}

回答by GuillaumeAgis

Solution 1

方案一

Create Folder "jniLibs" inside "src/main/" Put all your .so libraries inside "src/main/jniLibs" folder Folder structure looks like :

在“src/main/”中创建文件夹“jniLibs” 将所有 .so 库放在“src/main/jniLibs”文件夹中 文件夹结构如下:

|--app: 
|--|--src: 
|--|--|--main 
|--|--|--|--jniLibs 
|--|--|--|--|--armeabi 
|--|--|--|--|--|--.so Files 

Can you please confirm that you have this hierarchy ?

你能确认你有这个层次结构吗?

No extra code requires just sync your project and run your application.

没有额外的代码只需要同步您的项目并运行您的应用程序。

Reference https://github.com/commonsguy/sqlcipher-gradle/tree/master/src/main

参考 https://github.com/commonsguy/sqlcipher-gradle/tree/master/src/main

Solution 2

解决方案2

Add both code snippets in your module gradle.build file as a dependency:

在您的模块 gradle.build 文件中添加两个代码片段作为依赖项:

compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')

How to create this custom jar:

如何创建这个自定义 jar:

    task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
        destinationDir file("$buildDir/native-libs")
        baseName 'native-libs'
        from fileTree(dir: 'libs', include: '**/*.so')
        into 'lib/'
    }

tasks.withType(Compile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

source

来源

回答by user19922

Thank you guys for helping but it was a stupid problem. When I imported my .sofiles under jniLibs, they were named like libSONAME.so. In these lines of code:

谢谢你们的帮助,但这是一个愚蠢的问题。当我.so在 下导入我的文件时jniLibs,它们被命名为libSONAME.so. 在这些代码行中:

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

we should not use System.loadLibrary("libSONAME");, but just System.loadLibrary("SONAME");.

我们不应该使用System.loadLibrary("libSONAME");,而只是System.loadLibrary("SONAME");.

Then, just build the project and everything was OK.

然后,只需构建项目,一切就OK了。

Thank you all for helping. I hope this will save time to someone else.

谢谢大家的帮助。我希望这会为其他人节省时间。

回答by Arnold Vakaria

What is your gradle version? If you have a 0.7.3 or a newer version then the next article should help you: Click here!

你的gradle版本是什么?如果您有 0.7.3 或更新版本,那么下一篇文章应该可以帮助您:单击此处