博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
Pyhton扫描端口脚本
查看>>
redis队列(set)
查看>>
ORA-01578和ORA-26040--NOLOGGING操作引起的坏块-错误解释和解决方案
查看>>
我的友情链接
查看>>
解决eclipse创建项目,jdk版本太旧问题
查看>>
架构师进阶:Linux进程间如何共享内存?
查看>>
笨方法学Python,Lesson 26
查看>>
笨方法学python Lesson 45
查看>>
Java HashMap的实现原理
查看>>
服务器的发送数据
查看>>
kvm install 报错could not open disk imageXXX: Permission denied
查看>>
lduan office 365 自定义域的添加和配置二
查看>>
在Wordpress侧栏中使用下拉菜单显示分类
查看>>
基础排序算法 – 选择排序Selection sort
查看>>
Appium移动自动化测试环境部署
查看>>
corosync+pacemaker+crm实现drbd高可用
查看>>
Git Fork和PullRequest
查看>>
springBoot2.x设置quartz的overwriteExistingJobs参数
查看>>
VMware中通过克隆的Centos7,网卡突然没了
查看>>
学习笔记 DNS 子域授权 view
查看>>