如何在build.gradle项目文件中为NavigationComponent配置SafeArgs?

xzabzqsa  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(154)

我想在我的android项目中使用androidx.navigation.safeargs插件。

Android Gradle插件版本:7.4.1
Gradle版本:7.5
JDK版本:11

但是,当我添加到下面的行到我的settings.gradle文件,它给了我一个错误。

buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.3"
    }
}

字符串
错误代码:

org.gradle.api.GradleException: Error resolving plugin [id: 'com.android.application', version: '7.4.1', apply: false]
Caused by: org.gradle.plugin.management.internal.InvalidPluginRequestException: The request for this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so compatibility cannot be checked.


我该如何实现这一点?我想使用NavigationComponent和SafeArgs来实现类型安全。
我的build.gradle文件也显示如下:

build.gradle(Project:MyApp)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
}

build.gradle(Module:App)

plugins {
    id 'com.android.application'
    id 'androidx.navigation.safeargs' version '2.5.3' apply false
}

android {
    namespace 'com.gamenight.myapp'
    compileSdk 33
    buildFeatures {
        dataBinding true
        viewBinding true
    }
    sourceSets {
        main {
            java {
                srcDirs += 'build/generated/source/navigation-args/'
            }
        }
    }
    defaultConfig {
        applicationId "com.gamenight.nite"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
}

dependencies {
    // Lifecycle
    implementation 'androidx.lifecycle:lifecycle-viewmodel:2.6.1'
    implementation 'androidx.lifecycle:lifecycle-runtime:2.6.1'
    // ViewModel SavedState
    implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.6.1'
    implementation "androidx.appcompat:appcompat:1.6.1"
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
    // Room
    implementation 'androidx.room:room-runtime:2.5.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
    annotationProcessor 'androidx.room:room-compiler:2.5.2'

    implementation 'com.google.android.material:material:1.9.0'

    //Fragment Navigation
    /*implementation "androidx.navigation:navigation-fragment-ktx:2.6.0"
    implementation "androidx.navigation:navigation-ui-ktx:2.6.0"*/
    implementation "androidx.navigation:navigation-fragment:2.6.0"
    implementation "androidx.navigation:navigation-ui:2.6.0"

    // Feature module Support
    implementation "androidx.navigation:navigation-dynamic-features-fragment:2.5.3"

    // Testing Navigation
    androidTestImplementation "androidx.navigation:navigation-testing:2.5.3"

    // Jetpack Compose Integration
    implementation "androidx.navigation:navigation-compose:2.5.3"

    //SafeArgs
    implementation "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.3"

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

settings.gradle(项目设置)

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

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

    repositories {
        google()
        mavenCentral()
    }
}
buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.3"
    }
}

rootProject.name = "MyApp"
include ':app'

x759pob2

x759pob21#

我把classpath错误的文件。
它必须位于build.gradle(Project:MyApp)中,而不是build.gradle(Module:app)中。这对我很有效。

build.gradle(Project:MyApp)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.3"
    }
}
plugins {
    id 'com.android.application' version '7.4.1' apply false
    id 'com.android.library' version '7.4.1' apply false
}
task clean(type: Delete){
    delete rootProject.buildDir
}

字符串

settings.gradle(项目设置)

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

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "MyApp"
include ':app'

相关问题