Android Project Cleanup for Smaller APKs, Faster Builds, and a Better Development Experience

Oğuzhan Aslan
5 min readFeb 19, 2024

In any software project, development is a continuous process, and over time, the codebase grows and becomes more complex. This complexity can lead to slower build times, larger APKs, and a less productive development experience. In this guide, we will cover some of the practices for keeping your Android project clean and efficient.

1. Remove Unused Resources

One of the easiest ways to reduce the size of your APK is to remove unused resources. Also, they are easy to forget about and can add up over time. If you want to remove all of the unused resources from your project, you can use Refactor > Remove Unused Resourcesfrom the Android Studio menu.

This will find and remove all of the unused resources from your project. For example, if you have an unused drawable like this :

then you can remove it by using Refactor > Remove Unused Resources from the Android Studio menu.

Another way to remove unused resources is to use shrinkResources your build.gradle file. This will not actually remove the resources from your project, but it will remove them from the APK when it is built. Lets see an example of how to use shrinkResources in your build.gradle file :

  buildTypes {
release {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}

debug {
isMinifyEnabled = false
isShrinkResources = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}

then create an apk without shrinkResources and analyze it.

Here you can see that a large portion of the apk size is due to the large image that we added. Now let’s add shrinkResources to our build.gradle file and create an apk again:

buildTypes {
release {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}

debug {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}

here you can see that the apk size has been reduced by drastically by using shrinkResources.

Note: do not forget that R8 can also help to shink app size. Not for removing unused resources but for removing unused code.

One more way to remove, in fact to find unused resources is to use lint. Recall that lint is a static code analysis tool that checks your Android project for potential bugs and optimization improvements. It also finds unused resources and shows them in the “Unused resources” section of the lint report.

Use can generate a lint report by running the following command in the terminal:

./gradlew lint

After this command, lint will generate a report and there you can see the unused resources in the “Unused resources” section.

Unused Resources section of generated lint report

But keep in mind that removing them is up to you, lint will only show you the unused resources.

2. Remove Unused Code

Removing unused code is another way to reduce the size of your APK. It may not be as effective as removing unused resources, but it can still help. At least, it will make your codebase cleaner and easier to maintain.

In order to find unused code, which is basically made up of unused classes, methods, and fields. you can detect them by Analyze > Run Inspection by Name > Unused Symbol from the Android Studio menu.

This will find unused code in your project and show inside a popup window. There you can see the unused code and remove it.

Assume that we have an unused class like this:

class X {
private val y = 0

fun z() = Unit
}

then you can detect it by using Analyze > Run Inspection by Name > Unused Symbol and you can tap the safe delete button on the right side.

After this, you will see that the unused class has been removed from your project.

Moreover, another large portion of an app generally comes from the libraries that we use. If the libraries are not used and kept in the project, they will increase the size of the apk. In order to find unused libraries, you can use Analyze > Run Inspection by Name > Unused Library from the Android Studio menu.

This will find unused libraries in your project and show inside a popup window. There you can see the unused libraries and remove them.

For example lets add “glide” library to our project. then run this analysis.

    implementation("com.github.bumptech.glide:glide:4.16.0")

then when you run Analyze > Run Inspection by Name > Unused Library, you will see that the glide library is not used in the project and you can remove it by clicking the detact button.

In this comprehensive guide, we explore essential practices for optimizing Android projects to achieve smaller APK sizes, faster build times, and an enhanced development experience. I hope that these will help you in your projects and make them smaller and cleaner.

LinkedIn

Love you all.

Stay tune for upcoming blogs.

Take care.

--

--