google / dagger

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

not a valid name: <set-?>Provider After updating android studio to 3.5 #1593

Open mnayef95 opened 5 years ago

mnayef95 commented 5 years ago

Hi all, i'm facing issue with dagger after updating android studio to 3.5, this is the stacktrace

e: [kapt] An exception occurred: java.lang.IllegalArgumentException: not a valid name: <set-?>Provider
    at com.squareup.javapoet.Util.checkArgument(Util.java:53)
    at com.squareup.javapoet.FieldSpec.builder(FieldSpec.java:91)
    at dagger.internal.codegen.MembersInjectorGenerator.write(MembersInjectorGenerator.java:157)
    at dagger.internal.codegen.MembersInjectorGenerator.write(MembersInjectorGenerator.java:66)
    at dagger.internal.codegen.SourceFileGenerator.generate(SourceFileGenerator.java:78)
    at dagger.internal.codegen.InjectBindingRegistryImpl$BindingsCollection.generateBindings(InjectBindingRegistryImpl.java:89)
    at dagger.internal.codegen.InjectBindingRegistryImpl.generateSourcesForRequiredBindings(InjectBindingRegistryImpl.java:183)
    at dagger.internal.codegen.ComponentProcessor.postRound(ComponentProcessor.java:189)
    at dagger.shaded.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:183)
    at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt)
    at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:132)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1068)
    at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:80)
    at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:36)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:223)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:187)
    at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:98)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration$2.invoke(TopDownAnalyzerFacadeForJVM.kt:95)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:105)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:80)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:398)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:65)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:107)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:389)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:118)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:166)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:56)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:84)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:42)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:104)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$1$2.invoke(CompileServiceImpl.kt:442)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$1$2.invoke(CompileServiceImpl.kt:102)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:1005)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:102)
    at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:138)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:1047)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:1004)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:441)
    at sun.reflect.GeneratedMethodAccessor103.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Dagger version: 2.24

bulatgaleev commented 5 years ago

Same on dagger 2.16

bulatgaleev commented 5 years ago

Related issue: https://issuetracker.google.com/issues/133461087

nikiJava commented 5 years ago

I get the same error on 2.24 too.

gmazzo commented 5 years ago

Same error since moved to Android Studio 3.5, it's only triggered while running tests from inside the IDE. Console is fine.

vdbo commented 5 years ago

I encountered the same error after migrating to Android Studio 3.5 for 2.24 version. It seems to be because of changes in kapt.

gigaplex commented 5 years ago

Same issue here using dagger 2.11.

douglasmarques commented 5 years ago

Same issue here using dagger 2.19.

bellol commented 5 years ago

avoiding any usages of @set:Inject can be used as a workaround

douglasmarques commented 5 years ago

avoiding any usages of @set:Inject can be used as a workaround

I don't have any @set:Inject, other suggestions?

TheMedo commented 5 years ago

For me this did not work:

@set:Inject
lateinit var something: Something

This did not work either:

var something: Something? = null
   @Inject set

But, this did:

var something: Something? = null
   @Inject set(value) {
      field = value
   }

IDE will complain that the full declaration is redundant and can be simplified, however this was the only way that kapt accepted :)

jelleheemskerk commented 5 years ago

@TheMedo thanks! It works.

It seems that before the update @Inject set was shorthand for field = value but this magic is now removed (more explicit).

You can also use:

@Inject
@JvmField
var something: Something? = null

Thanks to @mennovogel

senkir commented 5 years ago

For what it's worth I was able to get it to compile if i downgrade from gradle plugin 3.5.0 to 3.4.2 as a temporary developer fix to unblock while we try the above

@JvmField

change

BelooS commented 5 years ago

Seems that i have managed it to work on: android studio 3.5.0 dagger version 2.24, gradle plugin 2.5.0 wrapper 5.4.1 Just double check that your project neither use @set:Inject or @Inject set structures.

@Inject lateinit var: Some

would be enough

bulatgaleev commented 5 years ago

Seems that i have managed it to work on: android studio 3.5.0 dagger version 2.24, gradle plugin 2.5.0 wrapper 5.4.1

Just double check that your project neither use @set:Inject or @Inject set structures.

@Inject lateinit var: Some

would be enough

image

fukusaka commented 5 years ago

I get the same error on 2.24 too.

However, I was able to build after changing the SDK location AdoptOpenJDK 8 or Amazon Corretto 8.

/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    1.8.0_222, x86_64:  "Amazon Corretto 8" /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home
    1.8.0_222, x86_64:  "AdoptOpenJDK 8"    /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
brayskiy commented 5 years ago

I got exactly the same issue after Android Studio was updated to the version 3.5 I fixed this issue in 4 steps:

  1. Added kotlinOptions section with jvmTarget into app/build.gradle kotlinOptions { jvmTarget = "1.8" } (the section compileOptions is already there) compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }

  2. Upgraded dagger from 2.22.1 to 2.24

  3. Disabled incremental kapt in gradle.properties kapt.incremental.apt=false

  4. Invalidated build cache, restarted Android Studio and rebuilt the project

