typetools / checker-framework

Pluggable type-checking for Java
http://checkerframework.org/
Other
990 stars 347 forks source link

Proguard rules #1678

Closed MahmoudiOussama closed 5 years ago

MahmoudiOussama commented 6 years ago

Hi all, I wanted to ask what proguard rules are needed. I am using Android studio 3.0 and developping an android app.

wmdietl commented 6 years ago

Instructions for using the Checker Framwork with Android Studio are here: https://checkerframework.org/manual/#android-gradle What proguard rules you enable is independent of this. Or are you observing some interaction that doesn't work? More details about what you're trying to do and what problem you are running into would be helpful.

MahmoudiOussama commented 6 years ago

Hi , thank you for your response sir. I ma using Android studio, here is my error : image

mernst commented 6 years ago

We would like to help you, but you have not given enough information to do so. Please give complete information that will permit others to reproduce your problem, by following the instructions at https://checkerframework.org/manual/#reporting-bugs .

MahmoudiOussama commented 6 years ago

Ok here is a detailed description : To use your library "checker-framework" i followed instructions from this link : https://checkerframework.org/manual/#android-gradle

This is my module app build.gradle file :

.
.
.
buildTypes {
        checkTypes {
            javaCompileOptions.annotationProcessorOptions.
                    classNames.add("org.checkerframework.checker.nullness.NullnessChecker")
            // You can pass options like so:
            // javaCompileOptions.annotationProcessorOptions.arguments.put("warns", "")
        }
        release {
            debuggable false
            signingConfig signingConfigs.release
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        checkReleaseBuilds false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

configurations {
    checkerFrameworkAnnotatedJDK {
        description = 'a copy of JDK classes with Checker Framework type qualifers inserted'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:27.0.1'
    compile 'com.android.support:appcompat-v7:27.0.1'
    compile 'com.android.support:design:27.0.1'
    compile 'com.android.support:recyclerview-v7:27.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-auth:11.6.0'
    compile 'com.google.firebase:firebase-database:11.6.0'
    compile 'com.google.firebase:firebase-messaging:11.6.0'
    compile 'com.google.android.gms:play-services-auth:11.6.0'
    compile 'com.github.bumptech.glide:glide:3.8.0'
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'
    compile 'com.thoughtbot:expandablerecyclerview:1.0'
    compile 'com.jaredrummler:material-spinner:1.2.1'
    testCompile 'junit:junit:4.12'
    ext.checkerFrameworkVersion = '2.2.2'
    implementation "org.checkerframework:checker-qual:${checkerFrameworkVersion}"
    annotationProcessor "org.checkerframework:checker:${checkerFrameworkVersion}"
    checkerFrameworkAnnotatedJDK "org.checkerframework:jdk8:${checkerFrameworkVersion}"
}

gradle.projectsEvaluated {
    tasks.withType(JavaCompile).all { compile ->
        if (compile.name.contains("CheckTypes")) {
            compile.options.compilerArgs += [
                    "-Xbootclasspath/p:${configurations.checkerFrameworkAnnotatedJDK.asPath}"
            ]
        }
    }
}

apply plugin: 'com.google.gms.google-services'

If i try to execute the app using the checkTypes build variant : image

The checker works perfectly, showing some errors on my code, explaining what are the errors in details. And thank you for the great work developping it.

So now, i want to generate a signed apk file to submit in google console, i go to : image

But, unfortunately, android studion won't generate the apk file, showing those errors : image

Now, if i try to put lines related to checker-framework in comments like this :

...
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:27.0.1'
    compile 'com.android.support:appcompat-v7:27.0.1'
    compile 'com.android.support:design:27.0.1'
    compile 'com.android.support:recyclerview-v7:27.0.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-auth:11.6.0'
    compile 'com.google.firebase:firebase-database:11.6.0'
    compile 'com.google.firebase:firebase-messaging:11.6.0'
    compile 'com.google.android.gms:play-services-auth:11.6.0'
    compile 'com.github.bumptech.glide:glide:3.8.0'
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'
    compile 'com.thoughtbot:expandablerecyclerview:1.0'
    compile 'com.jaredrummler:material-spinner:1.2.1'
    testCompile 'junit:junit:4.12'
    /*ext.checkerFrameworkVersion = '2.2.2'
    implementation "org.checkerframework:checker-qual:${checkerFrameworkVersion}"
    annotationProcessor "org.checkerframework:checker:${checkerFrameworkVersion}"
    checkerFrameworkAnnotatedJDK "org.checkerframework:jdk8:${checkerFrameworkVersion}"**/
}

/*gradle.projectsEvaluated {
    tasks.withType(JavaCompile).all { compile ->
        if (compile.name.contains("CheckTypes")) {
            compile.options.compilerArgs += [
                    "-Xbootclasspath/p:${configurations.checkerFrameworkAnnotatedJDK.asPath}"
            ]
        }
    }
}**/

Everything goes nice, and the signed apk file is generated.

Question is : is this the normal behavior, i mean, before generating the signed apk, i must disable checker-framework from build.gradle ?

mernst commented 6 years ago

We still have no way to reproduce this problem.

My guess is that maybe it's not possible to create signed classfiles that contains Checker Framework classes. Android desugars everything to Java 7 and then dexes it. Classes such as javax.lang.model.type.TypeKind are not defined in Android, and this could be the problem. This is just a guess, though, because we don't have enough information to reproduce your problem.

Most likely this can be reproduced by adding checker-qual.jar (and one use of one of its packages) to any Android package, and try to build a signed APK.

We will investigate whether that is the case, as time permits. It's good that you have a workaround for now.

If other people can provide more details, that would be helpful.

MahmoudiOussama commented 6 years ago

Thank you very much for the answer. I can assure you that i provided all i could (i don't own the app). Maybe other users will face the same problem if anyone try to generate signed apk to release in play store. Sorry for your time.

mernst commented 6 years ago

You have helped us to understand that the problem is creating a signed APK. That's a use case we hadn't yet tried, and we didn't realize it would be problematic. This is helpful information. We'll further investigate it and fix it as soon as we get a chance.

Thanks!

prizoff commented 6 years ago

I think the problem is not in creating a signed APK but in using Proguard (minifyEnabled true in build.gradle) I have similair problems... a little bit different class name (my case: Warning:afu.org.checkerframework.checker.experimental.tainting_qual.qual.Untainted: can't find referenced class afu.com.sun.source.tree.Tree$Kind and others) causing problems... But when I turn off the usage of Proguard - all compiles without problem. May be it is related to using of Google Play Services and/or support library updated versions, but I am not sure.

Currently, I use these libraries:

dependencies { compile 'com.google.firebase:firebase-crash:11.6.2' compile 'com.android.support:appcompat-v7:27.0.2' compile 'com.android.support:support-v13:27.0.2' compile 'com.android.support:design:27.0.2' compile 'com.google.android.gms:play-services-analytics:11.6.2' compile 'com.facebook.android:facebook-android-sdk:4.28.0' compile 'com.google.guava:guava:23.5-android' compile 'com.android.support:multidex:1.0.2' }

and these ones too:

dependencies { compile 'com.google.android.gms:play-services-ads:11.6.2' compile 'com.facebook.android:audience-network-sdk:4.+' compile 'com.google.ads.mediation:facebook:4.26.0.0' compile 'com.amazon.android:mobile-ads:5.8.1.1' }

If anyone does know the solution it would be very appreciated...

mernst commented 6 years ago

Thanks for the added details; that's useful. The Checker Framework developers don't use Proguard, but it must behave differently than tools that we do use. Your information will help whoever investigates this.

prizoff commented 6 years ago

Hmm... In my case using the proper Proguard config for the Guava library fixed the problem: https://stackoverflow.com/a/47555897/1048087

So, it has nothing common with checker-framework.

KimiChiu commented 5 years ago

Did you resolve this problem?

wmdietl commented 5 years ago

The last comment before yours indicates that this is a Proguard configuration issue. I'll close this issue now (we must have missed doing so back then). Let us know if you run into the same issue and the suggested solution doesn't work.

bpappin commented 5 years ago

I'm having the same issue, there should be a ProGuard config for this library.

mernst commented 5 years ago

@bpappin Can you contribute that config?

wmdietl commented 5 years ago

@bpappin An earlier comment pointed to simply using the Proguard config for Guava. Is that approach not working for you?

wosiu commented 5 years ago

I've got similar problem with tensorlite lib which aparetly uses checker-framework: Warning: org.tensorflow.lite.Interpreter: can't find referenced class org.checkerframework.checker.nullness.qual.NonNull I agree with @bpappin that thre should be a ProGouard config for libraries like this. Such line in proguard-rules.pro in this project might help:

-keepattributes *Annotation*

More details for keepattributes here: https://www.guardsquare.com/en/products/proguard/manual/usage/attributes

and nice generall intro, which mentions about similar problems: https://medium.com/androiddevelopers/practical-proguard-rules-examples-5640a3907dc9

mernst commented 5 years ago

Just to clarify @wosiu's comment (thanks!), the change needs to be made in the project that uses the Checker Framework. That's where the proguard-rules.pro file appears.