Moshi: ParseException when adding ProGuard script

Created on 15 Sep 2018  路  5Comments  路  Source: square/moshi

I've added the following code taken from here.

# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**

-keepclasseswithmembers class * {
    @com.squareup.moshi.* <methods>;
}

-keep @com.squareup.moshi.JsonQualifier interface *

# The name of @JsonClass types is used to look up the generated adapter.
-keepnames @com.squareup.moshi.JsonClass class *

# Retain generated JsonAdapters if annotated type is retained.
-if @com.squareup.moshi.JsonClass class *  # <- This is line 158
-keep class <1>JsonAdapter {
    <init>(...);
    <fields>;
}

When I try to build my app, I get the following error:

Running 'gradlew assembleStagingRelease'...                        
Warning: Exception while processing task java.io.IOException: proguard.ParseException: Unknown option '-if' in line 158 of file '/Users/uer/Documents/my-app/android/app/proguard-rules.pro'

FAILURE: Build failed with an exception.

Line 158 is pointed out on the script above.

Any ideas?

Most helpful comment

For further reference (in case somebody else gets with this problem):

  • Updating Android Plugin to 3.1.4 didn't solve it as they still use ProGuard 5.3.3.
  • I've tried to use newer version of ProGuard like Jake posted above but for some reason it wouldn't work on AGP 3.1.4. Error was the following:

Unable to find method 'proguard.KeepClassSpecification.(ZZZZZZLproguard/ClassSpecification;)V'.

  • Going back to AGP 3.0.1 and manually forcing ProGuard to be 6.0 does the trick.

All 5 comments

if requires ProGuard 6.x or newer. What version of AGP are you using?

Hey @JakeWharton thanks for the quick answer.

I have the following configuration for Gradle, if AGP stands for Android Gradle Plugin
screen shot 2018-09-14 at 20 30 01

I _think_ AGP jumped to ProGuard 6.x in 3.1. You can force old versions to use newer ProGuards by following these instructions: https://sourceforge.net/p/proguard/discussion/182455/thread/89a4d63d/. I would recommend updating though. 3.2 is about to go stable next week!

For further reference (in case somebody else gets with this problem):

  • Updating Android Plugin to 3.1.4 didn't solve it as they still use ProGuard 5.3.3.
  • I've tried to use newer version of ProGuard like Jake posted above but for some reason it wouldn't work on AGP 3.1.4. Error was the following:

Unable to find method 'proguard.KeepClassSpecification.(ZZZZZZLproguard/ClassSpecification;)V'.

  • Going back to AGP 3.0.1 and manually forcing ProGuard to be 6.0 does the trick.

Thank you for reporting the issue @matibzurovski and your hint @JakeWharton.
I tried the resolutionStrategy which you suggested and it worked for me.

Further, I tried to use the proguard-gradle plugin which I already use here. I can confirm that it works - at least for me ^^. Please have look at this project which I currently role out for KotlinConf 2018.

Was this page helpful?
0 / 5 - 0 ratings