-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP Add support for Junit tests #1811
Conversation
related studio bug: https://issuetracker.google.com/37003772 |
outputSourceRootDir: File, | ||
skipIfNotHilt : Boolean = false | ||
) { | ||
outputSourceRootDir.mkdirs() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Check return value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually the return value for mkdirs is not straightforward as it will return false if it already exists and that is a valid case here.
...agger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltClassTransformer.kt
Outdated
Show resolved
Hide resolved
...lt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltTransformTestClassesTask.kt
Outdated
Show resolved
Hide resolved
...lt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltTransformTestClassesTask.kt
Outdated
Show resolved
Hide resolved
...lt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltTransformTestClassesTask.kt
Outdated
Show resolved
Hide resolved
...lt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltTransformTestClassesTask.kt
Outdated
Show resolved
Hide resolved
val outputFileCollection = project.objects.fileCollection() | ||
val classpathKey = unitTestVariant.registerPreJavacGeneratedBytecode( | ||
outputFileCollection.apply { | ||
from(outputDir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to replace this with task output i.e. pass task.outputDir.getAsFileTree()
to registerPreJavacGeneratedBytecode
. That avoid usage of .builtBy
on line 113.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm not sure how to do that, maybe i'm missing something here about gradle.
I don't have the task until it is configured but to configure the task i need to know the output directory. what am i missing here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and if i try to register when task is configured then gradle won't really have a reason to configure it, but maybe i can add it as a dependency manually? i feel like i'm missing some gradle APIs here 🤷
} | ||
} | ||
// parse the classpath in reverse so that we respect overrides, if it ever happens | ||
compiledClasses.files.filter { it.isDirectory }.reversed().forEach { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you filter directories? In 4.1, for the unit test variant the tested code (i.e. main) will be in the classpath as jar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code i'm re-using here is from transform API which does ignore jars.
and yea i just tried w/ 4.1 and it fails, we were using 3.6.3 here. i'll see what we can do there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. added transform for jars but only here. i'm guessing the regular transform API still receives .class files ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually seems to be working fine but i'll keep this open in case we should also process contents of jar files for the transform API. FYI we are only interested in files from the compiled module/app.
java/dagger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt
Show resolved
Hide resolved
java/dagger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt
Outdated
Show resolved
Hide resolved
Android transforms do not run for junit tests which breaks hilt. This CL adds a new custom test transform just for tests so that Hilt can run fine for AndroidJUnit tests
Android transforms are not applied for JUnit tests which breaks when using the Hilt Plugin and its transform. This CL adds a new custom test transform task just for tests so that transformed classes are used in Android JUnit tests. The task does not transform test sources, therefore temporarily allow specifying a base class in @androidentrypoint that will be used even if the superclass validation option is given to the processor. This helps us still have test-only Android Entry Points. Closes #1811 RELNOTES=n/a ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=309143731
Android transforms are not applied for JUnit tests which breaks when using the Hilt Plugin and its transform. This CL adds a new custom test transform task just for tests so that transformed classes are used in Android JUnit tests. The task does not transform test sources, therefore temporarily allow specifying a base class in @androidentrypoint that will be used even if the superclass validation option is given to the processor. This helps us still have test-only Android Entry Points. Closes #1811 RELNOTES=n/a ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=309143731
Android Transform API does not work for
junit
tests which breaks robolectric tests that use Hilt.This CL adds a new optional task that will run the transform on the compiler output and feed it into the unit test task as the first classpath item (hence shadow regular output).
I've added an activity launcher test to the sample app and also a kotlin sample app since this task requires special handling for kotlin output.
Unfortunately, this does not work from Android Studio as it computes its own test classpath and does not use the gradle to run the test.