Search code examples
androidaar

Getting "Program type already present" error in Android Studio


Problem Description

Hi All,

I am working on one Android App having 4 (Four) other modules (Library Modules) created by me e.g. lib_module_1, lib_module_2, lib_module_3 and core_api_lib_module. So far, I was using these library module in my app using project dependency as below--

    implementation project(":lib_module_1")
    implementation project(":lib_module_2")
    implementation project(":lib_module_3")
    implementation project(":core_api_lib_module")

in this case my app is working fine, but now I got one requirement to use equivalent (.aar) files for the same modules instead of these actual library modules for the code management purpose. So, I separated these library modules from my app code and managed to create equivalent (.aar) files for the respective library modules. Now I created "libs" directory in my "app" and copied all the (.aar) files in to it. Then I written below dependency code in app-level build.gradle file--

    implementation(name: 'lib_module_1', ext: 'aar')
    implementation(name: 'lib_module_2', ext: 'aar')
    implementation(name: 'lib_module_3', ext: 'aar')
    implementation(name: 'core_api_lib_module', ext: 'aar')

Also written below code in project-level build.gradle file--

allprojects {
    repositories {
        jcenter()
        google()
        flatDir {
            dirs 'libs'
        }
    }
}

Then I Synced the project successfully, but now when I'm trying to run the project, I am getting below run-time error for any random class written in "core_api_lib_module"--

Program type already present: com.core.api.BuildConfig

Sometimes I get same error for any other class written in the same "core_api_lib_module" library. For more details, please find my complete "app/build.gradle" file code as below--

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
android {
    compileSdkVersion 29
    buildToolsVersion '29.0.2'
    defaultConfig {
        applicationId "come.xyz.my_package"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 87
        versionName "87.0"
        multiDexEnabled true;
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/license.txt'
        exclude 'META-INF/notice.txt'
        pickFirst 'lib/x86_64/libhsscl.so'
        pickFirst 'lib/armeabi/libhsscl.so'
        pickFirst 'lib/x86/libhsscl.so'
        pickFirst 'lib/armeabi-v7a/libhsscl.so'
        pickFirst 'lib/arm64-v8a/libhsscl.so'
        pickFirst 'lib/mips/libhsscl.so'
        pickFirst 'lib/mips64/libhsscl.so'
    }
    lintOptions {
        disable 'MissingTranslation'
    }
    sourceSets {
        main {
            assets.srcDirs = ['src/main/assets', 'src/main/assets/']
            res.srcDirs = ['src/main/res', 'src/main/res/drawable']
        }
    }
}

dependencies {
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:11.0.2'
    implementation 'com.google.android.gms:play-services-analytics:17.0.0'
    implementation 'com.google.android.gms:play-services-wallet:18.0.0'
    implementation fileTree(dir: 'libs', include: ['*.aar'])
    testImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude module: 'support-v4' exclude module: 'support-v13' exclude module: 'recyclerview-v7'
    })
    implementation 'androidx.appcompat:appcompat:1.0.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.amplitude:android-sdk:2.16.0'
    implementation 'com.google.zxing:core:3.2.1'
    implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
    implementation 'com.google.android.material:material:1.0.0'
    implementation project(":liblknscratcherview")
    implementation 'org.springframework.android:spring-android-core:1.0.1.RELEASE'
    implementation 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
    implementation 'commons-io:commons-io:2.4'
    implementation 'com.google.code.gson:gson:+'
    implementation 'org.apache.httpcomponents:httpcore:4.4.6'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.squareup.retrofit:retrofit:1.9.0'
    implementation 'com.daimajia.swipelayout:library:1.2.0@aar'
    implementation 'com.kontaktio:sdk:3.3.3'
    implementation 'io.gresse.hugo.vumeterlibrary:vumeterlibrary:1.0.15'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.mindorks:placeholderview:0.2.7'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.facebook.shimmer:shimmer:0.1.0@aar'
    implementation 'com.sothree.slidinguppanel:library:3.3.1'
    implementation 'org.twitter4j:twitter4j-core:4.0.3'
    implementation 'org.twitter4j:twitter4j-async:4.0.3'
    implementation 'androidx.multidex:multidex:2.0.0'
    implementation 'com.facebook.android:facebook-login:[4,5)'
    implementation 'com.facebook.android:facebook-share:[4,5)'
    implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
    implementation 'bz.tsung.android:objectify:2.0'
    implementation 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'
    implementation 'de.hdodenhof:circleimageview:3.0.0'

    implementation(name: 'lib_module_1', ext: 'aar')
    implementation(name: 'lib_module_2', ext: 'aar')
    implementation(name: 'lib_module_3', ext: 'aar')
    implementation(name: 'core_api_lib_module', ext: 'aar')

    implementation project(":library")
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true;
    }
    implementation('com.crashlytics.sdk.android:crashlytics-ndk:2.0.2@aar') {
        transitive = true;
    }
}

crashlytics {
    enableNdk true
    androidNdkOut 'src/main/obj'
    androidNdkLibsOut 'src/main/libs'
}

What I tried so far

1] Tried deleting ".gradle" folder from project hierarchy then clean, rebuild and run project but getting same error.

2] Tried deleting all cache from "C:\Users\%USERNAME%.gradle\caches\" directory but getting same error.

3] Tried "Invalidate Caches /Restart" option but still getting same error.

4] Then I googled this "Program type already present" error and tried all the possible option available on below links--

link-1: What does "Program type already present" mean?

link-2: Android Studio - Error:Program type already present

Strange thing I noticed:

I created separate demo app and tried using all 4 (.aar) library files in it, and surprisingly that demo app is working fine without any error. I am able to implement functionality from all 4 (.aar) library file to my demo app but not in actual app.

I hope given information is enough to understand my problem, but still in case you need any other details, please let me know. Thank you!


Solution

  • After 2 days of RND, finally I got the answer. Adding below code in my app-level build.gradle file worked for me--

    android {
        dexOptions {
            preDexLibraries = false
        }
    } 
    

    For more details, please check the answer given by "iceman" on this post.