【问题标题】:Use a specific minSdkVersion only in a Debug mode仅在调试模式下使用特定的 minSdkVersion
【发布时间】:2016-09-20 12:37:11
【问题描述】:

如何仅在调试模式下使用特定的 minSdkVersion? 我想将 minSdkVersion 21 用于调试模式,但将 minSdkVersion 15 用于发布。我不想为此使用风味,因为会带来麻烦。

我认为可能是这样的(但不工作):

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

        debug{
            minSdkVersion 21
        }

        targetSdkVersion 23
        versionCode 42
        versionName "1.72"

        multiDexEnabled true
    }

【问题讨论】:

标签: android gradle android-min-sdk


【解决方案1】:

没有Product Flavors 或挂钩到构建流程的情况下,存在更好的方法。 示例:

  buildTypes {
     debug {
       defaultConfig.minSdkVersion 19
       defaultConfig.vectorDrawables.useSupportLibrary = true
       defaultConfig.multiDexEnabled = true
     ...

【讨论】:

  • 这看起来会改变所有构建类型的默认值。它可能对您有用,但它实际上是 gradle 配置阶段的竞争条件。如果您重新排序 build.gradle 文件,它可能随时停止工作。除非我错过了什么?
  • 不起作用。就像@Hamy 所说的那样——它将新的 minSdkVersion 应用于所有构建类型。
【解决方案2】:

我不想为此使用风味,因为会带来麻烦。

如果没有productFlavors,我认为你做不到。我将把这个答案留给其他使用productFlavors 没有问题的人。 https://developer.android.com/studio/build/multidex.html#dev-build

基本上你需要做的就是用不同的minSdkVersion声明productFlavors

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】:

    我花了将近一天的时间来创建这个脚本。请注意这一点作为纪念品,但仅将其用作最后的手段

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

    【讨论】:

      【解决方案4】:

      我知道这是一个老问题,但有一个非常简单的解决方案,即使没有设置单独的口味。
      只需更换行;

      minSdkVersion 21
      

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

      还有中提琴,它会起作用的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-20
        • 2015-09-23
        • 2012-07-09
        • 1970-01-01
        相关资源
        最近更新 更多