Siloé Bezerra Bispo
Siloé Bezerra Bispo

Reputation: 2244

Use a specific minSdkVersion only in a Debug mode

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
    }

Upvotes: 14

Views: 2862

Answers (5)

Quang Nhat
Quang Nhat

Reputation: 155

Try this

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

For more information about beforeVariants, can check this link

Upvotes: 0

lionscribe
lionscribe

Reputation: 3513

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.

Upvotes: 2

Roman Nazarevych
Roman Nazarevych

Reputation: 7703

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

Upvotes: 11

Pier Betos
Pier Betos

Reputation: 1048

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
    }
}

Upvotes: 3

M-Wajeeh
M-Wajeeh

Reputation: 17284

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
    }
}

Upvotes: 4

Related Questions