Android Studio 新项目模板使用插件而非依赖项

Android Studio New Project Template Uses Plugins not Dependencies

如何将 safeargs 和 secrets 添加为插件,或者必须保留为依赖项?

新片段 + 视图模型的最新 Kotlin 模板 build.gradle(项目)使用插件 {} 而不是依赖项 {} 这是我的解决方法,如何使用 path:plugin:version 插件 ID 转换类路径。

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
        // not compatible with upgraded gradle classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0"
        //
        // Update this line to use 2.5.0-alpha01
        // While Navigation 2.4.1 is not out yet
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0-alpha01"
        classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.0"
    }
}
// buildscript MUST come before this these are used in the project build.gradle not a dependency
/* This came with the 'new project template' replaced with old style dependencies,
plugins {
    id 'com.android.application' version '7.1.0' apply false
    id 'com.android.library' version '7.1.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
} */

模块的build.gradle不一样,没有版本,很容易转换。

不要将 repositories 块添加到项目级别 build.gradle 文件中的 buildscript。 它应该在项目 settings.gradle 文件中。

这是我的解决方法:

  • build.gradle(Project)

     // Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
    
     dependencies {
    
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
         classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
         classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0-alpha02"
     }
    }
    
    plugins {
     id 'com.android.application' version '7.1.1' apply false
     id 'com.android.library' version '7.1.1' apply false
     id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    }
    
    task clean(type: Delete) {
     delete rootProject.buildDir
    }
    
  • settings.gradle(Project Settings) 存储库在这里声明。

    pluginManagement {
     repositories {
         gradlePluginPortal()
         google()
         mavenCentral()
     }
    }
    dependencyResolutionManagement {
     repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
     repositories {
         google()
         mavenCentral()
     }
    }
    
    rootProject.name = "Dictionary App"
    include ':app'
    

假设这些存储库在 settings.gradle 中:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

可以将 buildscript 块替换为:

plugins {
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id 'androidx.navigation.safeargs' version '2.5.0-alpha02' apply false
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.0' apply false
}

可能有或没有 -gradle-plugin 后缀。