chamathabeysinghe
chamathabeysinghe

Reputation: 868

Duplicate files copied in APK Android - Can it be merged?

In my android project I have included two libraries as JARs into libs folder. And I add them to the build Gradle as below.

dependencies {
    compile files('libs/siddhi-core-4.0.0-M13-SNAPSHOT.jar')
    compile files('libs/siddhi-execution-math-4.0.2-SNAPSHOT.jar')
}

Those two jar files the have a file with the same name ("org.wso2.siddhi.annotation.Extension") but with different content. And both files are important for the project. Since it has same name gradle won't build saying

Duplicate files copied in APK

How can I merge those two files into one single file with the same name? Those two files are text files with a list of Class names. In two files they have two different lists. So I want to merge them into one list in a text file with same name.

enter image description here

Upvotes: 5

Views: 870

Answers (4)

Prashant Andhale
Prashant Andhale

Reputation: 169

try this one

 dependencies {
       compile files('libs/siddhi-execution-math-4.0.2-SNAPSHOT.jar')
       compile files("$buildDir/libs/siddhi-core-4.0.0-M13-SNAPSHOT") {
          builtBy "unzipJar"
       }
    }

Upvotes: -1

chamathabeysinghe
chamathabeysinghe

Reputation: 868

Finally I found the answer. In the app build gradle you can specify to merge the conflicting files.

packagingOptions {         
    merge 'META-INF/annotations/org.wso2.siddhi.annotation.Extension'
}

for details look here https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html

Upvotes: 4

Alok
Alok

Reputation: 881

You can not as both files having same name resolution.(org.wso2.siddhi.annotation.Extension). So it will not going to work . Lets say some how you have included both the jars than how you are going to use one of them ie. How runtime will uniquely identify the class as both class have same namespace as well as name.

Upvotes: 0

VikasGoyal
VikasGoyal

Reputation: 3376

You can exclude the file from jar by unzip them first and copy the jar without those files, After that compile that unZip copy file instead of the actual file, like this

task unzipJar(type: Copy) {
   from zipTree('libs/siddhi-core-4.0.0-M13-SNAPSHOT.jar')
   into ("$buildDir/libs/siddhi-core-4.0.0-M13-SNAPSHOT")
   include "**/*.class"
   exclude "org.wso2.siddhi.annotation.Extension"
}

dependencies {
   compile files('libs/siddhi-execution-math-4.0.2-SNAPSHOT.jar')
   compile files("$buildDir/libs/siddhi-core-4.0.0-M13-SNAPSHOT") {
      builtBy "unzipJar"
   }
}

Please check this, even I didn't get the chance to use it but it should work.

Upvotes: 2

Related Questions