一、最新 Gradle 中配置插件依赖的变化



当前最新的 Android Studio 开发环境 , 生成的 Gradle 配置脚本使用了最新 API , 用起来不太习惯 ;

根目录下的 build.gradle 构建脚本变成了下面的样式 , 单纯的用于配置 Android 应用编译所需插件的 插件 和 版本 ;

// Top-level build file where you can add configuration options common to all sub-projects/modules.
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
}

原来应用中配置插件 , 是在 根目录下的 build.gradle 中的 buildscript / dependencies 中配置编译过程中所需的插件 ;

这种方式目前已经淘汰了 ;

buildscript {
    repositories {
        google()
        mavenCentral()
        jcenter()
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven{
            url 'https://maven.aliyun.com/repository/google/'
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.3.1"
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

这里说明一下插件关系 , 导入了

classpath "com.android.tools.build:gradle:7.3.1"

插件 , 就相当于导入了 最新 Gradle 配置中的

    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false

这两个插件 , 一个是 Android 应用编译所需的插件 , 一个是 Android 依赖库编译所需的插件 ;


导入的

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20'

插件 是 Kotlin 语言插件 , 如果在 开发中使用 Kotlin 进行开发 , 就必须导入该插件 ,

对应最新 Gradle 配置中的

id 'org.jetbrains.kotlin.android' version '1.7.20' apply false

插件 ;





二、报错信息



现在有一个需求 , 就是在 Navigation 组件开发 界面跳转 时 , Bundle 数据传递是类型不安全的 , 这里需要进行 安全数据传递 ,

需要导入

classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha06'

中的

id 'androidx.navigation.safeargs' version '2.3.0-alpha06' apply false

插件 , 前者是下载地址 , 后者是真实导入的 插件名称 和 插件版本号 ;


尝试在根目录中配置 androidx.navigation.safeargs 插件 ,

// Top-level build file where you can add configuration options common to all sub-projects/modules.
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 'androidx.navigation.safeargs' version '2.3.0-alpha06' apply false
}

结果报如下错误 : 提示找不到 2.3.0-alpha06 版本的 androidx.navigation.safeargs 插件 ;

Build file 'D:\002_Project\002_Android_Learn\Navigation\build.gradle' line: 6

Plugin [id: 'androidx.navigation.safeargs', version: '2.3.0-alpha06', apply: false] was not found in any of the following sources:

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'androidx.navigation.safeargs', version: '2.3.0-alpha06', apply: false] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'androidx.navigation.safeargs:androidx.navigation.safeargs.gradle.plugin:2.3.0-alpha06')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    Google
    MavenRepo
	at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolveToFoundResult(DefaultPluginRequestApplicator.java:243)




三、增加 Maven 仓库源



在 settings.gradle 中的 pluginManagement / repositories 中增加 jcenter 和 阿里云的自定义源 , 这个源配置已经很全了 , 基本上能解决所有的问题 ;

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        jcenter()
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven{
            url 'https://maven.aliyun.com/repository/google/'
        }
    }
}

使用上述源 , 还是报错 ,

Build file 'D:\002_Project\002_Android_Learn\Navigation\build.gradle' line: 6

Plugin [id: 'androidx.navigation.safeargs', version: '2.3.0-alpha06', apply: false] was not found in any of the following sources:

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'androidx.navigation.safeargs', version: '2.3.0-alpha06', apply: false] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'androidx.navigation.safeargs:androidx.navigation.safeargs.gradle.plugin:2.3.0-alpha06')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    Google
    MavenRepo
    BintrayJCenter
    maven(https://maven.aliyun.com/repository/public/)
    maven2(https://maven.aliyun.com/repository/google/)
	at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolveToFoundResult(DefaultPluginRequestApplicator.java:243)

【错误记录】Android Studio 中最新的 Gradle 配置中设置插件依赖 ( 2023 年 8 月 24 日 | 最新 Gradle 中配置插件依赖的变化 | 增加 Maven 仓库源 )-LMLPHP





五、使用老版本方式导入插件



使用上述 源 还是无法下载 androidx.navigation.safeargs 插件 , 这里暂时不在这个方面进行尝试了 , 不使用 plugins 新方式导入插件 ;

id 'androidx.navigation.safeargs' version '2.3.0-alpha06' apply false

使用之前的老版本的导入 编译插件 的方法 ;

首先 , 将整个 build.gradle 中 配置 plugins 插件的内容全部注释掉 ;

【错误记录】Android Studio 中最新的 Gradle 配置中设置插件依赖 ( 2023 年 8 月 24 日 | 最新 Gradle 中配置插件依赖的变化 | 增加 Maven 仓库源 )-LMLPHP
然后 , 在 settings.gradle 中添加如下代码 , 这是老版本的方式导入编译时依赖库 ;

buildscript {
    repositories {
        google()
        mavenCentral()
        jcenter()
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        maven{
            url 'https://maven.aliyun.com/repository/google/'
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.3.1"
        classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha06'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

上面的 dependencies 中的三个依赖 , 与下面的四个插件是对应的 , com.android.tools.build:gradle:7.3.1 包含着 com.android.applicationcom.android.library 两个插件 ;

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 'androidx.navigation.safeargs' version '2.3.0-alpha06' apply false
}

然后再进行编译 , 即可编译通过 ;

【错误记录】Android Studio 中最新的 Gradle 配置中设置插件依赖 ( 2023 年 8 月 24 日 | 最新 Gradle 中配置插件依赖的变化 | 增加 Maven 仓库源 )-LMLPHP


完整的代码可查看 【Jetpack】Navigation 导航组件 ④ ( Fragment 跳转中使用 safe args 安全传递参数 ) https://hanshuliang.blog.csdn.net/article/details/131406972 中的博客源码快照 ;

这是开发 Navigation 导航组件时遇到的报错问题 ;

08-25 07:00