繁体   English   中英

新版Android Studio如何添加classpath

[英]How to add classpath in new version of Android Studio

我将我的 android 工作室版本更新为大黄蜂版本。
现在我想将导航组件添加到我的项目中。
我想将类路径添加到 gradle,但是这个文件 gradle 已经更改,我不知道如何添加它。

我想将这个classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")添加到 gradle 个文件中!

我的项目 gradle 文件是:

    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
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

如何将此类路径添加到应用程序 gradle?!

在 2022 年 2 月的最新版本中,不再需要添加buildscript ,只需在项目的build.gradle中添加 id。 项目的build.gradle中将是这样的:

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.4.1' apply false
    // classpath are changed, so no need to add classpath anymore just the id and the version
}

不要忘记在build.gradle模块中再次添加 id,

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'androidx.navigation.safeargs'
}

他们没有在发布文档中讨论过,而是在 plugins 块上方手动添加一个 buildscript 块,然后在 buildscript 块内添加一个 depedencies 块。

像这样:

buildscript {

        dependencies {

              classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0")

                     }
       }


plugins {
      id 'com.android.application' version '7.1.0-rc01' apply false
      //......
    }

所以他们在 build.Gradle 文件中提到了作为顶级评论我认为这是一种不好的做法,不要提到它
1-打开您的构建。Gradle(应用程序模块)滚动到顶部,直到看到该注释 // 顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

然后把这段代码作为副本放它这一行是在此处输入图像描述 builtin comment // 顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。

构建脚本 {

dependencies {

    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:lasted-version"

}

}

您不需要 buildscript。您可以在插件组件中添加“id”。我在刀柄集成中遇到了它。这是示例顶级 gradle

plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'com.google.dagger.hilt.android' version '2.43.2' apply false
}


task clean(type: Delete) {
    delete rootProject.buildDir
}

和二级(项目级gradle)

plugins {
    id 'com.android.application'
    id 'com.google.dagger.hilt.android'
}....


dependencies {

.....
    implementation 'com.google.dagger:hilt-android:2.43.2'
    annotationProcessor 'com.google.dagger:hilt-compiler:2.43.2'
}

我们可以直接在顶级Build.gradle文件中添加class路径如下。

代码在此处输入图像描述

buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.5.3"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}
// 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
}

这是按照文档中的建议为导航组件添加安全参数的示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM