9

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

1
  • 1
    I'm having a similar problem. I need to use a different class for BuildType (debug and release), NOT productFlavor. But I'm getting "Redeclaration" class error on IDE. For now, I just ignore it because both assemble and bundle commands still working.
    – aldok
    Commented Apr 10, 2020 at 11:09

6 Answers 6

3

In my case deleting /build folder solved the issue.

2

"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.

1

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.

3
  • 2
    Hi, "MyApplication.kt" contains different logic based on build type (debug/release), not on variants (mock/dev/prod), so I don't want to refactor/copy this class to mockDebug, devDebug and prodDebug for the debug variant and mockRelease, devRelease and prodRelease for the release variant. I just want to maintain debug/release implementations, but I want to override it only for mockDebug combo. Android documentation says it is possible, but it doesn't work Commented Jul 29, 2019 at 9:41
  • Oh ok... So have you tried using Source sets in Gradle? Also, if you are using Source sets, then your /src/main/ -> Should not have Application.kt; folders should be like:- 1) /src/main/ <other files> 2) /src/debug/Application.kt 3) /src/release/Application.kt This way you can build the Gradle without duplicate error Commented Jul 29, 2019 at 9:49
  • 1
    I don't have any MyApplication.kt in main dir, only in debug and release, which are mutually exclusive. But when I create a third file in mockDebug directory, I receive that "Redeclaration" error both in debug and mockDebug MyApplication classes, as if there was a conflict between them Commented Jul 29, 2019 at 10:07
0

As the documentation specifies, you have to remove the class from the main package.

0

You should see only debug or release java folder as "sources root" unmark the one not active. In android studio, right click on Java folder -> mark directory as -> Unmark as Sources Root

0

As explained here, you can't do that. You need to use another source folder other than main.

Note: For a given build variant, Gradle throws a build error if it encounters two or more source set directories that have defined the same Kotlin or Java class. For example, when building a debug app, you can't define both src/debug/Utility.kt and src/main/Utility.kt, because Gradle looks at both these directories during the build process and throws a "duplicate class" error. If you want different versions of Utility.kt for different build types, each build type must define its own version of the file and not include it in the main/ source set.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.