Note, that the CI build is working all the time.

sevar83 commented 5 years ago

I got exactly the same issue after Android Studio was updated to the version 3.5 I fixed this issue in 4 steps:

  1. Added kotlinOptions section with jvmTarget into app/build.gradle kotlinOptions { jvmTarget = "1.8" } (the section compileOptions is already there) compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }
  2. Upgraded dagger from 2.22.1 to 2.24
  3. Disabled incremental kapt in gradle.properties kapt.incremental.apt=false
  4. Invalidated build cache, restarted Android Studio and rebuilt the project

Note, that the CI build is working all the time.

Didn't help in my case :(

ZacSweers commented 4 years ago

Edited out because this is a different issue

daverix commented 4 years ago

Have you seen this article? https://www.donnfelker.com/dagger-open-jdk-version-mismatch/

When changing the jdk location in the module settings to what I'm running Android Studio with I no longer get this problem.

My jdk location: /usr/lib/jvm/java-1.8.0-openjdk

This links to my current jdk which is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.fc31.x86_64 as seen below:

$ java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
yogurtearl commented 3 years ago

Hit this again today: JDK 11.0.8 Kotlin 1.4.10 Dagger 2.28.3

@Singleton
class FooClass @Inject constructor(
    thingProvider: ThingProvider
) : FooTracker(thingProvider) {

    var myVar: Boolean = false
        .....

}

Which produces bytecode like:

// access flags 0x11
public final setMyVar(Z)V
// annotable parameter count: 1 (visible)
// annotable parameter count: 1 (invisible)
L0
LINENUMBER 13 L0
ALOAD 0
ILOAD 1
PUTFIELD com/example/FooClass.myVar : Z
RETURN
L1
LOCALVARIABLE this Lcom/example/FooClass; L0 L1 0
LOCALVARIABLE <set-?> Z L0 L1 1
MAXSTACK = 2
MAXLOCALS = 2

Looks like <set-?> is a local variable name?

and we get this exception...

Caused by: com.sun.tools.javac.processing.AnnotationProcessingError: java.lang.IllegalArgumentException: not a valid name: <set-?>Provider
        at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:992)
        at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:896)
        at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1222)
        at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1334)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1258)
        at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1157)
        ... 38 more
Caused by: java.lang.IllegalArgumentException: not a valid name: <set-?>Provider
        at com.squareup.javapoet.Util.checkArgument(Util.java:53)
        at com.squareup.javapoet.FieldSpec.builder(FieldSpec.java:91)
        at dagger.internal.codegen.writing.MembersInjectorGenerator.write(MembersInjectorGenerator.java:177)
        at dagger.internal.codegen.writing.MembersInjectorGenerator.write(MembersInjectorGenerator.java:73)
        at dagger.internal.codegen.base.SourceFileGenerator.generate(SourceFileGenerator.java:77)
        at dagger.internal.codegen.validation.InjectBindingRegistryImpl$BindingsCollection.generateBindings(InjectBindingRegistryImpl.java:97)
        at dagger.internal.codegen.validation.InjectBindingRegistryImpl.generateSourcesForRequiredBindings(InjectBindingRegistryImpl.java:191)
        at dagger.internal.codegen.ComponentProcessor.postRound(ComponentProcessor.java:198)
        at dagger.shaded.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:183)
        at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt)
        at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:161)
        at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:980)
        ... 43 more
ZacSweers commented 3 years ago

I've figured this one out - this only happens when Dagger attempts to generate a MembersInjector for a class not in the current compilation unit. You need to ensure you're running dagger-compiler over every project that has injections. Otherwise, Dagger will try to generate it later in a downstream subproject. Aside from potentially hitting this issue (I suspect the problem is the elements API does not expose the setter correctly in this case), you will also break incremental processing in kapt.

If we run dagger-compiler over all the target classes in their own modules we don't have this issue.

iamanbansal commented 3 years ago

I've figured this one out - this only happens when Dagger attempts to generate a MembersInjector for a class not in the current compilation unit. You need to ensure you're running dagger-compiler over every project that has injections. Otherwise, Dagger will try to generate it later in a downstream subproject. Aside from potentially hitting this issue (I suspect the problem is the elements API does not expose the setter correctly in this case), you will also break incremental processing in kapt.

If we run dagger-compiler over all the target classes in their own modules we don't have this issue.

what worked for me is changing the JDK. I changed to this JDK https://adoptopenjdk.net/?variant=openjdk8&jvmVariant-hotspot

TachikomaGT commented 1 year ago

In my case @JvmOverloads with Kotlin's default parameter value causes this bug. Old, Java-way methods overloading is my solution.