Processing Android Studio profiles in multi-library projects

I have an application that uses a library with an external link (that is, the library directory is at the same level as the application, but is not copied inside the application folder). The library refers to the application, and both the library and the application include proguard files. Everything works fine until I build the application. When I built the application, all references to the classes defined in the library were not found - I get the error "I can not find the character ..." with all the imported library classes. As I found, this is because when rebuilding the application, proguard confuses all classes and variables, and therefore the application cannot reference them. I added the following to the build.gradle file:

buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt' } debug { minifyEnabled false } } 

but it seems that when creating the application, the above is not taken into account (or the building is executed in release mode). If I change the above to the following (i.e., disable proguard in release mode),

 buildTypes { release { **minifyEnabled false** proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt' } debug { minifyEnabled false } } 

The application compiles fine.

Is there a solution? Can I enable proguard only when creating a signed application?

Here is the proguard library file:

 -optimizationpasses 5 -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -dontpreverify -verbose -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* -optimizations !method/marking/static -keep public class * extends android.app.Activity -keep public class * extends android.app.Application -keep public class * extends android.app.Service -keep public class * extends android.content.BroadcastReceiver -keep public class * extends android.content.ContentProvider -keep public class * extends android.app.backup.BackupAgentHelper -keep public class * extends android.preference.Preference -keep public class com.android.vending.licensing.ILicensingService -keepclasseswithmembernames class * { native <methods>; } -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet); } -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet, int); } -keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } -dontwarn **CompatHoneycomb -keep class android.support.v4.** { *; } -keep class * extends java.util.ListResourceBundle { protected Object[][] getContents(); } -keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable { public static final *** NULL; } -keepnames @com.google.android.gms.common.annotation.KeepName class * -keepclassmembernames class * { @com.google.android.gms.common.annotation.KeepName *; } -keepnames class * implements android.os.Parcelable { public static final ** CREATOR; } -keep class com.google.android.gms.** { *; } -keep public class com.google.ads.** { public *; } -keep public class com.google.gson.** { public protected *; } -keep public class com.google.ads.internal.** {*;} -keep public class com.google.ads.internal.AdWebView.** {*;} -keep public class com.google.ads.internal.state.AdState {*;} -keep public class com.google.ads.mediation.** { public *; } -keep public class com.google.ads.searchads.** {*;} -keep public class com.google.ads.util.** {*;} -keep class com.google.ads.** -dontwarn com.google.ads.** -keepattributes *Annotation* 

Is the problem a problem of using proguard both in the library and in the application?

+29
android gradle proguard
Jun 13 '15 at 16:25
source share
3 answers

After some searching, I found the answer. If you use external / separate source libraries with your main project / application, you should not use proguard in library modules. Instead, you replace the following:

 buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt' } debug { minifyEnabled false } } 

with the following (in build.gradle library / s):

 buildTypes { release { consumerProguardFiles 'proguard-project.txt' } } 

where proguard-project.txt is the file containing the proguard rules for your library project. When creating the application (in debug or release mode), the compiler will take care of all the rules (in the library and in the application).

+53
Jun 24 '15 at 16:03
source share
— -

I think you need to define proguard rules for your libraries. They are usually found in library documents.

(For example, see my answer here for ButterKnife lib: link )

+1
Jun 13 '15 at 17:30
source share

As @Eric Lafortune mentioned in a post , you can use the following syntax in your library project to enable ProGuard [About]

 android { defaultConfig { consumerProguardFiles 'proguard-rules.txt' } //... } 
0
Sep 17 '19 at 21:30
source share



All Articles