I'm migrating several apps to use the Navigation Component, and I ran into a problem - the Directions/Args classes generated by the android-safe-args-gradle-plugin
are not being recognized in Android Studio.
Code demonstrating this problem is located at github.com/wooldridgetm/android-navigation.git, but crux of the matter is, in the code below, HomeFragmentDirections isn't recognized. This is taken from the class HomeFragment
, lines 76 - 82.
val b2 = view.findViewById<Button>(R.id.navigate_action_button)
b2.setOnClickListener {
// PROBLEM: HomeFragmentDirections isn't recognized.
val action = HomeFragmentDirections.nextAction()
// PROBLEM: results in Unresolved reference: flowStepNumber
// action.flowStepNumber = 1
findNavController().navigate(action)
}
I know what the documentation says & I know that class HomeFragmentDirections
is being generated by the plugin & is located at app/build/generated/source/navigation-args/debug/code/example/android/codelabs/navigation, but it is not recognized by AS.
I also know that this did work pre-v2 of the plugin...but it's not working now & I can't even get a simple example to behave as expected.
Any ideas on what's wrong?
BACKGROUND
I'm using AS 3.5 on MacOS Mojave, 10.14.6.
Project build.gradle
dependencies:
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50"
App build.gradle
file:
apply plugin: 'androidx.navigation.safeargs.kotlin'
dependencies {
// Navigation
def nav_version = "2.1.0"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}
The reason Android Studio/Lint wasn't "seeing" these classes was BIAD (b/c I'm a dummy).
I excluded the build
folder in the Project Panel via Preferences > Editor > File Types > Ignore files and folders.
In any event, removing the build
folder from Ignore files and folders did the trick.
Select the Project view in the project tab, so you can see the actual folders. You'll notice the app/build folder is oranged out, which means it's being excluded. Simply right click > Mark directory as > Cancel exclusion, then right click again > Mark directory as > Sources root. The folder will be blued out like the java folder and AS should recognize the generated classes.
Although I'm not sure I believe all the code you're gonna use is actually in app/build/generated, so you could be more specific marking that one as the sources root instead. So it should look something like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With