Skip to content

Commit 7465d80

Browse files
committed
feat: optimize build configuration and ProGuard rules for reduced APK size
1 parent 9ec6f29 commit 7465d80

File tree

2 files changed

+160
-104
lines changed

2 files changed

+160
-104
lines changed

app/build.gradle.kts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ android {
1010
abi {
1111
isEnable = true
1212
reset()
13-
include("armeabi-v7a", "arm64-v8a") // Adjust as needed
14-
isUniversalApk = true // This is key for your universal APK
13+
include("armeabi-v7a", "arm64-v8a") // Only include the architectures you need
14+
isUniversalApk = true // Create a universal APK with all architectures
1515
}
1616
}
1717

18-
1918
defaultConfig {
2019
applicationId = "com.fadcam"
2120
minSdk = 28
@@ -24,14 +23,21 @@ android {
2423
versionName = "1.4.0"
2524

2625
// testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
26+
27+
// Enable vector drawable support
28+
vectorDrawables.useSupportLibrary = true
2729
}
2830

2931
buildTypes {
3032
release {
31-
// Disable R8/ProGuard completely for testing
32-
isMinifyEnabled = false
33-
isShrinkResources = false
33+
// Enable R8 optimization with custom rules
34+
isMinifyEnabled = true
35+
isShrinkResources = true
3436
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
37+
38+
// More aggressive optimizations for release
39+
isDebuggable = false
40+
isCrunchPngs = true // Aggressively optimize PNG files
3541
}
3642
}
3743

@@ -48,6 +54,7 @@ android {
4854

4955
// Proper resource handling
5056
android.aaptOptions.noCompress += listOf("xml")
57+
android.aaptOptions.cruncherEnabled = true // Enable PNG cruncher
5158

5259
// Generate R class for the AppLock library
5360
android.namespace = "com.fadcam"
@@ -62,9 +69,45 @@ android {
6269
getByName("test").java.srcDirs("none")
6370
getByName("androidTest").java.srcDirs("none")
6471
}
72+
73+
// Exclude specific ABIs from the FFmpeg library to reduce size
74+
packagingOptions {
75+
// Exclude unnecessary architectures
76+
jniLibs {
77+
excludes += listOf("**/x86/**", "**/x86_64/**", "**/mips/**", "**/mips64/**")
78+
}
79+
80+
// Exclude unnecessary files from the APK
81+
resources {
82+
excludes += listOf(
83+
"META-INF/LICENSE",
84+
"META-INF/LICENSE.txt",
85+
"META-INF/NOTICE",
86+
"META-INF/NOTICE.txt",
87+
"META-INF/DEPENDENCIES",
88+
"META-INF/*.kotlin_module",
89+
"META-INF/AL2.0",
90+
"META-INF/LGPL2.1",
91+
"**/*.kotlin_metadata",
92+
"**/*.kotlin_builtins",
93+
"**/*.proto"
94+
)
95+
}
96+
}
97+
98+
// Enable resource optimization
99+
androidResources {
100+
additionalParameters += listOf("--no-version-vectors")
101+
}
102+
103+
// Enable build config optimization
104+
buildFeatures {
105+
buildConfig = true
106+
}
65107
}
66108

67109
dependencies {
110+
// Use implementation instead of api for all dependencies to reduce APK size
68111

69112
implementation(libs.appcompat)
70113
implementation(libs.material)

app/proguard-rules.pro

Lines changed: 111 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
# For more details, see
66
# http://developer.android.com/guide/developing/tools/proguard.html
77

8+
# Aggressive optimization settings
9+
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*,!code/allocation/variable
10+
-optimizationpasses 7
11+
-allowaccessmodification
12+
-dontusemixedcaseclassnames
13+
-dontskipnonpubliclibraryclasses
14+
-verbose
15+
816
# If your project uses WebView with JS, uncomment the following
917
# and specify the fully qualified class name to the JavaScript interface
1018
# class:
@@ -26,33 +34,45 @@
2634
-keep class com.fadcam.data.** { <fields>; }
2735
-keep class com.fadcam.trash.** { <fields>; }
2836

29-
# Keep menu resources and drawables
30-
-keep class **.R$drawable
31-
-keep class **.R$menu
32-
-keep class **.R$layout
33-
-keep class **.R$id
34-
-keepclassmembers class **.R$* {
35-
public static <fields>;
36-
}
37-
38-
# More specific rules for preserving resources referenced in XML
39-
-keepclasseswithmembers class **.R$drawable {
40-
public static final int ic_*;
41-
}
42-
43-
# Keep all drawable resources
44-
-keep public class * extends android.graphics.drawable.Drawable
45-
-keepclassmembers class * extends android.graphics.drawable.Drawable {
46-
<init>(...);
37+
# Keep only necessary resources - this is critical for icons to display properly
38+
# Instead of keeping all resources, keep only what's needed
39+
-keep class **.R$drawable { *; }
40+
-keep class **.R$mipmap { *; }
41+
-keep class **.R$id { *; }
42+
-keep class **.R$menu { *; }
43+
-keep class **.R$layout { *; }
44+
-keep class **.R$string { *; }
45+
-keep class **.R$style { *; }
46+
-keep class **.R$styleable { *; }
47+
-keep class **.R$color { *; }
48+
-keep class **.R$dimen { *; }
49+
-keep class **.R$attr { *; }
50+
-keep class **.R$bool { *; }
51+
-keep class **.R$integer { *; }
52+
-keep class **.R$array { *; }
53+
-keep class **.R$raw { *; }
54+
-keep class **.R$xml { *; }
55+
56+
# FFmpeg specific rules - more aggressive
57+
-keep class com.arthenica.ffmpegkit.FFmpegKit { *; }
58+
-keep class com.arthenica.ffmpegkit.FFmpegKitConfig { *; }
59+
-keep class com.arthenica.ffmpegkit.FFprobeKit { *; }
60+
-keep class com.arthenica.ffmpegkit.MediaInformation { *; }
61+
-keep class com.arthenica.ffmpegkit.MediaInformationSession { *; }
62+
-keep class com.arthenica.ffmpegkit.ReturnCode { *; }
63+
-keep class com.arthenica.ffmpegkit.Session { *; }
64+
-keep class com.arthenica.ffmpegkit.SessionState { *; }
65+
-keep class com.arthenica.ffmpegkit.Statistics { *; }
66+
-keep class com.arthenica.smartexception.java.Exceptions { *; }
67+
-dontwarn com.arthenica.ffmpegkit.**
68+
-keep class com.fadcam.utils.FFmpegUtil { *; }
69+
70+
# Keep only essential drawable resources
71+
-keep public class * extends android.graphics.drawable.Drawable {
72+
public <init>(...);
4773
}
4874

49-
# Keep all menu-related resources and layouts
50-
-keep public class * extends android.view.Menu
51-
-keep public class * extends android.view.MenuItem
52-
-keep public class * extends android.widget.PopupMenu
53-
-keep public class * extends android.view.ContextMenu
54-
55-
# Keep custom views
75+
# Keep only essential view methods
5676
-keep public class * extends android.view.View {
5777
public <init>(android.content.Context);
5878
public <init>(android.content.Context, android.util.AttributeSet);
@@ -65,80 +85,73 @@
6585
native <methods>;
6686
}
6787

68-
# Keep all resources referenced from XML layouts
69-
-keepclasseswithmembers class * {
70-
public <init>(android.content.Context, android.util.AttributeSet);
71-
}
72-
73-
-keepclasseswithmembers class * {
74-
public <init>(android.content.Context, android.util.AttributeSet, int);
88+
# Keep only essential AndroidX components
89+
-keep class androidx.appcompat.widget.** { *; }
90+
-keep class androidx.appcompat.app.** { *; }
91+
-keep class androidx.core.widget.** { *; }
92+
-keep class androidx.fragment.app.** { *; }
93+
-keep class androidx.recyclerview.widget.** { *; }
94+
-keep class androidx.viewpager2.widget.** { *; }
95+
-keep class androidx.constraintlayout.widget.** { *; }
96+
-keep class androidx.coordinatorlayout.widget.** { *; }
97+
-keep class androidx.swiperefreshlayout.widget.** { *; }
98+
-keep class androidx.camera.** { *; }
99+
-dontwarn androidx.**
100+
101+
# Keep Material components - only essential ones
102+
-keep class com.google.android.material.bottomsheet.** { *; }
103+
-keep class com.google.android.material.bottomnavigation.** { *; }
104+
-keep class com.google.android.material.navigation.** { *; }
105+
-keep class com.google.android.material.floatingactionbutton.** { *; }
106+
-keep class com.google.android.material.snackbar.** { *; }
107+
-keep class com.google.android.material.tabs.** { *; }
108+
-dontwarn com.google.android.material.**
109+
110+
# Preserve attributes for Android runtime - only essential ones
111+
-keepattributes SourceFile,LineNumberTable,*Annotation*,Signature,InnerClasses,EnclosingMethod
112+
113+
# Keep Lottie animations - only essential classes
114+
-keep class com.airbnb.lottie.LottieAnimationView { *; }
115+
-keep class com.airbnb.lottie.LottieDrawable { *; }
116+
-keep class com.airbnb.lottie.LottieComposition { *; }
117+
-keep class com.airbnb.lottie.LottieCompositionFactory { *; }
118+
-keep class com.airbnb.lottie.LottieResult { *; }
119+
-dontwarn com.airbnb.lottie.**
120+
121+
# Keep ExoPlayer - only essential classes
122+
-keep class com.google.android.exoplayer2.ExoPlayer { *; }
123+
-keep class com.google.android.exoplayer2.SimpleExoPlayer { *; }
124+
-keep class com.google.android.exoplayer2.ui.** { *; }
125+
-keep class com.google.android.exoplayer2.source.** { *; }
126+
-keep class com.google.android.exoplayer2.extractor.** { *; }
127+
-keep class com.google.android.exoplayer2.upstream.** { *; }
128+
-keep class com.google.android.exoplayer2.decoder.** { *; }
129+
-keep class com.google.android.exoplayer2.Format { *; }
130+
-keep class com.google.android.exoplayer2.MediaItem { *; }
131+
-dontwarn com.google.android.exoplayer2.**
132+
133+
# Keep OSMDroid - only essential classes
134+
-keep class org.osmdroid.views.** { *; }
135+
-keep class org.osmdroid.tileprovider.** { *; }
136+
-keep class org.osmdroid.util.** { *; }
137+
-dontwarn org.osmdroid.**
138+
139+
# Remove Android logging code
140+
-assumenosideeffects class android.util.Log {
141+
public static *** d(...);
142+
public static *** v(...);
143+
public static *** i(...);
144+
public static *** w(...);
145+
public static *** e(...);
75146
}
76147

77-
# Preserve the special static fields of all resource classes in the R class
78-
-keepclassmembers class **.R$* {
79-
public static <fields>;
80-
}
81-
82-
# Prevent stripping off any resources/attributes referenced in XML
83-
-keep public class * extends android.content.res.Resources$Theme
84-
-keepclassmembers class * extends android.content.res.Resources$Theme {
85-
<methods>;
86-
}
87-
88-
# Preserve the special static methods that are called by the Android runtime
89-
-keepclassmembers class * {
90-
void <init>(android.content.Context);
91-
}
92-
93-
# Preserve attributes for Android runtime
94-
-keepattributes SourceFile,LineNumberTable
95-
96-
# If you keep the line number information, uncomment this to
97-
# hide the original source file name.
98-
#-renamesourcefileattribute SourceFile
99-
100-
# Keep all model classes used with Gson
101-
-keep class com.faded.fadcam.model.** { *; }
102-
103-
# Keep Gson stuff
104-
-keep class com.google.gson.** { *; }
105-
-keep class com.google.gson.reflect.** { *; }
106-
-keepattributes Signature
107-
-keepattributes *Annotation*
108-
109-
# Keep R classes - prevents resource stripping issues
110-
-keep class **.R
111-
-keep class **.R$* {
112-
<fields>;
113-
}
114-
115-
# Keep drawables
116-
-keep class **.R$drawable
117-
118-
# Keep menus
119-
-keep class **.R$menu
120-
121-
# Keep layouts
122-
-keep class **.R$layout
123-
124-
# Keep AndroidX stuff
125-
-keep class androidx.** { *; }
126-
-keep interface androidx.** { *; }
127-
-keep public class * extends androidx.**
128-
129-
# Keep native methods
130-
-keepclasseswithmembernames class * {
131-
native <methods>;
132-
}
133-
134-
# Keep view stuff
135-
-keep public class * extends android.view.View {
136-
public <init>(android.content.Context);
137-
public <init>(android.content.Context, android.util.AttributeSet);
138-
public <init>(android.content.Context, android.util.AttributeSet, int);
139-
public void set*(...);
148+
# Remove System.out/err
149+
-assumenosideeffects class java.io.PrintStream {
150+
public void println(...);
151+
public void print(...);
140152
}
141153

142-
# Only optimize the app minimally
143-
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
144-
-optimizationpasses 2
154+
# Aggressive optimization settings
155+
-repackageclasses ''
156+
-flattenpackagehierarchy ''
157+
-mergeinterfacesaggressively

0 commit comments

Comments
 (0)