google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.36k stars 2k forks source link

[Ksp] InjectProcessingStep was unable to process 'test' because 'error.NonExistentClass' could not be resolved. #4097

Closed Lans closed 9 months ago

Lans commented 9 months ago
error: e: [ksp] InjectProcessingStep was unable to process 'test' because 'error.NonExistentClass' could not be resolved.

Dependency trace:
    => element (CLASS): com.lans.auto.ui.AppInfoActivity
    => type (DECLARED superclass): com.lans.auto.base.BaseActivity<error.NonExistentClass>
    => type (ERROR type argument): error.NonExistentClass

If type 'error.NonExistentClass' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'error.NonExistentClass' is on your classpath.
e: Error occurred in KSP, check log for detail
    implementation("com.google.dagger:hilt-android:2.48.1")
    ksp("com.google.dagger:hilt-android-compiler:2.48.1")
    implementation("com.google.dagger:dagger-android:2.48.1")
    ksp("com.google.dagger:dagger-compiler:2.48.1")
@AndroidEntryPoint
class AppInfoActivity : BaseActivity<ActivityAppInfoBinding>() {
    private lateinit var binding: ActivityAppInfoBinding
    private lateinit var appAdapter: AppAdapter
    @Inject lateinit var test: Test
}
bcorso commented 9 months ago

This error means that Dagger could not find ActivityAppInfoBinding on the classpath.

This usually happens when you have a setup like the following:

:app -> :lib1 -> :lib2

Where

The issue is that while trying to generate the component in :app Dagger does not find ActivityAppInfoBinding on the classpath because Gradle prunes transitive implementation dependencies from the classpath.

To fix this, you will need to either add a direct dependency from :app -> :lib2 or change the :lib1 -> :lib2 dependency from implementation to api so that it does not get pruned.

Lans commented 9 months ago

This error means that Dagger could not find ActivityAppInfoBinding on the classpath.

This usually happens when you have a setup like the following:

:app -> :lib1 -> :lib2

Where

  • :app contains your Application
  • :lib1 contains AppInfoActivity
  • :lib2 contains ActivityAppInfoBinding

The issue is that while trying to generate the component in :app Dagger does not find ActivityAppInfoBinding on the classpath because Gradle prunes transitive implementation dependencies from the classpath.

To fix this, you will need to either add a direct dependency from :app -> :lib2 or change the :lib1 -> :lib2 dependency from implementation to api so that it does not get pruned.

image image

no lib ,was only one app project

bcorso commented 9 months ago

In that case, you'll also want to double check that you've added the import statement for ActivityAppInfoBinding in the AppInfoActivity.kt source file, as a missing import statement can also lead to this error.

Lans commented 9 months ago

In that case, you'll also want to double check that you've added the import statement for ActivityAppInfoBinding in the AppInfoActivity.kt source file, as a missing import statement can also lead to this error.

look this ,i use viewbinding

image
bcorso commented 9 months ago

Hmm, I'm not sure then. It could be a bug in KSP, but I would need an example project to reproduce this locally and test it myself.

bcorso commented 9 months ago

Ah, I just noticed from the import that the package is com.lans.auto.databinding.

There's a known issue with DataBinding classes in KSP, which I think can be handled by https://github.com/google/dagger/issues/4049#issuecomment-1749067296

Lans commented 9 months ago

I looked at IssueTracker and it solves the problem like this, but it still requires the Google team to solve the problem with KSP

Groovy
androidComponents {
        onVariants(selector().all(), { variant ->
            afterEvaluate {
                // This is a workaround for https://issuetracker.google.com/301245705 which depends on internal
                // implementations of the android gradle plugin and the ksp gradle plugin which might change in the future
                // in an unpredictable way.
                def dataBindingTask = (DataBindingGenBaseClassesTask) project.tasks.named("dataBindingGenBaseClasses" + variant.name.capitalize()).get()

                if (dataBindingTask != null) {
                    project.tasks.getByName("ksp" + variant.name.capitalize() + "Kotlin") {
                        ((AbstractKotlinCompileTool) it).setSource(dataBindingTask.sourceOutFolder)
                    }
                }
            }
        })
    }
gradle.kts
 androidComponents {
        onVariants(selector().all()) { variant ->
            afterEvaluate {
                project.tasks.getByName("ksp" + variant.name.capitalized() + "Kotlin") {
                    val dataBindingTask =
                        project.tasks.getByName("dataBindingGenBaseClasses" + variant.name.capitalized()) as DataBindingGenBaseClassesTask
                    (this as AbstractKotlinCompileTool<*>).setSource(dataBindingTask.sourceOutFolder)
                }
            }
        }
    }