Skip to content

Conversation

MGaetan89
Copy link
Member

This commit removes the dependency on the test task when running the simulator task.
We only need the assembleDebug task, to have an APK to run in the simulator.

Fixes #10412

@MGaetan89 MGaetan89 requested a review from hoisie June 19, 2025 09:51
@MGaetan89 MGaetan89 self-assigned this Jun 19, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully removes the dependency on the testDebugUnitTest task from the simulator task, ensuring that only the assembleDebug task is executed to provide the necessary APK for the simulator. This change aligns with the stated objective and improves the efficiency of the simulation task.

@@ -87,7 +86,7 @@ class SimulatorPlugin : Plugin<Project> {
jvmArgs = testTask.jvmArgs + robolectricJvmArgs
mainClass.set(MAIN_CLASS)
args = listOf(resourceApkFile.absolutePath)
dependsOn(testTaskName, "assembleDebug")
dependsOn("assembleDebug")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Removing the dependency on testDebugUnitTest from the dependsOn list for the simulate task aligns with the PR description and correctly ensures that the test task is not executed unnecessarily when only the simulator is needed. This improves the efficiency of the simulate task.

@@ -87,7 +86,7 @@ class SimulatorPlugin : Plugin<Project> {
jvmArgs = testTask.jvmArgs + robolectricJvmArgs
mainClass.set(MAIN_CLASS)
args = listOf(resourceApkFile.absolutePath)
dependsOn(testTaskName, "assembleDebug")
dependsOn("assembleDebug")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, this simulate task requires debutTest task finished?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, but according to the issue #10412 opened by @hoisie, it is not necessary to do see.
We just need an APK to install in the simulator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I know is that "apk" in test environment is liek "xxx._ap", and it is generated by test task. If you we remove this task dependency, how can we ensure the "apk" file is generated ?

Copy link
Member Author

@MGaetan89 MGaetan89 Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I thought it was looking for the app APK, not the test APK.

So maybe, dependsOn should include the following. What do you think?

// Find the 'apk-for-local-test.ap_' file
val targetTask = project.tasks.getByName("packageDebugUnitTestForUnitTest")

dependsOn(targetTask, "assembleDebug"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to run the packageDebugUnitTestForUnitTest locally, but it was not found (using AGP 8.10.1). Maybe we need to find a better way to generate the apk-for-local-test.ap_ file (as suggested in #10382, suggestion 7)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, the issue was that I didn't set unitTests.isIncludeAndroidResources = true.
I'll add a tip here if the task is not found.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I just upgraded to AGP 8.10.1 and the task packageDebugUnitTestForUnitTest exists for me in a very simple project.

Copy link
Contributor

@hoisie hoisie Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MGaetan89 that sounds great, thanks. We should also consider failing Robolectric tests if the ap_ is not supplied as well, but that can be a separate thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoisie I've pushed the changes. Can you try again?

The build is already failed when the .ap_ is not find:

val resourceApkFile =
targetTask.outputs.files.find { it.name.endsWith(".ap_") }
?: throw GradleException(
"Could not find an .ap_ file in the outputs of task '${targetTask.name}'. "
)

Or do you have something else in mind?

Copy link
Contributor

@hoisie hoisie Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was thinking that RobolectricTestRunner could check for the ap_ file to be supplied, to force people to set unitTests.isIncludeAndroidResources = true for tests as well (not just the simulator). But that can be a separate issue.

Copy link
Contributor

@hoisie hoisie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey this PR works for me in my simple projects. I think it is a step in the right direction.

@MGaetan89 MGaetan89 force-pushed the fix_10412 branch 2 times, most recently from 70261d0 to 1a64763 Compare June 19, 2025 14:55
This commit removes the dependency on the test task when running the `simulator` task.
We only need the `assembleDebug` task, to have an APK to run in the simulator.

Fixes robolectric#10412
@hoisie
Copy link
Contributor

hoisie commented Jun 19, 2025

Thank you @MGaetan89 for quickly resolving this issue, I'll CP these fixes and push out 4.15.

@MGaetan89 MGaetan89 merged commit 6ac4071 into robolectric:master Jun 19, 2025
21 checks passed
@MGaetan89 MGaetan89 deleted the fix_10412 branch June 19, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gradle simulate task should not run tests
3 participants