Android jetpack Safeargs 插件未生成方向类(JAVA)

发布于 2025-01-09 20:58:45 字数 4334 浏览 1 评论 0原文

我正在尝试使用 safeargs 插件来传递数据并从一个片段导航到另一个片段。在导航图中的目标片段中添加参数并选择重建后,我没有获得自动生成的方向类和 Args 类。 我遵循官方文档并添加了 safeargs 必要的依赖项。我也在 gradle.properties 中将 android.useAndroidX 设置为 true 。

我的应用程序级别 build.gradle 文件:

plugins {
    id 'com.android.application'
    id 'androidx.navigation.safeargs'
}
android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.fragnavpractise"
        minSdk 23
        targetSdk 32
        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_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    def nav_version = "2.4.1"
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    // Java language implementation
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

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

    // Testing Navigation
    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

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

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

我的项​​目级别 build.gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.4.1"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}
plugins {
    id 'com.android.application' version '7.1.0' apply false
    id 'com.android.library' version '7.1.0' apply false

}

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

我的导航图:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/payment_navigation_graph"
    app:startDestination="@id/launchFragment">

    <fragment
        android:id="@+id/launchFragment"
        android:name="com.example.fragnavpractise.LaunchFragment"
        android:label="fragment_launch"
        tools:layout="@layout/fragment_launch" >
        <action
            android:id="@+id/action_launchFragment_to_payFragment"
            app:destination="@id/payFragment" />
    </fragment>
    <fragment
        android:id="@+id/payFragment"
        android:name="com.example.fragnavpractise.PayFragment"
        android:label="fragment_pay"
        tools:layout="@layout/fragment_pay" >
        <action
            android:id="@+id/action_payFragment_to_finishFragment"
            app:destination="@id/finishFragment" />
    </fragment>
    <fragment
        android:id="@+id/finishFragment"
        android:name="com.example.fragnavpractise.FinishFragment"
        android:label="fragment_finish"
        tools:layout="@layout/fragment_finish" >
        <action
            android:id="@+id/action_finishFragment_to_launchFragment"
            app:destination="@id/launchFragment" />
        <argument
            android:name="payee_name"
            app:argType="string"
            app:nullable="true" />
    </fragment>
</navigation>

我还尝试清理构建然后重建。我确实在网上找到的教程中尝试了不同的解决方案,但无法修复它。请帮我解决这个问题。提前致谢。

I'm trying to use safeargs plugin to pass data and navigate from one fragment to another. After adding the arguments in the destination fragment in the navigation graph, and choosing rebuild, I'm not getting the autogenerated directions class and Args class.
I followed the official docs and added the necessary dependecies for safeargs. I did set android.useAndroidX to true in the gradle.properties as well.

My app level build.gradle file:

plugins {
    id 'com.android.application'
    id 'androidx.navigation.safeargs'
}
android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.fragnavpractise"
        minSdk 23
        targetSdk 32
        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_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    def nav_version = "2.4.1"
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    // Java language implementation
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

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

    // Testing Navigation
    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

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

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

My Project level build.gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.4.1"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}
plugins {
    id 'com.android.application' version '7.1.0' apply false
    id 'com.android.library' version '7.1.0' apply false

}

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

My Navigation graph:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/payment_navigation_graph"
    app:startDestination="@id/launchFragment">

    <fragment
        android:id="@+id/launchFragment"
        android:name="com.example.fragnavpractise.LaunchFragment"
        android:label="fragment_launch"
        tools:layout="@layout/fragment_launch" >
        <action
            android:id="@+id/action_launchFragment_to_payFragment"
            app:destination="@id/payFragment" />
    </fragment>
    <fragment
        android:id="@+id/payFragment"
        android:name="com.example.fragnavpractise.PayFragment"
        android:label="fragment_pay"
        tools:layout="@layout/fragment_pay" >
        <action
            android:id="@+id/action_payFragment_to_finishFragment"
            app:destination="@id/finishFragment" />
    </fragment>
    <fragment
        android:id="@+id/finishFragment"
        android:name="com.example.fragnavpractise.FinishFragment"
        android:label="fragment_finish"
        tools:layout="@layout/fragment_finish" >
        <action
            android:id="@+id/action_finishFragment_to_launchFragment"
            app:destination="@id/launchFragment" />
        <argument
            android:name="payee_name"
            app:argType="string"
            app:nullable="true" />
    </fragment>
</navigation>

I've also tried to clean build and then rebuild. I did try different solutions in tutorials I found online but couldn't fix it. Please help me resolve this. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文