No release source set

415 Views Asked by At

I'm trying to make my application have different behaviour for the "debug/release" build types. According to the documentation, I should first make sure that I have 2 build types, which I do:

build.gradle.kts

android {
    buildTypes {
        release {
            isMinifyEnabled = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
}

Android Studio showing build and release build variants

Next, I should create the source set directories for "build" and "debug" variant. While the "debug" variant is in the list, the "release" source set is not there, this is what I have:

  • main
  • debug
  • androidTest
  • androidTestDebug
  • test
  • testDebug

It seems like the "release" build type is omitted altogether. Trying to configure the "release" type to have source sets also doesn't work, as typing this. in the release {} lambda in build.gradle.kts does not show any source-set related entries.

I am aware there's this other related question, but that uses Groovy Gradle, not Kotlin, so it doesn't help.

How can I make the "release" source set appear and, if that's not possible, how can I write code that is specific to the "release" build type?

1

There are 1 best solutions below

0
Chaoz On

Apparently, the source sets do exist, Android Studio just doesn't display them.

You can verify this by running ./gradlew sourceSets:

[...]

main
----
Compile configuration: compile
build.gradle name: android.sourceSets.main
Java sources: [frontend\src\main\java]
Kotlin sources: [frontend\src\main\kotlin, frontend\src\main\java]
[...]

release
-------
Compile configuration: releaseCompile
build.gradle name: android.sourceSets.release
Java sources: [frontend\src\release\java]
Kotlin sources: [frontend\src\release\kotlin, frontend\src\release\java]
[...]

All the source sets are there, but if the current build type is set to "debug", Android Studio won't acknowledge they exist. Change the build type to "release", let it sync, and then try to create the folders again - it will work. Sadly, tho, this will make the "debug" folders disappear (or, if you switch to 'Project' view from 'Android', it will display them, but it will no longer recognize files inside as source files).

So, if you're looking to make changes to both simultaneously, that's impossible as far as I can tell, as you will not get any intelligent coding features for the un-selected build type.