iDevie
August 2019
M T W T F S S
« Jul    
 1234
567891011
12131415161718
19202122232425
262728293031  

Categories


Since the Android Marketplace launched in March 2012, the size of the average Android app has quintupled.

Some of this increase makes sense: the memory available to the typical Android device has increased, so why not use this extra memory to deliver a better user experience?

However, there is a point where all of those high-definition graphics, innovative features and rich, multimedia content have a negative impact on the user experience—no-one wants an app that takes forever to download and then takes a chunk out of their device’s memory! 

There’s even evidence to suggest that there’s an inverse correlation between the size of your APK and the number of people who download your app. According to a recent study, for every 6MB increase in APK size, you can expect a 1% drop in the number of people who install your app after visiting its Google Play page. 

Everything you can do to reduce the size of your APK will encourage more users to download your app, so in this article I’ll show you how to create a trimmer, slimmer APK and ensure your app winds up on as many devices as possible. 

Android App Bundles: Support More Devices, While Reducing APK Size

Whenever you develop an Android app, you’ll want to ensure that app is compatible with as many different devices as possible, by providing code and resources that are optimized for different screen densities, CPU architectures, languages, and many other factors. Unfortunately, this also means that every single person who downloads your app will receive a ton of code and resources that they’re never going to use.

Supporting as many devices as possible without dramatically increasing the size of your APK has long been a problem for Android developers. However, the Android team are aiming to change all this, with a new publishing format that’s designed to help you support the full range of Android devices, while reducing the size of your APK. According to the official Android Developers blog, by publishing your app in this new Android App Bundle (.aab) format, you should be able to reduce the size of your APK by an average of 35%. 

Once you’ve uploaded your app as an .aab file, Google Play will use this single file to generate:

  • A base APK containing all the code and resources required to deliver your app’s base functionality. Everyone who downloads your app will receive the same base APK. 
  • Multiple configuration APKs, which are tailored to different device configurations. Every time someone downloads your app, they’ll receive a configuration APK that’s tailored to their specific device and contains only the code and resources they actually need. 

To build your app as a signed Android App Bundle, first select Build > Generate Signed Bundle/APK from the Android Studio toolbar. Then select the Android App Bundle checkbox, and then click Next. Now, open the module dropdown, and choose app as your base module.

Follow the onscreen instructions to complete the build process. Android Studio will now generate an .aab file and store it in your computer’s AndroidAppBundle/app/release directory.

When you’re ready to upload your .aab file to Google Play, just head over to the Google Play Console and select Create Application.

You’ll be asked to enter some basic information about your app and then click Create, followed by Save. In the Console’s left-hand menu, select App releases. Decide which track you want to upload this app to, and then select its accompanying Manage button and click Create release.

When prompted, enroll in App Signing by Google Play, which is mandatory for using the App Bundle format. Select Browse files and then select the .aab file that you want to upload.

Upload your Android App Bundle to the Google Play Console

Finally, click Save to save your configuration.

Once you’ve uploaded your app to the Console, you can see how much space you’ve saved by using the App Bundle format. To do so, select App releases from the left-hand menu. Find the track where you uploaded your Bundle, and then select Edit release then click to expand the Android App Bundle section.

Select Explore App Bundle, and you’ll see a rough estimate of the space you’ve saved, expressed as a percentage. 

Check how much space youve saved your users by using the aab format

Deliver Features on Demand, With Dynamic Feature Modules 

It’s not unusual for a project to have one or more features that aren’t necessary to deliver that application’s core functionality. You can often reduce the size of your APK by separating these features into optional dynamic feature APKs, which the user can then download on demand, as and when required.

To create a dynamic feature module, select File > New > New Module… from the Android Studio toolbar. Select Dynamic Feature Module, followed by Next

From here, you can open the Base application module dropdown and select your base module. Give your module a name, and then click Next. Select the Enable on-demand checkbox. If your project supports Android 4.4 or earlier, then you’ll also need to enable Fusing, as this will make your dynamic feature module available to these devices as a multi-APK. 

Select the Enable on-demand and Fusing checkboxes

Finally, give your module a public name, which will be visible to the end user. Click Finish

Enabling Access to the Dynamic Feature Module

Once you’ve created a dynamic feature module, you’ll need to give users a way to request it at runtime. Open your module-level build.gradle file, and add the Google Play Core library as a project dependency: 

Open the Activity or Fragment where you want to load your dynamic feature module, and then add the following: 

