【问题标题】:Cannot build release apk with proguard无法使用 proguard 构建发布 apk
【发布时间】:2018-01-20 21:04:34
【问题描述】:

无法使用 proguard 构建发布 apk。没有proguard,一切都会很好。我已经尝试了许多其他在互联网上找到的 proguard 规则,但均未成功。我怀疑问题在于我使用 VectorDrawable 和 minApi 19。

    -ignorewarnings

    # Remove logs
    -assumenosideeffects class android.util.Log {
        public static boolean isLoggable(java.lang.String, int);
        public static int v(...);
        public static int i(...);
        public static int w(...);
        public static int d(...);
        public static int e(...);
    }

    # BaseRecyclerViewAdapterHelper
    -keep class com.chad.library.adapter.** {
        *;
    }
    -keep public class * extends com.chad.library.adapter.base.BaseQuickAdapter
    -keep public class * extends com.chad.library.adapter.base.BaseViewHolder
    -keepclassmembers public class * extends com.chad.library.adapter.base.BaseViewHolder {
        (android.view.View);
    }

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

    # Realm
    -keep class io.realm.annotations.RealmModule
    -keep @io.realm.annotations.RealmModule class *
    -keep class io.realm.internal.Keep
    -keep @io.realm.internal.Keep class *
    -dontwarn javax.**
    -dontwarn io.realm.**

    # Retrofit 2.X
    -dontwarn retrofit2.**
    -keep class retrofit2.** { *; }
    -keepattributes Signature
    -keepattributes Exceptions
    -keepclasseswithmembers class * {
        @retrofit2.http.* ;
    }
    -dontwarn retrofit2.Platform$Java8

    # OkHttp
    -keepattributes Signature
    -keepattributes *Annotation*
    -keep class okhttp3.** { *; }
    -keep interface okhttp3.** { *; }
    -dontwarn okhttp3.**

    # Okio
    -dontwarn okio.**
    -dontwarn org.codehaus.**
    -dontwarn java.nio.**
    -dontwarn java.lang.invoke.**
    -dontwarn rx.**

Android Studio 3.0 Beta 2

Gradle 4.1 版

【问题讨论】:

标签: android gradle build proguard


【解决方案1】:

问题出在 proguard-rules 的路径中

修改了这段代码

proguardFiles getDefaultProguardFile('proguard-android.txt'), '$project.rootDir/tools/proguard-rules.pro'

到这里

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

【讨论】: