Shopify / android-testify

Add screenshots to your Android tests
https://testify.dev
MIT License
231 stars 23 forks source link

Patch for AGP 7 issue accessing the applicationId on the testVariant #235

Closed DanielJette closed 3 years ago

DanielJette commented 3 years ago

What does this change accomplish?

Resolves #234

How have you achieved it?

The issue is that the applicationId on the testVariant is not available when configuring the Testify plugin. I verified that this is invoked in AfterEvaluate and should be valid at this point.

The documentation for testVariant says:

To process elements in this collection, you should use all. That's because the plugin populates this collection only after the project is evaluated. Unlike the each iterator, using all processes future elements as the plugin creates them.

I tried switching to using all and I still get an exception on the applicationId. Only a single testVariant is ever returned and it's always got an uninitialized applicationId.

According to this Stack Overflow post, this is an AGP bug and it is fixed in 7.1.0-alpha5. I verified that this does, indeed, work as expected. However, 7.1.0-alpha6 breaks again with the identical issue. So, I have reasoned that we can't wait on an upstream fix for this issue.

My solution here is to attempt to fetch the testVariant's applicationID, and if it fails, to fallback to the inferred applicationId prepended with .test. This should be correct for most instances, but may break some applications with customized product variants & flavors.

In the case where the test packaged ID is incorrectly inferred, the user can specify testify { testPackageId "my.custom.package.test" } in their build.gradle.

Tophat instructions

  1. Check out this branch (it is upgraded to AGP 7)
  2. Run ./gradlew tasks and verify that it completes successfully
  3. Run all tests in the Sample app
  4. Run ./gradlew Sample:testifySettings and verify that the testPackageId is correct set