You can now add code and resources to your dynamic feature module, and users will be able to request the module at runtime.

Try Before You Buy: Offer an Instant Apps APK 

It may not be an APK in the traditional, installable sense, but you can offer part of your application as a lightweight, standalone Instant Apps APK that users can then load on demand. For anyone who’s struggling with extremely limited storage or a restrictive data plan, Instant Apps may be the only viable way for them to experience your application. 

In our three-part Instant Apps series, we created a multi-feature Instant Apps APK that was mapped to a unique URL. URL mapping makes your Instant App accessible from any location on the web that supports hyperlinks, including forums, tweets, Facebook posts, and blogs. However, URL mapping is also a multi-step process that requires you to own a web domain where you can host your project’s assetlinks.json file. 

In the latest versions of Android Studio, creating an Instant Apps APK just got easier, as it’s now possible to create an Instant App that isn’t mapped to any URL, removing all of the complexity associated with URL mapping.

Once you’ve published your URL-less Instant App, users will be able to access it via a Try Now button on your app’s Google Play listing. 

The BuzzFeed app is one example of an Android Instant App

To create these kind of “URL-less” Instant Apps, you’ll need:

  • Android Studio 3.2 or higher.
  • Instant Apps Development SDK 1.3.0 or higher.
  • The latest versions of the Android SDK Build and Platform tools. 

Projects that support Instant Apps have a unique structure. If you practice modularization as part of your standard app development process, then you may only need to make some tweaks to your project, but if your project isn’t modularized, then be prepared to spend some time restructuring it. 

To support Instant Apps, you’ll need to create the following modules:

  • App module. This is the “standard” installable app module.
  • Base module. This contains the code and resources that’ll be used across all of your project’s modules, such as your app’s launcher icons. 
  • Feature module. This contains the code and resources that are required to deliver a stand-alone feature.
  • Instantapp module. This is a container that’ll eventually take each of your project’s feature modules, and transform them into Instant App APKs. 
The structure of an Instant App-enabled project

For a more detailed look at this unique project structure, check out: What Are Android Instant Apps?

If you’re starting a new project, then you’re in luck: you can create a blank project that already has the Instant Apps structure. Simply create a new project as normal, but when prompted, select This project will support instant apps.

When creating a project make sure you select the following checkbox This project will support instant apps

You can then add code and resources to each module, as normal. 

To test your URL-less Instant App, you’ll need a compatible device, which according to the official Android docs is “most” physical devices running Android 5.1 or higher. If you’re using the emulator, then you’ll need an AVD (Android Virtual Device) that’s using Android 8.1 or later, x86 architecture, and includes the Google APIs. You’ll also need to be logged in to a valid Google account on your test device or AVD.

To put your Instant App to the test, select Run > Run… from the Android Studio toolbar and then select Edit Configurations

In the left-hand menu, select instantapp. Since we’re testing a URL-less Instant App, find the URL field and delete all of the text inside it. Now select Run. If the device prompts you to opt into the Instant App program, then tap Yes, I’m in

Your Instant App should now appear onscreen, with no installation and no URL required!

When it’s time to release your app, you can either complete the signing process twice in order to create separate installable and Instant App APKs or you can build your project as an App Bundle. 

Publish an Instant Apps-Enabled App Bundle 

In the Android Studio 3.3 beta 1 and higher, you can combine Instant Apps with App Bundles, to create a single .aab file that features both your installable and Instant App. 

To set your app up to publish an .aab file with an Instant App included, open your project’s instantapp module. 

In this module’s AndroidManifest.xml file, add the following:

Open your app module’s build.gradle file, and ensure your instant variant has a smaller versionCode than its installable counterpart:

Select Build > Generate Signed Bundle / APK from the Android Studio menu bar, and then build your App Bundle as normal, making sure to sign it with a release key. 

Remove All Unused Methods, Classes, Fields, Attributes, and Resources

It’s easy for unused code and resources to find their way into your project, particularly when you’re using libraries. Since they add zero value to your app, you should always track down and remove unnecessary assets, and there are several tools that can help you:

1. Feeling Adventurous? Try the New R8 Code Shrinker

Code shrinking can help you reduce the size of your APK by deleting unused code and resources. 

Android Studio 3.3 beta 1 features a preview of the upcoming R8 code shrinking tool, which also performs desugaring and dexing and, according to Google, shrinks code faster than ProGuard.

To try R8 for yourself, make sure you’re running Android Studio 3.3 beta 1 or higher, and then add the following to your gradle.properties file: 

