一、最新 Gradle 中配置插件依賴的變化
當(dāng)前最新的 Android Studio 開發(fā)環(huán)境 , 生成的 Gradle 配置腳本使用了最新 API , 用起來不太習(xí)慣 ;
根目錄下的 build.gradle 構(gòu)建腳本變成了下面的樣式 , 單純的用于配置 Android 應(yīng)用編譯所需插件的 插件 和 版本 ;
// 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
}
原來應(yīng)用中配置插件 , 是在 根目錄下的 build.gradle 中的 buildscript / dependencies 中配置編譯過程中所需的插件 ;
這種方式目前已經(jīng)淘汰了 ;
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
}
}
這里說明一下插件關(guān)系 , 導(dǎo)入了
classpath "com.android.tools.build:gradle:7.3.1"
插件 , 就相當(dāng)于導(dǎo)入了 最新 Gradle 配置中的
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
這兩個(gè)插件 , 一個(gè)是 Android 應(yīng)用編譯所需的插件 , 一個(gè)是 Android 依賴庫編譯所需的插件 ;
導(dǎo)入的
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20'
插件 是 Kotlin 語言插件 , 如果在 開發(fā)中使用 Kotlin 進(jìn)行開發(fā) , 就必須導(dǎo)入該插件 ,
對(duì)應(yīng)最新 Gradle 配置中的
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
插件 ;
二、報(bào)錯(cuò)信息
現(xiàn)在有一個(gè)需求 , 就是在 Navigation 組件開發(fā) 界面跳轉(zhuǎn) 時(shí) , Bundle 數(shù)據(jù)傳遞是類型不安全的 , 這里需要進(jìn)行 安全數(shù)據(jù)傳遞 ,
需要導(dǎo)入
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha06'
中的
id 'androidx.navigation.safeargs' version '2.3.0-alpha06' apply false
插件 , 前者是下載地址 , 后者是真實(shí)導(dǎo)入的 插件名稱 和 插件版本號(hào) ;
嘗試在根目錄中配置 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
}
結(jié)果報(bào)如下錯(cuò)誤 : 提示找不到 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 和 阿里云的自定義源 , 這個(gè)源配置已經(jīng)很全了 , 基本上能解決所有的問題 ;
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
jcenter()
maven {
url 'https://maven.aliyun.com/repository/public/'
}
maven{
url 'https://maven.aliyun.com/repository/google/'
}
}
}
使用上述源 , 還是報(bào)錯(cuò) ,
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)
五、使用老版本方式導(dǎo)入插件
使用上述 源 還是無法下載 androidx.navigation.safeargs 插件 , 這里暫時(shí)不在這個(gè)方面進(jìn)行嘗試了 , 不使用 plugins 新方式導(dǎo)入插件 ;
id 'androidx.navigation.safeargs' version '2.3.0-alpha06' apply false
使用之前的老版本的導(dǎo)入 編譯插件 的方法 ;
首先 , 將整個(gè) build.gradle 中 配置 plugins 插件的內(nèi)容全部注釋掉 ;
然后 , 在 settings.gradle 中添加如下代碼 , 這是老版本的方式導(dǎo)入編譯時(shí)依賴庫 ;
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 中的三個(gè)依賴 , 與下面的四個(gè)插件是對(duì)應(yīng)的 , com.android.tools.build:gradle:7.3.1
包含著 com.android.application
和 com.android.library
兩個(gè)插件 ;
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
}
然后再進(jìn)行編譯 , 即可編譯通過 ;
完整的代碼可查看 【Jetpack】Navigation 導(dǎo)航組件 ④ ( Fragment 跳轉(zhuǎn)中使用 safe args 安全傳遞參數(shù) ) https://hanshuliang.blog.csdn.net/article/details/131406972 中的博客源碼快照 ;文章來源:http://www.zghlxwxcb.cn/news/detail-732883.html
這是開發(fā) Navigation 導(dǎo)航組件時(shí)遇到的報(bào)錯(cuò)問題 ;文章來源地址http://www.zghlxwxcb.cn/news/detail-732883.html
到了這里,關(guān)于【錯(cuò)誤記錄】Android Studio 中最新的 Gradle 配置中設(shè)置插件依賴 ( 2023 年 8 月 24 日 | 最新 Gradle 中配置插件依賴的變化 | 增加 Maven 倉庫源 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!