NeoTech-Software/Android-Root-Coverage-Plugin

Unresolved reference BuildConfig when upgrading to 1.5.0

Closed this issue · 6 comments

Hi, I've been using this library using AGP 4 and Root Coverage Plugin 1.4.0. But after I update the AGP and kotlin version to this version (see version below) I got an error that said Unresolved reference: BuildConfig. The unresolved BuildConfig causes an error when I generate jacoco report or assembleDebug the app. Fyi I saved the value of BuildConfigField at shared gradle and apply it to each module, is that affect this issue?. Also, when I downgrade to 1.4.0 the error is gone, generating a report is successful but causes [ant:jacocoReport] Execution data for class 'xxx' does not match.

This is the version:

AGP: 7.2.0
Kotlin Gradle Plugin: 1.6.21
Jacoco: 0.8.6

This is the stackrace:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':core:compileDebugKotlin'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:145)
        at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:282)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:143)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:131)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:77)
        at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
        at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:56)
        

@MirzaUkas Thanks for sharing this.

For some reason I'm doubting that this has something to do with this plugin directly, since it does not by itself manipulate classes. It could be JaCoCo related but I also doubt that, since the version did not change. Maybe this has something to do with the result filtering that the plugin applies? It does (by default) filter out BuildConfig from the final report. But filtering on report level should not affect actual shipped code, and this filtering has not changed in version 1.5 compared to 1.4.

By pure chance I also experienced this error in one of my own projects, about a week ago. Back then I figured it must have had something to do with the AGP upgrade to 7.2.0.

Anyhow, I need a bit more time to investigate what exactly is going on.

Did some digging, I found that I can no longer reproduce this when I remove this line:

But obviously this line is needed for code coverage to work. So while it fixes the BuildConfig problem, it breaks code coverage.

I suspect the issue is in the variant.sources.java.all part. This is a new public but unstable API to get the Java source locations. Release 1.4 of this plugin was using an older (unofficial) method to get the Java sources. For some reason calling this API results in the BuildConfig file to disappear. I updated to AGP version 7.3.0-beta01 and by doing so the problem was gone.

Conclusion:
variant.sources.java.all is broken in AGP version 7.2 and causes class files to disappear (not generated?). In AGP 7.3.0-beta01 this issue is not present.

Solution:
There are 2 things possible, the quick and easy is to update to AGP version 7.3.0-beta01 but not everyone might be able to do so. The other (more difficult) solution is to create a hot-fix for this release where I change to code so it uses the old (unofficial) method to get the Java sources. However this is not as easy as one might think, since in release 1.5 I changed the whole plugin setup to use official APIs only.

TLDR: This is potentially a bug in AGP 7.2, I'm looking to workaround this.

Thanks a lot, @Rolf-Smit for digging into this issue.

I got it, so it's still a bug in AGP 7.2 and the quick solution is to upgrade the AGP version 7.3.0-beta01. But like you already said, not everyone can do it and I can't do it because my project show this The project is using an incompatible version (AGP 7.3.0-beta01) of the Android Gradle plugin. Latest supported version is AGP 7.2.0.

So yeah I'll wait for the update for gradle 7.3 or maybe next time you find another solution.

@MirzaUkas that is strange, higher AGP versions should be allowed (I guess I will add some test cases for that check then).

Anyhow, I'm planning to hot-fix this today (by reintroducing some old and deprecated AGP code, since that seems to be working in AGP 7.2).

@MirzaUkas I just released version 1.5.1 which contains a workaround for this issue. Can you test on your side as well and confirm?

After upgrading to 1.5.1, it works like a charm! Thanks @Rolf-Smit