2. Shrink Your Code With ProGuard 

At the time of writing, R8 was still in preview, so until it’s stable you might prefer to use ProGuard to remove unused classes, methods, fields and attributes from your code.

To enable ProGuard, add the following to your module-level build.gradle file:

Just be aware that ProGuard may remove code that your application actually needs, so you should always test your project with ProGuard enabled, before publishing it. 

3. Enable Resource Shrinking 

Once you’ve enabled code shrinking, you can use resource shrinking to identity and remove unnecessary resources: 

4. Improve Your Code With Android Lint 

Lint is a static scanning tool that can identify resources that aren’t referenced anywhere in your code. However, Lint doesn’t actually remove these resources, so you’ll need to check its report and remove the identified resources manually.

To run Lint, select Analyze > Inspect Code… from the Android Studio toolbar. If Lint detects any unreferenced resources, then it’ll list them in its Inspection Results window.

After running a Lint inspection you can check its findings in the Inspection Results window

Convert Your PNGs, JPEGs and BMPs Into WebP

If your project contains a number of drawables, then compressing them can significantly reduce the size of your APK.

If your project’s minSdkVersion is 18 or higher, then you can compress your PNGs, JPEGs and BMPs by converting them to WebP format.

First, control-click the drawable that you want to convert and select Convert to WebP… Choose between lossy, or lossless encoding. Select Skip images when the encoded result is larger than the original and click OK to convert the image.

Examine Your APK Using Android Studio’s APK Analyzer

Sometimes, you may not be entirely sure what’s caused your APK to grow out of control.

You can identify the biggest contributors towards the size of your APK by using Android Studio’s new APK Analyzer. This tool lets you explore the absolute and relative size of different categories of files, and even the size of individual files, so you’ll know exactly where you should be focusing your efforts.

To analyze an APK, just select Build > Analyze APK from Android Studio’s menu bar.

In the subsequent window, select the APK in question. The APK Analyzer will open in a new panel. 

The APK Analyzer displays the raw and download size of every file that makes up your APK

For each file and folder, the APK Analyzer displays:

  • Raw file size. This is the unzipped size of this item on the disk.
  • Download size. This is the estimated compressed size of the item, as it would be delivered by Google Play.
  • % of total download size. The percentage of the APK’s total download size that this item represents. 

This isn’t the only feature the APK Analyzer has to offer.

Imagine you’ve successfully slashed the size of your APK and released a lightweight application that amasses glowing five-star reviews on Google Play—that doesn’t mean you can stop worrying about APK size! At some point, you’ll want to release an update, which means APK size will once again become a major concern. 

While it’s always a good idea to monitor how your APK evolves from one release to the next, if there’s ever a major increase in APK size then you’ll definitely want to take a closer look at what’s going on. 

You can use the APK Analyzer to highlight all the differences between your current APK and your previous APK, which can help you zero in on areas and assets that you could optimize or even remove completely.

To compare APKs, first select Build > Analyze APK from Android Studio’s menu bar.

Then select the most recent version of your APK. In the upper-right corner of the APK Analyzer, select Compare with previous APK… Next, select the previous version of your APK. A popup should appear, highlighting all the differences between your current APK and your previous APK.

Don’t Forget Android Go!

Emerging markets represent some of the fastest growing smartphone audiences in the world, and the rise of brands like Vivo and Oppo has proven that budget devices have the potential to dominate this rapidly-expanding market. 

To help Android appeal to this entirely new and growing audience, Google have released Android Go, a version of Android with performance and storage improvements designed to provide a better experience on budget-friendly devices, including devices that have less than 1GB of RAM. 

Android Go users have access to the entire Google Play store, but Google highlights apps that are optimized for Android Go, including applications that take up less than 40MB on the user’s device.

By following the advice in this article, you should be able to significantly reduce the size of your APK for everyone who uses it. However, if you want to make some additional reductions specifically for Android Go users, then you can use Android’s multi-APK feature to publish an APK that’s optimized for the Android Go platform. 

To create a dedicated Android Go app, you’ll need to create an APK that has the same package name and signing certificate as your “regular” app, but has a unique version code. 

When creating your Android Go APK, you’ll also need to add the following to your Manifest: 

Conclusion 

In this article, I covered all the major ways that you can reduce the size of your APK to create a sleeker, more slimline application that will download faster and take up less space on the user’s device. 


Comments 0
There are currently no comments.