You can also use an alternative, mobile-friendly library to add specific functionality to your app. Note: code shrinking can clean up some of a library's unnecessary code, but it might not be able to remove some large internal dependencies. When it was introduced in Android 11, this API decoded only the first image from animations in these formats.
Use ImageDecoder instead of third-party libraries to further decrease APK size and benefit from future updates related to security and performance.
Android supports a very large set of devices, encompassing a variety of screen densities. In Android 4. Although Android supports all these densities, you don't need to export your rasterized assets to each density. If you know that only a small percentage of your users have devices with specific densities, consider whether you need to bundle those densities into your app. If you don't include resources for a specific screen density, Android automatically scales existing resources originally designed for other screen densities.
We recommend that every app include at least an xxhdpi image variant. For more information screen densities, see Screen Sizes and Densities. Some images don't require a static image resource; the framework can dynamically draw the image at runtime instead. In addition, XML Drawable objects produce monochromatic images compliant with material design guidelines.
You can include a separate resource for variations of an image, such as tinted, shaded, or rotated versions of the same image. We recommend, however, that you reuse the same set of resources, customizing them as needed at runtime.
Android provides several utilities to change the color of an asset, either using the android:tint and tintMode attributes on Android 5. For lower versions of the platform, use the ColorFilter class. You can also omit resources that are only a rotated equivalent of another resource.
The following code snippet provides an example of turning a "thumb up" into a "thumb down" by pivoting at the middle of the image and rotating it degrees:. You can also reduce your APK size by procedurally rendering your images. Procedural rendering frees up space because you no longer store an image file in your APK. For example, the aapt tool can convert a true-color PNG that does not require more than colors to an 8-bit PNG with a color palette.
Doing so results in an image of equal quality but a smaller memory footprint. Keep in mind that the aapt has the following limitations:. You can reduce PNG file sizes without losing image quality using tools like pngcrush , pngquant , or zopflipng. All of these tools can reduce PNG file size while preserving the perceptive image quality.
The pngcrush tool is particularly effective: This tool iterates over PNG filters and zlib Deflate parameters, using each combination of filters and parameters to compress the image. It then chooses the configuration that yields the smallest compressed output. You can use vector graphics to create resolution-independent icons and other scalable media.
Using these graphics can greatly reduce your APK footprint. Vector images are represented in Android as VectorDrawable objects. With a VectorDrawable object, a byte file can generate a sharp image the size of the screen. However, it takes a significant amount of time for the system to render each VectorDrawable object, and larger images take even longer to appear on the screen.
Therefore, consider using these vector graphics only when displaying small images. For more information on working with VectorDrawable objects, see Working with Drawables. Do not use AnimationDrawable to create frame-by-frame animations because doing so requires that you include a separate bitmap file for each frame of the animation, which drastically increase the size of your APK.
Instead, you should use AnimatedVectorDrawableCompat to create animated vector drawables. Reduce native and Java code There are several methods you can use to reduce the size of the Java and native codebase in your app. Make sure to understand the footprint of any code which is automatically generated.
For example, many protocol buffer tools generate an excessive number of methods and classes, which can double or triple the size of your app. A single enum can add about 1. These additions can quickly accumulate for complex systems or shared libraries. If possible, consider using the IntDef annotation and code shrinking to strip enumerations out and convert them to integers.
This type conversion preserves all of the type safety benefits of enums. If your app uses native code and the Android NDK, you can also reduce the size of the release version of your app by optimizing your code.
Two useful techniques are removing debug symbols and not extracting native libraries. Using debug symbols makes sense if your application is in development and still requires debugging. Use the arm-eabi-strip tool, provided in the Android NDK, to remove unnecessary debug symbols from native libraries. After that, you can compile your release build. When building the release version of your app, package uncompressed. Disabling this flag prevents PackageManager from copying.
When building your app using Android Gradle plugin 3. Your APK might contain content that users download but never use, like additional language or per-screen-density resources.
To ensure a minimal download for your users, you should upload your app to Google Play using Android App Bundles.
If you're not publishing your app to Google Play, you can segment your app into several APKs, differentiated by factors such as screen size or GPU texture support. When a user downloads your app, their device receives the correct APK based on the device's features and settings. This way, devices don't receive assets for features that the devices don't have.
Content and code samples on this page are subject to the licenses described in the Content License. App Basics.
Build your first app. App resources. Resource types. App manifest file. Device compatibility. Multiple APK support. Tablets, large screens, and foldables. Build responsive UIs. Build for foldables.
Getting started. Handling data. User input. Watch Face Studio. Health services. Creating watch faces. Android TV.
Build TV Apps. Build TV playback apps. Help users find content on TV. Recommend TV content. Watch Next. Build TV games. Build TV input services. TV Accessibility. Android for Cars. Build media apps for cars. Build navigation, parking, and charging apps for cars. Android Things.
Supported hardware. Advanced setup. Build apps. Create a Things app. Ensure that the Image Compression techniques are understood and implemented to reduce the size of the apk before releasing the app to the play store. As we have seen in the image shared for the Android Analyser plugin above, one of the recommendations was to change the PNG file to a WebP file format. Every time we build a new project, we see the following piece of code in the app-level build.
To learn more about Proguard, you can refer our MindOrks article on the same. If we are not using App Bundles, we can go with the traditional way. To learn more about it, you can refer to our MindOrks article on the same. Reduce resources where ever possible.
Using shrinkResources attribute in the Gradle will remove all the resources which are not being used anywhere in the project. Enable this in your app-level build. Remove the localized resources which are not needed by using resConfigs.
The following snippet shows how to limit your language resources to just English and French:. As discussed, having unused language resources only swells the apk size. Hence it is important to remove unused files and resources. Remove any debug library you have in the app. It can be done by using debugImplementation while building testing debug apk. R8 shrinking is a process in which we reduce the amount of code of our application and by doing so, the APK size automatically gets reduced.
R8 does most of the work as Proguard. Why do we need to prefer it? Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Introducing Content Health, a new way to keep the knowledge base up-to-date. Podcast what if you could invest in your favorite developer? Featured on Meta. Now live: A fully responsive profile. Reducing the weight of our footer. Related 5.
Hot Network Questions. Question feed. Accept all cookies Customize settings.
0コメント