Search code examples
androidkotlingradle-kotlin-dslkotlin-multiplatformandroid-safe-args

Android SafeArgs - Can't import the Navigation Directions class generated


I'm here to report my problem for what it seems I can't find any solution on the internet, so I decided to write here, I've been trying for days to solve this and I still don't understand where the reason for the error is. The error basically is, I can't import the class that is generated in navigations, Directions, when I write the whole path or when I click import, it just puts the whole path where the class is located and doesn't know what that is but behind it recognizes that there is a class there but can't give import to it. Strange isn't it? I appreciate any kind of solution you can give me, thanks!

package pt.saphirex.sample.android.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import pt.saphirex.sample.shared.Greeting
import android.widget.TextView
import pt.saphirex.sample.android.R

fun greet(): String {
    return Greeting().greeting()
}

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tv: TextView = findViewById(R.id.text_view)
        tv.text = greet()
        
        pt.saphirex.sample.android.ui.user.home.UserHomeFragmentDirections // <---- Doesn't recognize
    }

}

My software versions are:

  • Android Studio: 4.2.1
  • KMM Plugin: 0.2.5(202-1.5.10-834-IJ)-3

GitHub example: https://github.com/0rangeFox/KMM-Sample


Solution

  • The solution is basically this, go to android's "build.gradle.kts", and add the following lines inside the "android { }" code:

    android {
        ...
    
        sourceSets {
            getByName("main") {
                java.srcDir("build/generated/source/navigation-args")
            }
        }
    
        ...
    }