Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
12.99k stars 1.85k forks source link

Android Lint Error 1.3.6 - Invalid package reference #2004

Closed ZakTaccardi closed 4 years ago

ZakTaccardi commented 4 years ago

I'm seeing the following Android lint error when upgrading from 1.3.5 to 1.3.6

Package not included in Android
../../../../../../.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.3.6/84cc2e5a484555e49e908fd831ff503303401dd0/kotlinx-coroutines-core-1.3.6.jar: Invalid package reference in org.jetbrains.kotlinx:kotlinx-coroutines-core; not included in Android: java.lang.instrument. Referenced from kotlinx.coroutines.debug.AgentPremain.
This check scans through libraries looking for calls to APIs that are not included in Android.

When you create Android projects, the classpath is set up such that you can only access classes in the API packages that are included in Android. However, if you add other projects to your libs/ folder, there is no guarantee that those .jar files were built with an Android specific classpath, and in particular, they could be accessing unsupported APIs such as java.applet.

This check scans through library jars and looks for references to API packages that are not included in Android and flags these. This is only an error if your code calls one of the library classes which wind up referencing the unsupported package.
To suppress this error, use the issue id "InvalidPackage" as explained in the Suppressing Warnings and Errors section.
ZacSweers commented 4 years ago

As a temporary workaround, you can suppress this in lint in your lint XML config

<lint>
    <!-- Temporary until https://github.com/Kotlin/kotlinx.coroutines/issues/2004 is resolved. -->
    <issue id="InvalidPackage">
        <ignore path="**/kotlinx-coroutines-core-*.jar"/>
    </issue>
</lint>
qwwdfsad commented 4 years ago

This is indeed a very unfortunate problem, we never tested lint check. Ignoring core artifact should be completely safe, actual code with java.lang.instrument is never loaded (unless core artifact is used as an argument to -javaagent).

Hopefully, I will release a 1.3.7 release with lint suppression next week

qwwdfsad commented 4 years ago

I've published a fixed version 1.3.6-lint to my bintray and tested it against a bunch of trivial projects.

It would be nice if someone could verify it against their (likely more complex and real-world-ish) projects before we release it as 1.3.7.

sorinirimies commented 4 years ago

@qwwdfsad I confirm that the 1.3.6-lint works. The Builds went through our CI/CD and our Monorepo project works as expected. We have quite a large codebase that heavily relies on the coroutines core and android lib, and we just refactored our Channels to the new StateFlow. I couldn't observe any abnormalities.

mhernand40 commented 4 years ago

@qwwdfsad I too can confirm that 1.3.6-lint works for my project. All CI checks passed for us. 🙂 🎉

ZacSweers commented 4 years ago

Same here

chrimaeon commented 4 years ago

Works for my projects!

jolaf commented 2 years ago

I'm seeing exactly the same problem on 1.6.1:

../../../../../../.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/kotlinx-coroutines-core-jvm/1.6.1-native-mt/ee5d3def1a25e11bb9450ce73c7dd1d8163ec856/kotlinx-coroutines-core-jvm-1.6.1-native-mt.jar: Error: Invalid package reference in org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm; not included in Android: java.lang.instrument. Referenced from kotlinx.coroutines.debug.AgentPremain. [InvalidPackage]

Explanation for issues of type "InvalidPackage":
This check scans through libraries looking for calls to APIs that are not included in Android.
When you create Android projects, the classpath is set up such that you can only access classes in the API packages that are included in Android.
However, if you add other projects to your libs/ folder, there is no guarantee that those .jar files were built with an Android specific classpath, and in particular, they could be accessing unsupported APIs such as java.applet.
This check scans through library jars and looks for references to API packages that are not included in Android and flags these. This is only an error if your code calls one of the library classes which wind up referencing the unsupported package.

Should I reopen the issue?