Chapters

Hide chapters

Android App Distribution

First Edition · Android 12 · Kotlin 1.5 · Android Studio Bumblebee

7. Optimizing App Size & Obfusticating the APK
Written by Evana Margain Puig

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You have two options when releasing an app to your users:

  1. You can launch it exactly as it was when you, or one of the developers in your team, typed the last line of code.
  2. You can implement optimizations on your code and app to make it run better and occupy less space in your users’ smartphones.

At this point, you may be thinking the obvious best option is the second one. But it would surprise you how many companies release apps with the default settings because of the time contraints most teams are under.

In the first section of this book, you learned how to release an app with the basics. If you’ve gotten this far, I assume you care for your users. The last chapter was the first step in making a more robust app.

In this chapter, you’ll learn and implement techniques to optimize your app in several ways that will make your app smaller, give it better performance, and overall, provide a better experience for your users.

Why is optimization important?

The answer to this question may come from the word optimization itself. In today’s world, we often talk about optimized things as better or enhanced, but why is it important for your app?

To understand this, take a step back. You probably own a smartphone, and with it, you use several apps. Think about what qualities make you like or dislike an app. Do you dislike apps with a lot of bugs?

When users judge apps and choose which to uninstall, they look for several things. For example, they might look at:

  1. App size if they are looking to free space on their device.
  2. Loading times from screens and actions in the app.
  3. How much battery the app consumes.

These examples pertain to optimization. If you want a happy userbase and want your app to perform well, you’ll want to optimize it.

Terminology

Throughout this chapter, you’ll learn terms that may be confusing or unknown to you. Take a look at some of those terms to establish the base for what you’ll learn.

App size

The term refers to the amount of disk space your app takes up on the user’s phone and the amount of data they’ll require to download it.

Shrinking

Shrinking, as the word implies, refers to making your app smaller. There are two types of shrinking, one for code and another for resources. Take a look at each of them.

Code shrinking

Code shrinking is when you use tools to run through your app code and find what you don’t use. This kind of tool removes unused things in your code, like classes and variables. This process generates a thinner version of your app with only the necessary code. It may sound like magic, but you’ll learn how useful this can be.

Resource shrinking

Based on the definitions above, you may have an idea of the meaning of resource shrinking. As with code shrinking, these tools scan your project’s resources, which have a wide range including images, strings and XML files. Then, they remove the unused ones.

Obfuscation

Obfuscation is a process performed by a program that reduces the names of classes, methods and variables, from your human standard syntax to something like a group of letters and identifiers to make the code smaller. After obfuscation, the process of debugging changes since you won’t know exactly what the class refers to.

Optimization

This step is like code shrinking but involves some more advanced techniques. The tools analyze your code and rewrite some things, so your code works faster.

Best practices for reducing your app size

There are many things you can do to make your app smaller. Options range from reducing image quality to letting the Google Play console build your APK. In this part, you’ll take a look at the recommended options for reducing app size.

Android App Bundles

One of the best things you can do to reduce your app size is to create an Android App Bundle, or AAB. You already did this in Chapter Two.

APK Analyzer

APK Analyzer, a tool included in Android Studio by default, will help you notice the resources that increase your app’s size the most.

Performing an APK analysis

Before running the analysis you need to ensure that the APK you’re using doesn’t have instant run, as this may give you false information. To ensure this, go to Build ▸ Build Bundle(s) / APK(s) ▸ Build APK(s) as shown in the image below.

Understanding APK Analyzer results

What you do with the analysis results will vary greatly depending on your app. Despite that, there’s a couple of important things to observe here:

Other improvements you can do with the analysis

Besides the app specific things that you may get from the analysis, here are some best practices to improve your app size.

Optimizing images

As you may already know, images play a significant role in determining an app’s size. Experienced designers have a variety of non-coding techniques they will do to reduce these sizes. But, as this book is about coding, we are going to take a look at what you can do code-wise to reduce the size of images.

Focus only on certain densities: Generating the resources from the largest DPI

One of the easiest but most ignored things you can do is create your resources for only one density, in particular the biggest one you’ll support.

Bitmap images

Whenever you reference a bitmap, the first thing that may come to mind is one of those old school video games where you have a pixelated image. The reality is most of the images you use on your computer are bitmap formats, such as JPG, PNG and GIF.

PNGs: Portable Network Graphics

If you’ve done development in the past couple of years, it is likely that you have used several PNG resources in your apps. That said, this is a heavy format that contains more detail than you need for an Android application. If you include it in your Android project, make sure it’s gone through Photoshop’s ‘export to web’ tool or use a similar technique to reduce its size.

JPG or PNG

JPG is a bit older than PNG. Generally, there’s no noticeable difference from other formats if your app is not exploring images in detail.

WebP

In 2013, Google introduced this new image type. Don’t worry if you haven’t heard of it. It’s relatively new and not as widespread as it should be. In short, WebP allows you to use lossless compression like you do for PNG, but with savings of 24% to 35% in size. There is also an option to apply lossy compression to a WebP image to make it even smaller.

AAPT: Android Asset Packaging tool

Android Studio has a tool called Android Asset Packaging tool, which runs by default on any project with Android Gradle Plugin 3.0.0 and above.

Vector graphics

You may have noticed that Android offers a wide range of possibilities when using images. Vector graphics, also known as SVGs, are another trending image format often used in Android apps. Android has its specific sub-version of SVGs called Vector Drawables.

Making variations of the same image

This last image optimization tip is as underestimated as it is useful. The simplest example of this is an arrow. Say your app needs a set of arrows as assets, each pointing in a different direction. The most common thing to do is export each of the arrows and upload them as assets. But what if you upload one arrow and reuse the asset by using code to rotate it?

Making changes in your images

Now that you reviewed the best practices for images in Android, you can optimize them for reducing PodPlay’s size.

Converting images to WebP

Navigating through the structure of the analyzer results, go to res ▸ drawable-xxhdpi-v4 ▸ ic_play_arrow_white.png. You will get a preview of the image at the bottom of the APK analyzer.

Large files

The next suggestion is to review large files. A common recommendation is to add the images through a dynamic-feature. You’ll learn how to do this in the next chapter.

Unused resources

Another thing you can do is remove unused resources, but this is not easy to spot through APK Analyzer.

fun myUnusedMethod() {
	// this is a useless method
}

Enabling Proguard / R8

The last recommendation is enabling Proguard or R8. You’ll learn how to do this later in this chapter, so don’t worry about it for now.

Size reductions to make directly in your code

Besides the changes mentioned above, developers can do some recommended optimizations to reduce the size of the APK:

Going one step further in optimization

You now understand the basics of optimizing your apps. At the beginning of the chapter, there was a mention of a so-called ProGuard or R8. In this section, you’ll learn about ProGuard, and its successor R8, which will let you configure your optimization even further.

ProGuard

For a long time in Android, ProGuard was the standard for code obfuscation. If you’re using Android Gradle plugin 3.4.0 or higher, you’re probably not using ProGuard anymore. Even when the loaded file is from ProGuard, you’re using R8.

What is ProGuard?

In short, it’s a code optimizer for Java Bytecode. Even when you’re using Kotlin, the compiler will transform your code into Java, and Google recommends using a code optimizer. ProGuard and R8 are similar, but ProGuard is a bit more complicated.

Using R8: Shrinking, obfuscation and optimization

Shrinking, obfuscation and optimization aren’t enabled by default in newer versions of Android Studio for two main reasons:

android {
  buildTypes {

    // 1
    debug {

      //2
      minifyEnabled true

      //3
      shrinkResources true

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

Key points

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now