广告
返回顶部
首页 > 资讯 > 移动开发 >【错误记录】Android Studio 中最新的 Gradle 配置中设置插件依赖 ( 2023 年 8 月 24 日 | 最新 Gradle 中配置插件依赖的变化 | 增加 Maven 仓库源 )
  • 592
分享到

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

androidstudiomavenandroidgradleplugins原力计划 2023-09-12 13:09:31 592人浏览 独家记忆
摘要

文章目录 一、最新 Gradle 中配置插件依赖的变化二、报错信息三、增加 Maven 仓库源五、使用老版本方式导入插件 一、最新 Gradle 中配置插件依赖的变化 当前最新

文章目录





一、最新 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: 6Plugin [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    MavenRepoat 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: 6Plugin [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)

在这里插入图片描述





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



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

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

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

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

在这里插入图片描述
然后 , 在 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}

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

在这里插入图片描述


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

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

来源地址:https://blog.csdn.net/han1202012/article/details/132479213

--结束END--

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

本文链接: https://www.lsjlt.com/news/404542.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作