14

How can I use a specific minSdkVersion only in a debug mode? I want to use the minSdkVersion 21 for debug mode, but minSdkVersion 15 for the release. I don't wanna use flavors for this because will give troubles.

I think the could be like this (but don't work):

defaultConfig {
        applicationId "blacktoad.com.flapprototipo"
        minSdkVersion 15

        debug{
            minSdkVersion 21
        }

        targetSdkVersion 23
        versionCode 42
        versionName "1.72"

        multiDexEnabled true
    }
Siloé Bezerra Bispo
  • 2,056
  • 1
  • 17
  • 31

5 Answers5

11

There exist a better way to do it without Product Flavors or hooking into the build flow. Example:

  buildTypes {
     debug {
       defaultConfig.minSdkVersion 19
       defaultConfig.vectorDrawables.useSupportLibrary = true
       defaultConfig.multiDexEnabled = true
     ...
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
  • 1
    This looks like it would change the default for all build types. It might be working for you, but it's effectively a race condition during the gradle configure stage. If you reorder the build.gradle file, it could stop working at any time. Unless I'm missing something? – Hamy May 08 '20 at 14:39
  • 2
    Doesn't work. Like @Hamy said - it applied the new minSdkVersion to all build types. – sagis Oct 16 '20 at 08:53
  • This is wrong. It references the defaultConfig function of the android block. The ApplicationBuildType scope itself does not provide defaultConfig. – FalloutBoy May 24 '22 at 22:13
3

I don't wanna use flavors for this because will give troubles.

I don't think you can do it without productFlavors. I am leaving this answer here for others who have no issue using productFlavors. https://developer.android.com/studio/build/multidex.html#dev-build

Basically all you need to do is declare productFlavors with different minSdkVersion:

productFlavors {
    dev {
        // Enable pre-dexing to produce an APK that can be tested on
        // Android 5.0+ without the time-consuming DEX build processes.
        minSdkVersion 21
    }
    prod {
        // The actual minSdkVersion for the production version.
        minSdkVersion 14
    }
}
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
3

It took me almost a day to create this script. Please take note of this for keepsake, but only use this as a last resort.

android.applicationVariants.all { variant ->

    //Making specific variant disablements for faster build
    if (variant.buildType.name.contains("debug")) {
        println "Making min version to 21 and disabling multidex"
        variant.mergedFlavor.setMultiDexEnabled false

        def versionMin = new com.android.builder.core.DefaultApiVersion(21)
        variant.mergedFlavor.setMinSdkVersion versionMin
    }
}
Pier Betos
  • 1,038
  • 9
  • 17
2

I know this is an old question, but there is a very simple solution, even without setting up separate flavors.
Just replace the line;

minSdkVersion 21

with

minSdkVersion gradle.startParameter.taskNames.toString().contains("Release")?16:21

and viola, it will work.

lionscribe
  • 3,413
  • 1
  • 16
  • 21
0

Try this

androidComponents {
    beforeVariants(selector().withName("debug")) { variantBuilder ->
        variantBuilder.minSdk = 21
}}

For more information about beforeVariants, can check this link

Quang Nhat
  • 135
  • 1
  • 8