Android导航组件无法生成Directions类问题求助
问题背景
项目因Gradle问题重新搭建后,其他功能正常,但Navigation组件无法自动生成Directions类,已尝试查阅相关帖子但未解决,以下是相关配置文件:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Unscramble" tools:targetApi="31" > <activity android:name=".MainActivity" android:exported="true" android:screenOrientation="sensorPortrait" android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
项目级 build.gradle
plugins { id 'com.android.application' version '8.0.2' apply false id 'com.android.library' version '8.0.2' apply false id 'org.jetbrains.kotlin.android' version '1.8.20' apply false id 'androidx.navigation.safeargs' version '2.7.4' apply false }
应用级 build.gradle
buildscript { repositories { google() } dependencies { classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.7.4") } } plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'androidx.navigation.safeargs' } android { namespace 'com.example.unscramble' compileSdk 34 defaultConfig { applicationId "com.example.unscramble" minSdk 24 targetSdk 34 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 } kotlinOptions { jvmTarget = '1.8' } buildFeatures { viewBinding true } } dependencies { implementation 'androidx.core:core-ktx:1.12.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.10.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.google.code.gson:gson:2.10.1' implementation 'androidx.legacy:legacy-support-v4:1.0.0' //LiveData implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2' //ViewModel implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2' implementation 'androidx.fragment:fragment-ktx:1.6.1' // Navigation implementation "androidx.navigation:navigation-fragment-ktx:2.7.4" implementation "androidx.navigation:navigation-ui-ktx:2.7.4" 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() } } rootProject.name = "unscramble" include ':app'
排查与解决步骤
修正Safe Args插件配置
当前应用级build.gradle同时用buildscript和plugins块引入插件,存在冗余且可能导致冲突。删除应用级build.gradle中的buildscript代码块,同时将Kotlin项目对应的Safe Args插件改为androidx.navigation.safeargs.kotlin(通用版插件不适用于纯Kotlin项目)。修改后的应用级build.gradle开头如下:plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'androidx.navigation.safeargs.kotlin' }检查导航图动作配置
Directions类基于导航图中的action生成,确保res/navigation/nav_graph.xml中存在有效动作配置,示例:<action android:id="@+id/action_firstFragment_to_secondFragment" app:destination="@id/secondFragment" />清理重建项目
- 执行
Build > Clean Project - 再执行
Build > Rebuild Project - 完成后检查
build/generated/source/navigation-args目录,确认是否生成了Directions类
- 执行
验证注解处理器设置
打开File > Settings > Build, Execution, Deployment > Compiler > Annotation Processors,确保勾选Enable annotation processing(混合Java/Kotlin项目需确认,纯Kotlin项目通常自动开启)确认版本一致性
当前Navigation插件与依赖版本均为2.7.4,版本一致无需修改
内容的提问来源于stack exchange,提问作者Kalaitos Ornitier
相关产品推荐
相关产品推荐

