Android Proguard编译时间常量

时间:2016-12-12 17:14:26

标签: android proguard

好的,我有这个用例:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (loginManager.isLoggedIn()) {
            if(BuildConfig.IS_FLAVOR_A){
                ActivityA.startActivity(this)
            } else if(BuildConfig.IS_FLAVOR_B){
                ActivityB.startActivity(this)
            }
        } else {
            ActivityC.startActivity(this)
        }
        this.finish()
    }

BuildConfig.IS_FLAVOR_A

是productFlavors中的标志,用于标识风味。 ActivityB只在这里被引用,我希望Proguard能够移除ActivityB及其所有资源,但它不是。

buildTypes {
        release {
            minifyEnabled true
            shrinkResources = true
            signingConfig signingConfigs.APK_Release_Temp
            proguardFiles getDefaultProguardFile('proguard-android.txt'), '.../proguard-rules.pro'
        }

    }

缺少什么?我真的可以做到这一点吗? Flavors编译时常量允许proguard删除未使用的代码,在这种情况下,ActivityB.startActivity(this)

编辑:Proguard规则

-dontobfuscate
#-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*,!code/allocation/variable

# AdvancesWebView
-keep class * extends android.webkit.WebChromeClient { *; }

# RxJava
-dontwarn sun.misc.**

-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
   long producerIndex;
   long consumerIndex;
}

-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode producerNode;
}

-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode consumerNode;
}

# Retrofit2
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

#OKHttp
-dontwarn okio.**

# RxBle
-dontwarn com.polidea.**

# Support v4
-dontwarn android.support.v4.**

#Anko
-dontwarn org.jetbrains.anko.internals.AnkoInternals

#Support v7
-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }

-keep public class * extends android.support.v4.view.ActionProvider {
    public <init>(android.content.Context);
}

# CardView
-keep class android.support.v7.widget.RoundRectDrawable { *; }

#Support Design
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }

#Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
    **[] $VALUES;
    public *;
}

#Moshi
-keepclassmembers class ** {
    @com.squareup.moshi.FromJson *;
    @com.squareup.moshi.ToJson *;
}

1 个答案:

答案 0 :(得分:1)

您需要启用优化。为此,请改用此配置:

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'.../proguard-rules.pro'