博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Gradle Plugin 2.3.3 升级 3.0.0 遇到的问题
阅读量:6905 次
发布时间:2019-06-27

本文共 3760 字,大约阅读时间需要 12 分钟。

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:
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.复制代码

在 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 过期了,可以使用 implementationapi 代替。

apicompile 功能相同。

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'}复制代码

转载于:https://juejin.im/post/59f41fe2518825603b5830f4

你可能感兴趣的文章
《机器学习实战》中Logistic回归几个算法的解析
查看>>
设置交互要多久?最快只需三秒!
查看>>
我的友情链接
查看>>
页面loading效果
查看>>
关于SUID、SGID、SBIT
查看>>
高性能计算机的发展趋势
查看>>
理解ICMPv6前缀请求与前缀公告消息
查看>>
Bootstrap4从入门到精通
查看>>
安卓githup项目归纳
查看>>
Item 46: Use Built-in Algorithms and Data Structures
查看>>
UIkit框架(14)自定义标签控制器
查看>>
文件的读写
查看>>
夭折的TMG,微软停止开发TMG 相关产品
查看>>
坑- observeOn(AndroidSchedulers.mainThread())
查看>>
我的友情链接
查看>>
Vert.x 单元测试 译<六>
查看>>
Pyhton扫描端口脚本
查看>>
redis队列(set)
查看>>
ORA-01578和ORA-26040--NOLOGGING操作引起的坏块-错误解释和解决方案
查看>>
我的友情链接
查看>>