Home » Android » android – Proguard on multimodule app

android – Proguard on multimodule app

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have an app that consists of following modules:

/app
/common
/customviews
/model

Now I want to obfuscate my code for production, so I put the following lines on all my build.gradle files for all modules.

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

This file contains the following rules:

-dontnote android.net.http.*
-dontnote org.apache.commons.codec.**
-dontnote org.apache.http.**


-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**
-dontwarn org.conscrypt.**
-dontwarn retrofit2.Platform$Java8


-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

The app compiles fine, however when running it I get the following exception:

b.g.b.a.ac: Incorrect resolution sequence for Java constructor public constructor a() defined in com.myexample.myapp.model.a.e.a[[email protected]] (b.g.b.a.a.d.a.f.a.j: class com.myexample.myapp.model.a.e.a)
       at b.g.b.a.ai.a(Unknown Source)
       at b.g.b.a.o.<init>(Unknown Source)
       at b.g.b.a.l$a$e.b(Unknown Source)
       at b.g.b.a.l$a$e.a(Unknown Source)
       at b.g.b.a.ae$a.a(Unknown Source)
       at b.g.b.a.ae$c.a(Unknown Source)
       at b.g.b.a.l$a.e(Unknown Source)
       at b.g.b.a.l.h(Unknown Source)
       at b.g.a.c.a(Unknown Source)
       at com.b.a.r.a(Unknown Source)
       at com.b.a.v.a(Unknown Source)
       at com.b.a.d$1.a(Unknown Source)
       at com.b.a.d$1.a(Unknown Source)
       at com.b.a.v.a(Unknown Source)
       at e.b.a.a.a(Unknown Source)
       at e.n.a(Unknown Source)
       at e.n.b(Unknown Source)
       at e.o$a.c(Unknown Source)
       at e.o$a.a(Unknown Source)
       at e.n.a(Unknown Source)
       at e.n$1.invoke(Unknown Source)
       at java.lang.reflect.Proxy.invoke(Proxy.java:813)
       at $Proxy1.a(Unknown Source)
       at com.myexample.myapp.model.b.a.d.a(Unknown Source)
       at com.myexample.myapp.ui.splash.SplashViewModel.f(Unknown Source)
       at com.myexample.myapp.ui.splash.SplashActivity.k(Unknown Source)
       at com.myexample.myapp.ui.splash.SplashActivity.onCreate(Unknown Source)
       at android.app.Activity.performCreate(Activity.java:6912)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
       at android.app.ActivityThread.-wrap14(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6692)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

I am using Kotlin by the way.

EDIT: new exception:

6-13 10:17:37.884 24129-24129/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp.android, PID: 24129
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.android/com.myapp.android.ui.splash.SplashActivity}: java.lang.IllegalArgumentException: Unable to create converter for class com.myapp.android.model.api.userprofile.UserProfile
        for method d.a
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalArgumentException: Unable to create converter for class com.myapp.android.model.api.userprofile.UserProfile
        for method d.a
        at retrofit2.n$a.a(ServiceMethod.java:755)
        at retrofit2.n$a.c(ServiceMethod.java:741)
        at retrofit2.n$a.a(ServiceMethod.java:172)
        at retrofit2.m.a(Retrofit.java:170)
        at retrofit2.m$1.invoke(Retrofit.java:147)
        at java.lang.reflect.Proxy.invoke(Proxy.java:913)
        at $Proxy1.a(Unknown Source)
        at com.myapp.android.model.repository.impl.c.a(UserRepositoryImpl.kt:23)
        at com.myapp.android.ui.splash.SplashViewModel.f(SplashViewModel.kt:20)
        at com.myapp.android.ui.splash.SplashActivity.m(SplashActivity.kt:58)
        at com.myapp.android.ui.splash.SplashActivity.onCreate(SplashActivity.kt:31)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
            ... 9 more
     Caused by: java.lang.IllegalStateException: Resource not found in classpath: kotlin/collections/collections.kotlin_builtins
        at kotlin.reflect.jvm.internal.impl.builtins.BuiltInsPackageFragmentProviderKt.a(builtInsPackageFragmentProvider.kt:39)
        at kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns.c(KotlinBuiltIns.java:138)
        at kotlin.reflect.jvm.internal.impl.platform.JvmBuiltIns.<init>(JvmBuiltIns.kt:56)
        at kotlin.reflect.jvm.internal.impl.platform.JvmBuiltIns.<init>(JvmBuiltIns.kt:31)
        at kotlin.reflect.jvm.internal.impl.load.kotlin.reflect.RuntimeModuleData$Companion.a(RuntimeModuleData.kt:47)
        at kotlin.reflect.jvm.internal.ModuleByClassLoaderKt.a(moduleByClassLoader.kt:58)
        at kotlin.reflect.jvm.internal.KDeclarationContainerImpl$Data$moduleData$2.b(KDeclarationContainerImpl.kt:37)
        at kotlin.reflect.jvm.internal.KDeclarationContainerImpl$Data$moduleData$2.a(KDeclarationContainerImpl.kt:34)
        at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.a(ReflectProperties.java:93)
        at kotlin.reflect.jvm.internal.ReflectProperties$Val.a(ReflectProperties.java:32)
        at kotlin.reflect.jvm.internal.KDeclarationContainerImpl$Data.m(Unknown Source:7)
        at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.b(KClassImpl.kt:42)
        at kotlin.reflect.jvm.internal.KClassImpl$Data$descriptor$2.a(KClassImpl.kt:39)
        at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.a(ReflectProperties.java:93)
        at kotlin.reflect.jvm.internal.ReflectProperties$Val.a(ReflectProperties.java:32)
        at kotlin.reflect.jvm.internal.KClassImpl$Data.a(Unknown Source:7)
        at kotlin.reflect.jvm.internal.KClassImpl.f(KClassImpl.kt:159)
        at kotlin.reflect.jvm.internal.KClassImpl.b(KClassImpl.kt:173)
        at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.b(KClassImpl.kt:86)
        at kotlin.reflect.jvm.internal.KClassImpl$Data$constructors$2.a(KClassImpl.kt:39)
        at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.a(ReflectProperties.java:93)
06-13 10:17:37.885 24129-24129/? E/AndroidRuntime:     at kotlin.reflect.jvm.internal.ReflectProperties$Val.a(ReflectProperties.java:32)
        at kotlin.reflect.jvm.internal.KClassImpl$Data.e(Unknown Source:7)
        at kotlin.reflect.jvm.internal.KClassImpl.i(KClassImpl.kt:192)
        at kotlin.reflect.full.KClasses.a(KClasses.kt:40)
        at com.squareup.moshi.k.a(KotlinJsonAdapter.kt:160)
        at com.squareup.moshi.n.a(Moshi.java:100)
        at retrofit2.a.a.a.a(MoshiConverterFactory.java:91)
        at retrofit2.m.a(Retrofit.java:330)
        at retrofit2.m.b(Retrofit.java:313)
        at retrofit2.n$a.c(ServiceMethod.java:739)
            ... 22 more
How to&Answers:

Proguard is only relevant for the actual release. So instead of adding minifyEnabled true for every module, only add it for to the release configuration of the app itself.

All code coming from libraries (regardless if from a module or an external dependency) will be obfuscated with the code of the application.

If you have custom rules you’d like to share with your library, you can define the consumerProguardFiles 'proguard-rules.pro' to be added to the AAR file.

Answer:

If you want to add Proguard file to library module, you should add it to the correct property which is consumerProguardFiles and not proguardFiles, inside the defaultConfig block of your library’s build.gradle file. E.g.

android {
    defaultConfig {
        minifyEnabled true
        consumerProguardFiles 'proguard-rules.pro'
    }
    ...
}

proguardFiles works for application module and not with library modules.

Answer:

The initial error you were getting is similar to the one described in KotlinReflectionInternalError when attempting to create an adapter for a data class, and can be solved by adding the following to the ProGuard rules:

-keepclassmembers class kotlin.Metadata {
   public <methods>;
}

For the second error, add this rule to ProGuard:

-keep class kotlin.reflect.jvm.internal.** { *; }