简体   繁体   English

Android Studio链接预建库.so错误

[英]Android Studio link prebuilt library .so error

I'm developing an Android app using FFmpeg. 我正在使用FFmpeg开发Android应用。 I have built FFmpeg into *.so files and put them into jniLibs as follows: 我已经将FFmpeg内置到* .so文件中,并将它们放入jniLibs中,如下所示:

src src

--main - 主要

----jniLibs ---- jniLibs

------armeabi ------ armeabi

--------libavcodec-57.so --------libavcodec-57.so

--------libavformat-57.so --------libavformat-57.so

--------xx.so --------xx.so

While in grade script, abifilter of ndk is armeabi. 在等级脚本中,ndk的abifilter是armeabi。

In java files, I have succeeded load these .so files and the built apk also contains them. 在Java文件中,我已经成功加载了这些.so文件,并且内置的APK也包含它们。 However, when I use any API (eg av_register_all()) of them in a .c file under src/jni folder, build error comes: 但是,当我在src / jni文件夹下的.c文件中使用它们的任何API(例如av_register_all())时,都会出现构建错误:

Error:(14) undefined reference to 'av_register_all' 错误:(14)对“ av_register_all”的未定义引用

Error:Execution failed for task ':app:compileDebugNdk'. 错误:任务':app:compileDebugNdk'的执行失败。

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/zhouyf/Library/Android/sdk/ndk-bundle/ndk-build'' finished with non-zero exit value 2 com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:进程'command'/ Users / zhouyf / Library / Android / sdk / ndk-bundle / ndk-build”以非零退出值2

It seems that problem exists in linker. 看来链接器中存在问题。 But I found answer that just putting .so files to jniLibs/armeabi will be OK. 但是我找到了答案,只需将.so文件放入jniLibs / armeabi中就可以了。 Do I need modify build.gradle file to link those .so files or else? 我需要修改build.gradle文件来链接那些.so文件吗?

PS 聚苯乙烯

If I don't call the API, the app will run successfully, only with warning: W/linker: libavformat-57.so: unused DT entry: type 0x6ffffffe arg 0x60e0 W/linker: libavformat-57.so: unused DT entry: type 0x6fffffff arg 0x2 如果我不调用API,则该应用程序将成功运行,仅显示以下警告:W /链接器:libavformat-57.so:未使用的DT条目:类型0x6ffffffe arg 0x60e0 W /链接器:libavformat-57.so:未使用的DT条目:类型0x6fffffff arg 0x2

Environment: Android Studio 2.1.1 Mac OS X 10.11.5 环境:Android Studio 2.1.1 Mac OS X 10.11.5

You should add libraries as dependencies when you build by using ndk-build . 使用ndk-build时,应将库添加为依赖项。

Disable default ndk-build , in your build.gradle build.gradle禁用默认的ndk-build

sourceSets.main {
    jniLibs.srcDir 'src/main/libs'
    jni.srcDirs = []
}

Add your own ndk-build task, in your build.gradle build.gradle添加自己的ndk-build任务

def ndkDir = properties.getProperty("ndk.dir")

task ndkBuild(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine ndkDir + File.separator + 'ndk-build.cmd', '-C', file('src/main/jni').absolutePath
    } else {
        commandLine ndkDir + File.separator + 'ndk-build', '-C', file('src/main/jni').absolutePath
    }
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

Finally, in your Android.mk , add the libraries like this. 最后,在您的Android.mk ,添加类似的库。

LOCAL_MODULE := # ...
# ...
LOCAL_LDFLAGS += -L/path/of/the/libraries -lavcodec-57 -lavformat-57 ...

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

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