27 号早上到公司打开 As 就提示你有新版本 3.0 可以升级,于是乎果断升级。重启之后提示有新的Gradle 插件于是也果断升级,没想到升级之后出现一大堆错误。下面就介绍一下我遇到的问题,以及解决方案。
Cannot set the value of read-only property 'outputFile'
第一个报错就是这个,不能给只读的 outputFile
设置值
Cannot set the value of read-only property 'outputFile'for ApkVariantOutputImpl_Decorated{apkData=Main{ type=MAIN, fullName=googleplayDebug, filters=[]}}of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.复制代码
这是因为我使用了这种方式去修改生成的 apk 的文件名,这也是网上最常用的写法
applicationVariants.all { variant -> variant.outputs.each { output -> if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) { def flavorName = variant.flavorName.startsWith("_") ? variant.flavorName.substring(1) : variant.flavorName def apkFile = new File( output.outputFile.getParent(), "lovesy_${flavorName}_v${variant.versionName}_${buildTime()}.apk") output.outputFile = apkFile } }}复制代码
现在 outputFile
不给你设值了,使用上给的这种方式就行了
applicationVariants.all { variant -> variant.outputs.all { //outputFileName = "${variant.name}_${variant.versionName}_${buildTime()}.apk" } }}复制代码
Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List
如果你使用了 Butterknife 最新的插件的话就会报这个错
Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.Possible causes for this unexpected error include:
- Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)Re-download dependencies and sync project (requires network)
- The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.Stop Gradle build processes (requires restart)
- Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
在 github 上有专门的 ,我一个个的方法试了都试了一下,最后是把版本降到 8.4.0 就行了!!!
dependencies { classpath 'com.android.tools.build:gradle:3.0.0' classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'}复制代码
All flavors must now belong to a named flavor dimension
这是一个多渠道打包的错误
All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html复制代码
3.0 版本的 Gradle 插件需要给 flavors
指定 dimension
什么意思呢。
假如我们的 App 现在要分收费版和免费版,那么打渠道包的时候可能会这么写
productFlavors { yingyongbao_price { xxx } yingyongbao_free { xxx } baidu_price { xxx } baidu_free { xxx } ... //其它平台}复制代码
这么写也行但是不好,现在有更合理的写法,具体可以看
flavorDimensions 'free', 'notfree'productFlavors { yingyongbao { dimension 'free' xxx } baidu { dimension 'free' xxx } // 把收费和免费的区别写在下面这两块里就行了,最后它会帮我们自由组合 price { dimension 'notfree' xxx } free { dimension 'notfree' xxx }}复制代码
如果我们的 App 没有这类的区分,就简单了
flavorDimensions "default" //这里随便写一个默认值就行了productFlavors { yingyongbao {} baidu {}}复制代码
使用 implementation 和 api 代替 compile
在 Android Gradle plugin 3.0 中 compile
过期了,可以使用 implementation
和 api
代替。
api
和 compile
功能相同。
用 implementation
添加的依赖别的 module
依赖你它也使用不了你添加的依赖。
比如 module1依赖是这样的
dependencies { implementation 'com.android.support:appcompat-v7:27.0.0'}复制代码
module2的依赖是这样的
dependencies { implementation project(':module1')}复制代码
module2 依赖了 module1 但是使用不了 v7 包 ,如果想在 module2 中使用 v7 包那么在 module1 中可以这样添加依赖
dependencies { api 'com.android.support:appcompat-v7:27.0.0'}复制代码