gradle-retrolambda icon indicating copy to clipboard operation
gradle-retrolambda copied to clipboard

Retrolambda plugin 3.3.0 doesn't strip calls to addSuppressed when using try-with-resources

Open thevery opened this issue 8 years ago • 21 comments

lint produces a warning about this issue (and I've checked generated files as well - 3.2.5 ok, 3.3.0 broken)

classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'me.tatarka:gradle-retrolambda:3.3.0'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

minSdkVersion 15

3.2.5 with retrolamda 2.3.0 works ok, too

thevery avatar Sep 22 '16 17:09 thevery

Yes, I noticed this as well. I tried all sorts of permutations and always saw the same errors in lint, which led me to believe that the calls to those methods were actually there.

In one project I was able to just bump the minSdk to 19, because it was acceptable to do so. These calls become valid then.

bensandee avatar Sep 22 '16 17:09 bensandee

@thevery do you have javaVersion JavaVersion.VERSION_1_7 in your retrolambda config by any chance?

@tbsandee that's just Lint warnings, I checked the generated code and the calls to addSuppressed are actually stripped.

technoir42 avatar Sep 27 '16 17:09 technoir42

@zergtmn Any idea why this happens with 3.3.0 but not 3.2.5? I guess that's the confusing thing for me.

bensandee avatar Sep 27 '16 17:09 bensandee

@zergtmn no, just source/target set to 1.8. Let me re-check files again...

@tbsandee as I can see, plugin 3.3.0 generates files to ./app/build/intermediates/transforms/retrolambda/debug/folders/1/1/retrolambda/ but 3.2.5 puts files to other folder: ./app/build/intermediates/classes/debug/

thevery avatar Sep 27 '16 17:09 thevery

@zergtmn another confusing point is that files in ./app/build/intermediates/classes/prod/debug/ doesn't have addSuppressed calls with 3.2.5 but does have with 3.3.0

thevery avatar Sep 27 '16 17:09 thevery

@thevery gradle-retrolambda uses Transform API since 3.3.0 and doesn't modify the original class files produced by javac which in your case are located in ./app/build/intermediates/classes/prod/debug/. Do you see addSuppressed calls in class files produced by retrolambda which are located in ./app/build/intermediates/transforms/retrolambda/debug/folders/1/1/retrolambda/?

technoir42 avatar Sep 27 '16 18:09 technoir42

@zergtmn these files looks ok, let me re-check what files are analyzed by lint

thevery avatar Sep 27 '16 19:09 thevery

Yeah, lint checks app/build/intermediates/classes, but not app/build/intermediates/transforms. Transform API/lint bug?

thevery avatar Sep 27 '16 19:09 thevery

You sure lint isn't doing a source-level check here? https://github.com/evant/android-retrolambda-lombok already has instructions on how to ignore the lint check for try-with-resources pre api 19.

evant avatar Sep 27 '16 20:09 evant

@evant not 100% sure about source, but transforms folder is not checked during lint task. Instructions are applied for sure - 3.2.5 works well.

thevery avatar Sep 27 '16 20:09 thevery

Sample lint error attached: 2016-09-27_23-52-58

thevery avatar Sep 27 '16 20:09 thevery

Ah I see. They must have changed the way they detected it causing the previous filter not to work. Something like (untested)

<issue id="NewApi">
    <ignore regexp="Call requires API level 19 .* java\.lang\.Throwable#addSuppressed"/>
</issue>

should work, though it's certainly odd that lint isn't looking at the transformed class files.

evant avatar Sep 27 '16 21:09 evant

So proper lint.xml is the following

<issue id="NewApi">
    <ignore regexp="Try-with-resources requires API level 19"/>
    <ignore regexp="Call requires API level 19 \(current min is 15\): `java\.lang\.Throwable#addSuppressed`"/>
</issue>

(+default methods if used)

thevery avatar Sep 28 '16 15:09 thevery

A more general solution that isn't tied to minSdkVersion=15:

<issue id="NewApi">
        <ignore regexp="Try-with-resources requires API level 19" />
        <ignore regexp="Call requires API level 19 \(current min is \d+\): `java\.lang\.Throwable#addSuppressed`" />
</issue>

bensandee avatar Sep 28 '16 16:09 bensandee

Hm that's really odd. For me the original configuration from https://github.com/evant/android-retrolambda-lombok seems to be enough:

<issue id="NewApi">
    <ignore regexp="Try-with-resources requires API level 19" />
</issue>

I'm also using plugin 2.2.0 and minSdkVersion 15 like @thevery.

technoir42 avatar Sep 28 '16 16:09 technoir42

@zergtmn I get 2 lint warnings with clean lint.xml - for both try with resources and addSuppressed. Try updating your build tools.

thevery avatar Sep 28 '16 17:09 thevery

I need both suppressions with build tools 24.0.1.

bensandee avatar Sep 28 '16 17:09 bensandee

@thevery I'm already using the latest versions of everything. Can you provide a minimal reproducible example? Edit: Ok, I was able to reproduce it on a different project.

technoir42 avatar Sep 28 '16 17:09 technoir42

FWIW, I tried with build tools 24.0.2 as well and still need both suppressions.

bensandee avatar Sep 28 '16 17:09 bensandee

I hope I don't go too offtopic here, my build is working (right now we don't really use Lint). However, in the AndroidStudio editor it will still underline the try-with-resources red, although I did create the lint.xml file right next to my AndroidManifest.xml and also tried to specify it using

lintOptions { lintConfig file('lint.xml') }

Is there any way to get rid of the red underline in the editor?

benj56 avatar Nov 25 '16 11:11 benj56

@benj56 try placing lint.xml in the same folder as build.gradle.

technoir42 avatar Nov 25 '16 11:11 technoir42