博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Gradle Plugin 2.3.3 升级 3.0.0 遇到的问题
阅读量:6903 次
发布时间: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

你可能感兴趣的文章
VS2010编译Boost 1.56
查看>>
mysql5.x(<7) sql文件导入到5.7
查看>>
常用正则表达式
查看>>
TortoiseSvn安装的时候,将svn的命令行工具单独隔离出来
查看>>
GitHub 入门教程
查看>>
《转载》脚本实现从客户端服务端HTTP请求快速分析
查看>>
C# 轻松实现对窗体(Form)换肤[转]
查看>>
subversion adobe-flashplugin
查看>>
开启服务和停止服务
查看>>
谎言与逻辑的故事三则
查看>>
【iCore双核心组合是开发板例程】【uCGUI 例程及代码包下载】
查看>>
Dell笔记本刷回低版本bios的方法
查看>>
《程序员面试宝典》之错误纰漏(持续更新。。。)
查看>>
【OpenCV-Python】Python Extension Packages for Windows
查看>>
UVA 10163 Storage Keepers(dp + 背包)
查看>>
mysql has gone away
查看>>
linux驱动开发---导出内核符号
查看>>
php多语言截取字符串函数
查看>>
android项目 之 记事本(12) ----- 图片的等比例缩放及给图片加入边框
查看>>
C#创建自己的扩展方法
查看>>