Monits / static-code-analysis-plugin

A plugin to simplify Static Code Analysis on Gradle. Not restricted to, but specially useful, in Android projects, by making sure all analysis can access the SDK classes.
Apache License 2.0
39 stars 12 forks source link

PMD error on Android projects using Play Services or LeakCanary #37

Closed barriosnahuel closed 4 years ago

barriosnahuel commented 4 years ago

Hi!

I'm having two issues after upgrading to SCA v3.0.0.

First one is related to PMD, second one to Spotbugs.

Context

I was using v2.6.12 fine in the given context:

Note: SCA v2.6.12 comes with PMD v6.20.0

Error

PMD runs ok in all modules but on the dynamic feature module, I'm getting the following result:

* What went wrong:
Execution failed for task ':my_anroid_dynamic_feature_module:processDebugAndroidTestResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Android resource linking failed
     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:34:13-36:66: AAPT: error: resource xml/leak_canary_file_paths (aka com.github.myawesomeproject.awesomefeature.test:xml/leak_canary_file_paths) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:39:9-44:66: AAPT: error: resource mipmap/leak_canary_icon (aka com.github.myawesomeproject.awesomefeature.test:mipmap/leak_canary_icon) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:39:9-44:66: AAPT: error: resource string/leak_canary_display_activity_label (aka com.github.myawesomeproject.awesomefeature.test:string/leak_canary_display_activity_label) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:39:9-44:66: AAPT: error: resource style/leak_canary_LeakCanary.Base (aka com.github.myawesomeproject.awesomefeature.test:style/leak_canary_LeakCanary.Base) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:46:9-62:26: AAPT: error: resource drawable/leak_canary_tv_icon (aka com.github.myawesomeproject.awesomefeature.test:drawable/leak_canary_tv_icon) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:46:9-62:26: AAPT: error: resource bool/leak_canary_add_launcher_icon (aka com.github.myawesomeproject.awesomefeature.test:bool/leak_canary_add_launcher_icon) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:46:9-62:26: AAPT: error: resource mipmap/leak_canary_icon (aka com.github.myawesomeproject.awesomefeature.test:mipmap/leak_canary_icon) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:46:9-62:26: AAPT: error: resource string/leak_canary_display_activity_label (aka com.github.myawesomeproject.awesomefeature.test:string/leak_canary_display_activity_label) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:46:9-62:26: AAPT: error: resource style/leak_canary_LeakCanary.Base (aka com.github.myawesomeproject.awesomefeature.test:style/leak_canary_LeakCanary.Base) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:64:9-70:68: AAPT: error: resource mipmap/leak_canary_icon (aka com.github.myawesomeproject.awesomefeature.test:mipmap/leak_canary_icon) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:64:9-70:68: AAPT: error: resource string/leak_canary_storage_permission_activity_label (aka com.github.myawesomeproject.awesomefeature.test:string/leak_canary_storage_permission_activity_label) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/12544b80cee21462a629a686340edfa2/jetified-leakcanary-android-core-2.2/AndroidManifest.xml:64:9-70:68: AAPT: error: resource style/leak_canary_Theme.Transparent (aka com.github.myawesomeproject.awesomefeature.test:style/leak_canary_Theme.Transparent) not found.

     /Users/myuser/.gradle/caches/transforms-2/files-2.1/1242a38a4d8d07b0c228c34712765379/jetified-play-services-basement-17.0.0/AndroidManifest.xml:23:9-25:69: AAPT: error: resource integer/google_play_services_version (aka com.github.myawesomeproject.awesomefeature.test:integer/google_play_services_version) not found.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 6s
82 actionable tasks: 14 executed, 68 up-to-date

What I've tried

Why I tried downgrading to v5.2.1? After upgrading to SCA v3.0.0, when running:

./gradlew spotbugs

I'm getting the following error:

No classes configured for SpotBugs analysis.

which is extremely similar to https://github.com/spotbugs/spotbugs-gradle-plugin/issues/23.

But in this new context with Gradle v5.2.1 when running spotbugs I fall into https://github.com/spotbugs/spotbugs-gradle-plugin/issues/90.

Since all was working ok before upgrading and even after upgrading but using the previous version of PMD, I think it's a bug on SCA itself (perhaps on the workaround I assume you're doing to make Spotbugs work with Android projects? Should I file a new issue for this Spotbugs behaviour?

I can share a branch to try on..

barriosnahuel commented 4 years ago

We can close it, I added more info at https://issuetracker.google.com/issues/137253405#comment3 (if you need, I can add a new issue for the spotbugs failure)

jsotuyod commented 4 years ago

To sum up my research into this…

The issue with :my_anroid_dynamic_feature_module:processDebugAndroidTestResources is actually a bug in AGP for dynamic modules. It fails to compile androidTest for that module by not being able to resolve resources from an aar dependency that are actually there. SCA has nothing to do with it, other than simply forcing the task to run, as we need compiled sourced for PMD / Spotbugs analysis. The same issue would arise if you were to simply write an instrumentation test for that module even without SCA. I'd therefore recommend you report that back to https://b.android.com/

As for the issue with no classes for Spotbugs analysis… the problem relies on your project having a non-emtpy Java sourceset, that contains no Java classes (the failing module contains only Kotlin classes in the Java sourceset). As SCA doesn't yet support Kotlin, the Kotlin classes are not being present in the analysis, but as there are files, Spotbugs still believes there is something to analyze and fails…

I do have in backlog to fully support Kotlin in an upcoming 3.x release. In the meantime, you can use the following workaround:

tasks.matching { it.name =~ /^spotbugs.+/ }.configureEach {
  it.onlyIf { !it.classes.empty }
}