Android Studio proguard处理多库项目 [英] Android Studio proguard handling in multi-library projects

查看:149
本文介绍了Android Studio proguard处理多库项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用外部引用库的应用程序(即库的目录与应用程序处于同一级别 - 它不会复制到应用程序的文件夹中)。库由应用程序引用,库和应用程序都包含proguard文件。一切工作正常,直到我构建应用程序。当我构建应用程序时,所有引用库中定义的类都没有找到 - 我得到'无法找到符号类...)所有库类导入错误。正如我发现的,这是因为重建应用程序时,proguard混淆了所有类和变量,因此应用程序无法引用它们。我已将以下内容添加到我的build.gradle文件中,

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

但是在构建应用程序时似乎不考虑上述情况(或者建筑物是在发布模式下完成)。如果我将上面的内容更改为以下内容(例如,在发布模式下禁用proguard),

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

应用程序编译正常。



有没有解决方法?我只能在创建签名应用程序时启用proguard吗?



这是图书馆的proguard文件:

  -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。**
-twtwarn com.google.ads。**

-keepattributes * Annotation *

这是一个问题,我在库和应用程序中都使用了proguard?

解决方案

经过一番搜索,我找到了答案。如果您在主项目/应用程序中使用外部/独立源库,则不应在库模块上使用proguard。相反,您可以替换以下内容:

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

加入以下内容(在库/库的build.gradle中):

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

其中proguard-project.txt是包含您的库项目的proguard规则的文件。在构建应用程序时(无论是在调试模式还是释放模式下),编译器都会处理所有规则(在库和应用程序中)。

I have an application that uses an externally referenced library (that is, the directory of the library is in the same level as the application - it is not copied inside the application's folder). The library is referenced by the application and both the library and the application include proguard files. Everything works fine until I build the application. When I built the app, all referenced to the classes defined in the library are not found - I get 'cannot find symbol class ...) errors on all imports of library classes. As I found, this is because when rebuilding the application, proguard obfuscates all classes and variables and therefore the application cannot reference them. I have added the following to my build.gradle file,

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

but it seems like when building the application, the above is not taken into consideration (or the building is done 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 to this? Can I only enable proguard when creating a signed application?

Here is the library proguard 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 it a problem that I am using proguard in both the library and the application?

解决方案

After some searching I found the answer. If you are using external/separate source libraries with your main project/application, you should not use a proguard on the 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 the build.gradle of the library/libraries):

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

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

这篇关于Android Studio proguard处理多库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