gradle 7.x 中的一些变更

发布时间 2023-04-08 16:34:51作者: SharpCJ

根目录下 build.gradle 变更

变更前:

buildscript {
    ext.kotlin_version = '1.5.0'

    repository {
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
        classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.17"

        // 自定义 gradle 插件
        classpath "com.sharpcj.plugin:abc:1.0.6"
    }
}


allprojects {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "http://xx.xx.xx.xx:xxxx/xx/xx/"
        }
    }    
}

变更后:

根目录下的 buildscript 变更到 settings.gradle 中

setting.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven {
            allowInsecureProtocol = true
            url "http://xx.xx.xx.xx:xxxx/xx/xx/"
        }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        maven {
            allowInsecureProtocol = true
            url "http://xx.xx.xx.xx:xxxx/xx/xx/"
        }
    }
}

gradle 7.0.x 以上对于 http 协议的的仓库地址,需要显示声明:allowInsecureProtocol = true

根目录下 build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

// 全局 buildescript 依旧可以用
buildscript {
    ext.kotlin_version = '1.5.0'

    // 自定义 gradle 插件
    dependencies {
        classpath "com.sharpcj.plugin:abc:1.0.6"
    }
}

plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
    id 'com.google.protobuf' version '0.8.17' apply false
}

引入 aar 包gradle

7.0.x 以前:

  1. 将aar文件复制到libs文件夹中

  2. 在 model 下 build.gradle 中的 android {} 外层添加:

repositories {
    flatDir {
        dirs 'libs'
    }
}
  1. 在 dependencies 中加入
implementation files('libs/xxx.jar')

或者:

implementation(fileTree("libs"));

7.0.x 以上:

  1. 将aar文件复制到libs文件夹中
  2. build.gradle的dependencies中加入:
implementation(fileTree("libs"));