【问题标题】:How to control multiDex and minifying from product flavors?如何控制 multiDex 和缩小产品风味?
【发布时间】:2016-12-16 13:36:01
【问题描述】:

我正在使用以下方式来改进 Android 5 及更高版本设备的调试版本:

productFlavors {
    // Define separate dev and prod product flavors.
    dev {
      // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
      // to pre-dex each module and produce an APK that can be tested on
      // Android Lollipop without time consuming dex merging processes.
      minSdkVersion 21
    }
    prod {
      // The actual minSdkVersion for the application.
      minSdkVersion 16
    }
  }

但并不是我所有的设备都在 api 21+ 下运行,因此我想控制 multiDex 和缩小。例如:

  productFlavors {
    dev {
      minSdkVersion 21
      multiDexEnabled false
      minifyEnabled false
    }
    prod {
      minSdkVersion 16
      multiDexEnabled true
      minifyEnabled true
    }
  }

但这给了我:

Error:(44, 0) Could not find method minifyEnabled() for arguments [false] on ProductFlavor_Decorated

如何将这些属性组合在一起?

【问题讨论】:

    标签: android android-studio gradle android-productflavors android-build-flavors


    【解决方案1】:

    minifyEnabled() 属性仅在BuildType DSL 对象中可用。而multiDexEnabled 指的是ProductFlavor 对象。所以如果你想组合这些属性,你必须在这两个对象中指定它。例如:

    productFlavors {
        dev {
            minSdkVersion 21
            multiDexEnabled false
        }
        prod {
            minSdkVersion 16
            multiDexEnabled true
        }
    }
    
    buildTypes {
        debug {
            minifyEnabled false
        }
        release {
            minifyEnabled true
        }
    }
    

    对于调试版本,使用 devDebug 构建变体,对于发布版本 - prodRelease

    【讨论】:

      【解决方案2】:

      正如@maxost
      所建议的那样 就在下面,我提供了 android 开发者链接
      参考这里 [https://developer.android.com/studio/build/multidex.html#dev-build]:

      android {
                  defaultConfig {
                      ...
                      multiDexEnabled true
                  }
                  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
                      }
                  }
                  buildTypes {
                      release {
                          minifyEnabled true
                          proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                               'proguard-rules.pro'
                      }
                  }
              }
              dependencies {
                  compile 'com.android.support:multidex:1.0.1'
              }
      

      【讨论】:

        猜你喜欢
        • 2016-08-05
        • 2017-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-27
        • 1970-01-01
        • 1970-01-01
        • 2013-06-07
        相关资源
        最近更新 更多