0

We have a 3rd party aar file that due to size considerations we decided to split to a separate dynamic-feature (module). Both main app and dynamic module are using com.google.code.gson:gson when they were in the same module we removed our dependency to gson, but now our main module needs it.

The project build is fine, but when we try to build bundle(s) we get a

"Program type already present: com.google.gson.FieldNamingPolicy$5" error

We tried to exclude gson from the module's gradle: both in the dependencies and android sections but with no luck. this is a 3rd party aar so we do not have any access to its code or dependencies.

Main app gradle:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' }
    mavenCentral()
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 91
        versionName "1.1.91"
        multiDexEnabled true
    }
    buildTypes {
        debug {
            versionNameSuffix "-D"
            matchingFallbacks = ['debug']

        }
        release {
            matchingFallbacks = ['release']
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        pickFirst 'META-INF/license.txt'
        pickFirst 'META-INF/DEPENDENCIES'
    }

    dexOptions {
        javaMaxHeapSize "2g"
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
        targetCompatibility JavaVersion.VERSION_1_8
    }
    flavorDimensions "permissions"
    productFlavors {
    }
    dynamicFeatures = [":dynamic_feature"]
}

dependencies {
    implementation files('libs/gcm.jar')
    //...
    implementation 'com.google.code.gson:gson:2.8.2'
    //...
}

apply plugin: 'com.google.gms.google-services'

dynamic-feature gradle:

apply plugin: 'com.android.dynamic-feature'

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }    
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
    }

    configurations {
        //DOES NOT RESOLVE THE PROBLEM
        all*.exclude group: 'com.google.gson'
        all*.exclude group: 'com.google.code.gson'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation project(':myapp')
    //...
    implementation files('libs/sdk.3rd-party.aar')
    configurations {
        //DOES NOT RESOLVE THE PROBLEM
        all*.exclude group: 'com.google.gson'
        all*.exclude group: 'com.google.code.gson'
    }
}

Error:

org.gradle.execution.MultipleBuildFailures: Build completed with 1 failures. ... Caused by: java.lang.RuntimeException: com.android.build.api.transform.TransformException: Error while generating the main dex list: Error while merging dex archives: Program type already present: com.google.gson.FieldNamingPolicy$5

Snufkin
  • 108
  • 1
  • 8
  • Do you have to depend on jars/aars? Could you replace these dependencies with maven dependencies? – Pierre Jun 21 '19 at 07:19
  • @Pierre , unfortunately no. the `'libs/sdk.3rd-party.aar'` is an external 3rd party provided 'as is' that is not available on maven. We can build it together or apart. everything works fine. but we cannot build it as a bundle so we cannot publish it on google play. – Snufkin Jun 22 '19 at 12:08
  • You could try removing the gson dependency since it seems to be already included in that 3rd-party aar. – Pierre Jun 22 '19 at 20:27
  • @Pierre ,When the 2 models were together (as one base module) that was exactly what I did. But since we split it to 2 modules: base (main app) and dynamic-feature, the base module needs gson for itself and cannot relay on an 'optional' feature. The project won't 'build' if I remove the gson dependency from the base module. Thanks for helping!! I'm really stuck for more than a week... – Snufkin Jun 22 '19 at 22:05
  • Your only two options are to either put that aar as a dependency of the base module, or exclude gson from the aar (and mark it as a runtime dependency instead). – Pierre Jun 23 '19 at 11:44
  • One more option actually would be to proguard the gson library in the aar so the class names are obfuscated and don't conflict with the other gson library in your base module. – Pierre Jun 23 '19 at 12:10
  • @Pierre , we created the new module in order to remove this aar from our main app since its relatively big and most of our users don't use its features. as to the other two options: `runtime files('libs/sdk.3rd-party.aar')` gives us an error (Could not find method runtime() for arguments [file collection]) as to proguard: how do we proguard a specific library in an aar? Thanks!! – Snufkin Jun 26 '19 at 10:26
  • That's a very different question on the Gradle syntax which I'm not best equipped to answer unfortunately. – Pierre Jun 26 '19 at 16:19

0 Answers0