Gradle从2.1升级到3.3具体的一些坑

最近维护原有的老项目的时候,发现gradle版本居然是2.1版本,难怪我说编译需要花费5分钟!!!尤其是现在的gradle稳定版本已经4.1+的情况下,显得更慢。

本着时间就是金钱的理念,开启了我的升级填坑之旅!、

当然升级不能走的太快,太快容易扯着蛋。稳妥起见,决定先升级到3.3。(其实是公司其他项目都用3.3版本,我也很想升级到最新版本,体验快的感觉啊)

第一步就是在最外层的build.gradle文件中修改版本号。

classpath 'com.android.tools.build:gradle:2.1.0'

神马,版本号怎么对应?

谷歌官方版本对应说明,需要翻墙哦

对应下来,gradle3.3版本就是2.3.3

第一个坑

Error:A problem was found with the configuration of task ':app:packageRelease'.  
> File 'E:\project-gitosc\pet\Pet-Android\Pet-Bulter\app\build\intermediates\res\resources-release-stripped.ap_' specified for property 'resourceFile' does not exist.
  • 原因:build.gradle(app)文件中,代码混淆和移除无用的资源文件有一个为false。

  • 解决方案:两者都必须为true

//启用代码混淆  
minifyEnabled true  

//移除无用的资源文件  
shrinkResources true

第二个坑

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 
  • 原因:

Plugin 3.0.0之后有一种自动匹配消耗库的机制,便于debug variant 自动消耗一个库,然后就是必须要所有的flavor 都属于同一个维度。 为了避免flavor 不同产生误差的问题,应该在所有的库模块都使用同一个foo尺寸。具体的信息内容可以去看错误提示中的官网消息。

  • 解决方案:
在主app的build.gradle里面的
 defaultConfig {
 targetSdkVersion:***
 minSdkVersion :***
 versionCode:***
 versionName :***
//版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
flavorDimensions "versionCode"
}

第三个坑

Unable to load class ‘org.gradle.api.internal.component.Usage’.
  • 原因

novoda:bintray 版本老旧。

  • 解决方案

修改项目根目录下的build.gradle的版本号

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        ...
        classpath 'com.novoda:bintray-release:0.5.0'//修改此处版本号为 0.5.0---修改之前是0.3.4
        ...
    }
}

第四个坑

Error:Could not resolve all files for configuration ':app:xxxxxxxDebugCompileClasspath'.

Failed to transform file 'commons-lang-2.4.jar' to match attributes {artifactType=android-classes} using transform JarTransform Transform output file xxxxxxx/xxxxxxx/xxxxxxx/app/commons-lang-2.4.jar does not exist.
  • 原因 jar包寻找不到。

仔细一看项目中引入jar包的方式还是很古老的

compile files(‘libs/mta-sdk-1.6.2.jar’)

新版本不识别。

  • 解决方案

删除原有引入方式,重新引入

compile fileTree(include: [’*.jar’], dir: ‘libs’)

第五个坑

Cannot set the value of read-only property ‘outputFile’ ) 
Error:(56, 0) Cannot set the value of read-only property ‘outputFile’ for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=debug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImpl.
  • 原因,

自定义输出apk名字的语法出现改变。

  • 解决方案。如下修改。
//            applicationVariants.all { variant ->
//                variant.outputs.each { output ->
//                    def outputFile = output.outputFile
//                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
//                        def fileName = "你自定义.apk"
//                        output.outputFile = new File(outputFile.parent, fileName)
//                    }
//                }
//            }
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "你自定义.apk"
                }
            }