How to Exclude Duplicate C Shared Libraries (.so) in a Multi-Project Android Build?

后端 未结 1 1593

I get a \"duplicate files\" conflict when building a parent project with two library modules, which make use of the same libc++_shared.so shared library.

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 18:35

    I ran into the same problem and had no luck with exclude or pickFirst. So I used a somewhat ugly workaround. The idea is to create a 'native-libs' folder in the build directory of the main project, copy all required *.so files from ndk library projects there and then tell the build system to package those libs in the apk.

    In my main project (the app project), I explicitely define the list of modules that contain ndk codes on which I depend

    // Ndk stuff. We have to explicitely manage our NDK dependencies
    ext.jniProjects = [project(':ndklib1'), project(':ndklib2'), project(':ndklib3')]
    apply from: '../depend_ndk.gradle'
    

    And then, 'depend_ndk.gradle' is a gradle external script that contains

    // Build helper for projects that depends on a native library with a NDK part
    // Define the list of ndk library you depend on in project main file :
    //   ext.jniProjects = [project(':ndklib1')]
    //   apply from : 'depend_ndk.gradle'
    buildscript {
        repositories {
            jcenter()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.12.+'
        }
    }
    import com.android.build.gradle.tasks.PackageApplication
    
    // As a workaround, we create a new 'native-libs' folder in the current project and
    // copy all the .so we depend on into it
    def ndkLibsDir = new File(buildDir, 'native-libs')
    ndkLibsDir.mkdir()
    
    task copyDependingNativeLibs(type: Copy) {
        // Doc for copy http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html
        println 'jniProjects ' + jniProjects
        jniProjects.each {
            from(new File(it.buildDir, 'native-libs')) {
                include '**/*.so'
            }
        }
        into ndkLibsDir
    }
    
    tasks.withType(PackageApplication) { pkgTask ->
        pkgTask.jniFolders = new HashSet()
        pkgTask.jniFolders.add(ndkLibsDir)
        pkgTask.dependsOn copyDependingNativeLibs
    }
    

    0 讨论(0)
提交回复
热议问题