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

8
  • Do you have to depend on jars/aars? Could you replace these dependencies with maven dependencies? Commented Jun 21, 2019 at 7: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. Commented Jun 22, 2019 at 12:08
  • You could try removing the gson dependency since it seems to be already included in that 3rd-party aar. Commented Jun 22, 2019 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... Commented Jun 22, 2019 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). Commented Jun 23, 2019 at 11:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.