How to fix the libgnustl_shared.so file duplicated which in third party sdks?

扶醉桌前 提交于 2019-12-04 03:18:12

Cleaner solution is to explicitly tell Gradle that you know about the problem and accept any of these files. Depending on the architectures you support you may need only some of the. You can find details in documentation

android {

 // some stuff
 packagingOptions {
        pickFirst 'lib/armeabi-v7a/libgnustl_shared.so'
        pickFirst 'lib/arm64-v8a/libgnustl_shared.so'
        pickFirst 'lib/x86_64/libgnustl_shared.so'
        pickFirst 'lib/x86/libgnustl_shared.so'
    }
}

I was able to fix this by adding the code below to my build.gradle. It's kind of a hack; there should a more elegant fix, like not having to include the React Native version. Ideally React Native would resolve this. I opened an issue: https://github.com/facebook/react-native/issues/9454

import com.android.build.gradle.internal.pipeline.TransformTask

def deleteDuplicateJniFiles() {
    def files = fileTree("${buildDir}/intermediates/exploded-aar/com.facebook.react/react-native/0.31.0/jni/") {
        include "**/libgnustl_shared.so"
    }
    files.each { it.delete() }
}

tasks.withType(TransformTask) { pkgTask ->
    pkgTask.doFirst { deleteDuplicateJniFiles() }
}

Finally,i move one of the so file to assets,and load it manually before used

 String path = getApplication().getFilesDir().toString() + "/armeabi-v7a/libgnustl_shared.so";
 if (!FileUtils.isFileExit(path))  //move so from assets to another dir
       FileUtils.initSOFileFromAssetsFile(getApplication()); 
 System.load(path);

This works not very well , although it fixes the DuplicateFileException bug . If anyone get the better way pls tell me . Thanks!^_^

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!