6

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"
}
wooldridgetm
  • 2,500
  • 1
  • 16
  • 22

4 Answers4

2

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.

Preferences > Editor > File Types

wooldridgetm
  • 2,500
  • 1
  • 16
  • 22
  • 1
    I have the same problem with AS 4.2 Beta 6. All classes related to SafeArgs or Directions are not recognized and shows error, but compiles. Your solution didn't work for me. Any idea? – Amir Mar 31 '21 at 08:07
2

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.

  • This coused redeclareation issues for me. Instead, I've marked navigatin-args->debug as Generated sources root – osrl Jun 23 '21 at 12:50
0

I had a similar issue with actions which were clearly added in the navigation graph not 'autocompleting' or showing an option to import in the kotlin class trying to access them, even with the correct import added manually.

For me the fix was simply to to rebuild the project and they automatically appeared then.

Mick
  • 24,231
  • 1
  • 54
  • 120
0

It was an issue in Navigation API, Apparently, They have fixed this issue in 2.5.0-rc02 version.

Please refer to this for more details.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 23 '22 at 03:40