Use a specific minSdkVersion only in a Debug mode

无人久伴 提交于 2019-12-10 12:44:36

问题


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
    }

回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/39594317/use-a-specific-minsdkversion-only-in-a-debug-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!