AndroidStudio3.2遇上gradle3.2.1构建升级填坑

102 阅读3分钟

文章目录

问题概述

过完年后,公司项目突然跑不起来了,报了各种错误,内心真的是万马奔腾,一番云雨之后,痛定思痛,忽然想起来了去年一年每次打开as的那句关于2018年底即将弃用complie的提示语,由于种种原因和考虑,为了求稳,项目就一直没有进行替换,过完年后果然不能用了,导致用complie依赖的全部三方库都有问题,于是狠了狠心,升级了gradle插件版本,之前是2.3,直接升级到了3.2,把主module中的complie换成implementation,把功能moudle中的complie替换成了api,以为这样就可以了,我还是太年轻啊!升级gradle后有更多问题需要解决,最主要的就是jar包重复和冲突问题,这个在低版本的gradle中式不存在的,随着开发要求越来越高,越来越规范,现在,不允许了。

exclude去除项目中gradle重复依赖

首先要查看主module中的依赖树,查看有哪些依赖是重复的
>Gradle 默认开启了 依赖传递 意思就是 项目依赖了A,A又依赖了B和C,这时候,我们只需要写一行代码:implementation A就行了,由传递依赖导致的冲突,默认是以最高版本的依赖为准,查看整个项目的依赖传递关系

  1. x.x.x (*) 该依赖已经有了,将不再重复依赖。
  2. x.x.x -> x.x.x 该依赖的版本被箭头所指的版本代替。
  3. x.x.x -> x.x.x(*) 该依赖的版本被箭头所指的版本代替,并且该依赖已经有了,不再重复依赖。

exclude排除所有

在主module中build.gradle根节点下,剔除所有重复依赖

configurations{
    all*.exclude module: "support-fragment"
    all*.exclude module: "support-v4"
    all*.exclude module: "support-compat"
    all*.exclude module: "animated-vector-drawable"
    all*.exclude module: "support-vector-drawable"
    all*.exclude module: "appcompat-v7"
}

exclude排除部分(逐个排除)

更改原有依赖的语法结构,单个剔除重复依赖

implementation ('com.haibin:calendarview:3.2.8'){
            exclude group: 'com.android.support', module: 'recyclerview-v7'
            exclude group: 'com.android.support', module: 'support-compat'
            exclude group: 'com.android.support', module: 'support-core-ui'
            exclude group: 'com.android.support', module: 'support-media-compat'
            exclude group: 'com.android.support', module: 'support-core-utils'
            exclude group: 'com.android.support', module: 'support-fragment'
        }

Force 强制指定

在主module中build.gradle根节点下,强制指定重复依赖的版本号

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-fragment:27.1.1'
        force 'com.android.support:support-v4:27.1.0'
        force 'com.android.support:support-vector-drawable:27.1.1'
        force 'com.android.support:animated-vector-drawable:27.1.1'
        force 'com.android.support:appcompat-v7:27.1.1'
    }
}

使用gradle生成指定名称的apk包

在buildType中gradle2.3版本语法生成指定名称apk包

//        android.applicationVariants.all { variant ->
//            variant.outputs.each { output ->
//                def outputFile = output.outputFile
//                if (outputFile != null && outputFile.name.endsWith('.apk')) {
//                    def fileName = outputFile.name.replace("athesismentor-release.apk", "kzxt_student_${defaultConfig.versionName}_${getDate()}.apk")
//                    output.outputFile = new File(outputFile.parent, fileName)
//                }
//            }
//        }

buildType中graldle 3.2.1生成指定名称apk包

  android.applicationVariants.all { variant ->
            variant.outputs.all {
//                outputFileName = "app_${variant.flavorName}_V${defaultConfig.versionName}_${variant.buildType.name}.apk"
                outputFileName = "kzxt_student_${defaultConfig.versionName}_${getDate()}.apk"
            }
        }

build.gradle根节点下获取时间戳

//获取时间戳
static def getDate() {
    def date = new Date()
    def formattedDate = date.format('MMddHHmm')
    return formattedDate
}

构建异常处理

  1. com.android.builder.dexing.DexArchiveBuilderException: Failed to process

在工程 gradle.properties中添加android.enableD8=true 解决

  1. references to other resources are not supported by build-time PNG generation

解决方法:

defaultConfig{    
   vectorDrawables.useSupportLibrary = true    
}  
  1. Invoke-customs are only supported starting with android 0 --min-api 26

解决:在build.gradle的android节点下添加

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }