Search code examples
androidandroid-studioproguardaar

Problem with Proguard and consumer proguard in Android SDK 29


I have searched stackoverflow and proguard related questions and am still stuck.

I created an android .aar library (snifferservice) that holds hardware-related functions and a utility package. I use this for most of my Android apps that connect to this hardware device. I have also an application that only uses the utility package in the library. I'm using Proguard to obfuscate and shrink my main app, NoseBook, and also using it to obfuscate and shrink the aar in the finished application. Since migrating to androidx and changing to target API 29 I can't create a release version because I get an error stating it can't find a common superclass for files which I specifically marked as dontwarn.

I tried playing with the proguard rules - adding and removing the other packages in the aar, and specifically the packages referenced in the error. Nothing works. If I don't apply Proguard to the aar at all everything compiles OK.

My app uses Firebase Uuthentication and Firebase Cloud Functions. I am using Android Studio 3.6.3 on Windows 10. Any ideas would be most appreciated.

The error:

Optimizing (pass 2/5)...
Unexpected error while performing partial evaluation:
  Class       = [weizmann/com/snifferservice/data/SaveDataService]
  Method      = [onCreate()V]
  Exception   = [java.lang.IllegalArgumentException] (Can't find common super class of [android/content/Context] (with 2 known super classes) and [weizmann/com/snifferservice/roomdata/SnifferDatabase] (with 1 known super classes))
Unexpected error while performing partial evaluation:
  Class       = [weizmann/com/snifferservice/services/BluetoothLeService]
  Method      = [onCreate()V]
  Exception   = [java.lang.IllegalArgumentException] (Can't find common super class of [android/content/Context] (with 2 known super classes) and [weizmann/com/snifferservice/roomdata/SnifferDatabase] (with 1 known super classes))
Unexpected error while performing partial evaluation:
  Class       = [weizmann/com/snifferservice/services/CloudDataService]
  Method      = [onHandleIntent(Landroid/content/Intent;)V]
  Exception   = [java.lang.IllegalArgumentException] (Can't find common super class of [android/content/Context] (with 2 known super classes) and [weizmann/com/snifferservice/roomdata/SnifferDatabase] (with 1 known super classes))
Warning: Exception while processing task java.io.IOException: java.lang.RuntimeException: java.lang.IllegalArgumentException: Can't find common super class of [android/content/Context] (with 2 known super classes) and [weizmann/com/snifferservice/roomdata/SnifferDatabase] (with 1 known super classes)
Thread(Tasks limiter_4): destruction

My app build.gradle: compilesdk and targetsdk are 29.

release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            consumerProguardFiles 'consumer-proguard-rules.pro' //If I comment out this line everything works
        }
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

    implementation project(':SnifferService-7.142')

    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.firebaseui:firebase-ui-auth:4.3.1'

    implementation 'com.google.firebase:firebase-core:16.0.9'

    implementation 'com.google.firebase:firebase-functions:17.0.0'
}

My top level build.gradle:

 dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath 'com.google.gms:google-services:4.1.0'
    }

aar build.gradle: compilesdk and targetsdk are 28

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.appcompat:appcompat:1.0.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.1'
    //gson
    implementation 'com.google.code.gson:gson:2.8.0'
    //volley
    implementation 'com.android.volley:volley:1.1.1'
    
    implementation 'net.mabboud:android-tone-player:0.3'

    def room_version = "2.2.5"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
    // Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    // Core library
    androidTestImplementation 'androidx.test:core:1.0.0'

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'

    // Assertions
    androidTestImplementation 'androidx.test.ext:junit:1.0.0'
    androidTestImplementation 'androidx.test.ext:truth:1.0.0'
    androidTestImplementation 'com.google.truth:truth:0.42'

}

top level Proguard rules:

-android
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

# chart
-dontwarn com.github.mikephil.**

#continuous buzzer
-dontwarn net.mabboud.android_tone_player.**

# gson
-dontwarn com.google.gson.**

# android support
-keep class android.support.v4.app.** { *; }
-keep class android.support.v7.app.** { *; }

-keep interface android.support.v4.app.** { *; }

#androidx
-dontwarn com.google.android.material.**
-keep class com.google.android.material.** { *; }

-dontwarn androidx.**
-keep class androidx.** { *; }
-keep interface androidx.** { *; }

# monocontinuous buzzer
-dontwarn weizmann.com.snifferservice.audio.MonoContinuousBuzzer

#gmailsender
-dontwarn java.awt.**
-dontwarn java.beans.Beans
-dontwarn javax.security.**

# don't need device-related stuff
-dontwarn weizmann.com.snifferservice.communication.*
-dontwarn weizmann.com.snifferservice.fragments.*
-dontwarn weizmann.com.snifferservice.supportfragments.*
-dontwarn weizmann.com.snifferservice.services.*
-dontwarn weizmann.com.snifferservice.roomdata.*
-dontwarn weizmann.com.snifferservice.data.*


# This will strip `Log.v`, `Log.d`, and `Log.i` statements and will leave `Log.w` and `Log.e` statements intact.
-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int d(...);
    public static int i(...);
}


-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application

consumer-proguard-rules.pro:

-dontwarn weizmann.com.snifferservice.communication.*
-dontwarn weizmann.com.snifferservice.fragments.*
-dontwarn weizmann.com.snifferservice.supportfragments.*
-dontwarn weizmann.com.snifferservice.services.*
-dontwarn weizmann.com.snifferservice.roomdata.*
-dontwarn weizmann.com.snifferservice.data.*
-keep class weizmann.com.snifferservice.util.** {*;}

I have tried adding to this file also the support.v4 and support.v7 commands and also adding and removing android.content.* and weizmann.com.snifferservice.data.* and weizmann.com.snifferservice.roomdata.* , doesn't help.

EDIT The problem seems to be with the Room Database. These are the Proguard errors:

Warning: weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl: can't find referenced field 'java.util.List mCallbacks' in program class weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl
Warning: weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl: can't find referenced method 'void internalInitInvalidationTracker(androidx.sqlite.db.SupportSQLiteDatabase)' in program class weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl
Warning: weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl: can't find referenced field 'androidx.sqlite.db.SupportSQLiteDatabase mDatabase' in program class weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl
Warning: weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl: can't find referenced method 'void assertNotMainThread()' in program class weizmann.com.snifferservice.roomdata.SnifferDatabase
Warning: weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl: can't find referenced method 'androidx.sqlite.db.SupportSQLiteOpenHelper getOpenHelper()' in program class weizmann.com.snifferservice.roomdata.SnifferDatabase
Warning: weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl: can't find referenced method 'void beginTransaction()' in program class weizmann.com.snifferservice.roomdata.SnifferDatabase
Warning: weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl: can't find referenced method 'void setTransactionSuccessful()' in program class weizmann.com.snifferservice.roomdata.SnifferDatabase
Warning: weizmann.com.snifferservice.roomdata.SnifferDatabase_Impl: can't find referenced method 'void endTransaction()' in program class weizmann.com.snifferservice.roomdata.SnifferDatabase

I have added to my proguard variations on the following, nothing helps.

#this
-dontwarn android.arch.util.paging.CountedDataSource
-dontwarn android.arch.persistence.room.paging.LimitOffsetDataSource

#or this
-keep class androidx.room.**{
    public protected private *;
}
#or this
-keep class * extends androidx.room.RoomDatabase
#or this
-keep class androidx.room.RoomDatabase
#or this and various combinations.
-dontwarn androidx.room.paging.**

Solution

  • What finally worked: The error always appeared on the 2nd optimization pass. I added

    -optimizationpasses 1
    

    to the options at the beginning and the error never appeared.

    I tried various clear cache options, updating/reverting the version of the internal aar, adding removing numerous proguard options, with no success. Worst case also gave a STRING_TOO_LARGE error.

    Thanks JensV for your help!

    Any further ideas to solve this problem would still be appreciated.