升级到 Android Studio 3.0 + Gradle 4.1 遇到的一些坑及解决方案

本文解决Android Gradle Plugin 3.0及更高版本中常见的三个问题:设置只读属性outputFile的错误、所有flavor必须属于同一维度的错误以及执行Java预编译任务失败的错误。提供了详细的解决方案。
摘要由CSDN通过智能技术生成

问题一:

Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=commonDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl. Open File

解决方案一:

https://stackoverflow.com/questions/44239235/android-gradle-3-0-0-alpha2-plugin-cannot-set-the-value-of-read-only-property

I used this code

 applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        //输出apk名称为:应用名.apk
                        def fileName = "xxx.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }

**Use all() instead of each()
Use outputFileName instead of output.outputFile if you change only file name (that is your case)**

Example from the guide:

// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.

android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "xxx.apk"
                }
            }

问题二:

Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

解决方案二:

http://blog.csdn.net/syif88/article/details/75009663

大致是说,Plugin 3.0.0之后有一种自动匹配消耗库的机制,便于debug variant 自动消耗一个库,然后就是必须要所有的flavor 都属于同一个维度。
为了避免flavor 不同产生误差的问题,应该在所有的库模块都使用同一个foo尺寸。

但是我们从中已经知道解决方案了:

在主 app 的 build.gradle 里面的

defaultConfig {
 targetSdkVersion:***
 minSdkVersion :***
 versionCode:***
 versionName :***

//版本名后面加句话,意思是flavor dimension 它的维度就是该版本号,
//这样维度就是都是统一的了

 **flavorDimensions "versionCode"**

}

问题三:

Error:Execution failed for task ':youyoubao:javaPreCompileCommonDebug'.
> Annotation processors must be explicitly declared now.  The following dependencies on the compile classpath are found to contain annotation processor.  Please add them to the annotationProcessor configuration.
    - butterknife-compiler-8.6.0.jar (com.jakewharton:butterknife-compiler:8.6.0)
  Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior.  Note that this option is deprecated and will be removed in the future.
  See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.

解决方案三:

在 app 下的 build.gradle 的 defaultConfig 中加一句话:

defaultConfig {
   ...
   javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值