Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Redeclaration class configuring Android build variants

in my Android project I configured a dimension with 3 variants (example: mock, dev, prod). I also have the default build types (debug, release) where I have their implementation of Application:

  • src/debug/java/package/MyApplication.kt
  • src/release/java/package/MyApplication.kt

So I can generate 6 builds (mockDebug, mockRelease, devDebug, devRelease, etc.)

Now my mockDebug variant needs a specific implementation of MyApplication.kt.

As I read here I can do this creating a class MyApplication in this path: src/mockDebug/java/package/MyApplication.kt

However I'm receiving an error in Android Studio saying "Redeclaration: MyApplication".

I'm sure I can solve this problem moving all debug/release MyApplication.kt implementations into

  • mockDebug
  • mockRelease
  • devDebug
  • devRelease
  • prodDebug
  • prodRelease

directories, but I don't understand why the documentation says it can be possible, even if I'm receiving that error

Thanks for helping me

like image 336
Giuseppe Giacoppo Avatar asked Jul 29 '19 09:07

Giuseppe Giacoppo


3 Answers

In my case deleting /build folder solved the issue.

like image 131
Michał Tajchert Avatar answered Sep 27 '22 18:09

Michał Tajchert


"Redeclaration: MyApplication" You see this above error as its present in the main and in your flavour or variant

approach should be class or file you want to change should not be in main makes copies of that file and add them to flavour or variant and do the change you want to see.

like image 30
Bipin Avatar answered Sep 27 '22 20:09

Bipin


You can try implementing your Gradle in this manner. In Build.gradle:

  buildTypes {
    release {
        debuggable false
        minifyEnabled true
        zipAlignEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        debuggable true
        zipAlignEnabled false
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

  productFlavors {
    mock {
        minSdkVersion 17
        applicationId 'com.test.mock'
        targetSdkVersion 23
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        versionCode 1
        versionName '1.0'
    }
    dev {
        minSdkVersion 17
        applicationId 'com.test.dev'
        targetSdkVersion 23
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        versionCode 1
        versionName '1.0'
    }
    prod {
        minSdkVersion 17
        applicationId 'com.test.prod'
        targetSdkVersion 23
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        versionCode 1
        versionName '1.0'
    }
}

So, now you can have the folder structure like:

  /src/mock/Application.kt
  /src/dev/Application.kt
  /src/prod/Application.kt

So once you build the Project, select the variant from BuildVariant tabs so it will take respective Application.kt.

Hope this will help to solve your problem.

like image 35
Appoorva Faldu Avatar answered Sep 27 '22 19:09

Appoorva Faldu