From 89fb943733ba25970def9f1b5e302cfc6d7ec349 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sun, 23 May 2021 01:47:40 +0700 Subject: [PATCH] Reinstate Proguard/R8 (#5134) * Reinstate Proguard/R8 According to APK analyzer Before: 18044 classes with 113387 methods in 3 dex files After: 12604 classes with 68010 methods in 2 dex files APK download size is reduced by 2.8 MB * Add debug build type without R8 --- app/build.gradle.kts | 28 ++++++++------- app/proguard-android-optimize.txt | 34 ++++++++++++++++++ app/proguard-rules.pro | 60 +++++++++++++++---------------- 3 files changed, 78 insertions(+), 44 deletions(-) create mode 100644 app/proguard-android-optimize.txt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 5a6d060c17..3a18411715 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -55,20 +55,27 @@ android { named("debug") { versionNameSuffix = "-${getCommitCount()}" applicationIdSuffix = ".debug" + + isShrinkResources = true + isMinifyEnabled = true + proguardFiles("proguard-android-optimize.txt", "proguard-rules.pro") + } + create("debugFull") { // Debug without R8 + initWith(getByName("debug")) + isShrinkResources = false + isMinifyEnabled = false } named("release") { - /*named("postprocessing") { - postprocessing { - isObfuscate = false - isOptimizeCode = true - isRemoveUnusedCode = false - isRemoveUnusedResources = true - } - setProguardFiles(listOf(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")) - }*/ + isShrinkResources = true + isMinifyEnabled = true + proguardFiles("proguard-android-optimize.txt", "proguard-rules.pro") } } + sourceSets { + getByName("debugFull").res.srcDirs("src/debug/res") + } + flavorDimensions("default") productFlavors { @@ -76,9 +83,6 @@ android { buildConfigField("boolean", "INCLUDE_UPDATER", "true") dimension = "default" } - create("fdroid") { - dimension = "default" - } create("dev") { resConfigs("en", "xxhdpi") dimension = "default" diff --git a/app/proguard-android-optimize.txt b/app/proguard-android-optimize.txt new file mode 100644 index 0000000000..ad490a86c4 --- /dev/null +++ b/app/proguard-android-optimize.txt @@ -0,0 +1,34 @@ +-allowaccessmodification +-dontusemixedcaseclassnames +-verbose + +-keepattributes *Annotation* + +-keepclasseswithmembernames,includedescriptorclasses class * { + native ; +} + +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +-keepclassmembers class * implements android.os.Parcelable { + public static final ** CREATOR; +} + +-keep class androidx.annotation.Keep + +-keep @androidx.annotation.Keep class * {*;} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep ; +} + +-keepclasseswithmembers class * { + @androidx.annotation.Keep (...); +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 17c7045c4a..7f26725b13 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -1,29 +1,16 @@ -dontobfuscate -# Extensions may require methods unused in the core app --dontwarn eu.kanade.tachiyomi.** --keep class eu.kanade.tachiyomi.** { public protected private *; } +# Keep extension's common dependencies +-keep,allowoptimization class eu.kanade.tachiyomi.** { public protected *; } +-keep,allowoptimization class kotlin.** { public protected *; } +-keep,allowoptimization class okhttp3.** { public protected *; } +-keep,allowoptimization class rx.** { public protected *; } +-keep,allowoptimization class org.jsoup.** { public protected *; } +-keep,allowoptimization class com.google.gson.** { public protected *; } +-keep,allowoptimization class com.github.salomonbrys.kotson.** { public protected *; } +-keep,allowoptimization class com.squareup.duktape.** { public protected *; } --keep class org.jsoup.** { *; } --keep class kotlin.** { *; } --keep class okhttp3.** { *; } --keep class com.google.gson.** { *; } --keep class com.github.salomonbrys.kotson.** { *; } --keep class com.squareup.duktape.** { *; } - -# Design library --dontwarn com.google.android.material.** --keep class com.google.android.material.** { *; } --keep interface com.google.android.material.** { *; } --keep public class com.google.android.material.R$* { *; } - --keep class com.hippo.image.** { *; } --keep interface com.hippo.image.** { *; } --keepclassmembers class * extends nucleus.presenter.Presenter { - (); -} - -# RxJava 1.1.0 +##---------------Begin: proguard configuration for RxJava 1.x ---------- -dontwarn sun.misc.** -keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* { @@ -39,30 +26,38 @@ rx.internal.util.atomic.LinkedQueueNode consumerNode; } -# ReactiveNetwork --dontwarn com.github.pwittchen.reactivenetwork.** - -## GSON ## +-dontnote rx.internal.util.PlatformDependent +##---------------End: proguard configuration for RxJava 1.x ---------- +##---------------Begin: proguard configuration for Gson ---------- # Gson uses generic type information stored in a class file when working with fields. Proguard # removes such information by default, so configure it to keep all of it. -keepattributes Signature -# Gson specific classes --keep class sun.misc.Unsafe { *; } +# For using GSON @Expose annotation +-keepattributes *Annotation* -# Prevent proguard from stripping interface information from TypeAdapterFactory, +# Gson specific classes +-dontwarn sun.misc.** + +# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory, # JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter) +-keep class * extends com.google.gson.TypeAdapter -keep class * implements com.google.gson.TypeAdapterFactory -keep class * implements com.google.gson.JsonSerializer -keep class * implements com.google.gson.JsonDeserializer +# Prevent R8 from leaving Data object members always null +-keepclassmembers,allowobfuscation class * { + @com.google.gson.annotations.SerializedName ; +} +##---------------End: proguard configuration for Gson ---------- -## kotlinx.serialization ## - +##---------------Begin: proguard configuration for Kotlin Serializer ---------- -keepattributes *Annotation*, InnerClasses -dontnote kotlinx.serialization.AnnotationsKt # core serialization annotations +# kotlinx-serialization-json specific. Add this if you have java.lang.NoClassDefFoundError kotlinx.serialization.json.JsonObjectSerializer -keepclassmembers class kotlinx.serialization.json.** { *** Companion; } @@ -77,3 +72,4 @@ -keepclasseswithmembers class eu.kanade.tachiyomi.** { kotlinx.serialization.KSerializer serializer(...); } +##---------------End: proguard configuration for Kotlin Serializer ---------- \ No newline at end of file