Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
How to use R8 to Reduce APK Size in Android?
Next article icon

How to use R8 to Reduce APK Size in Android?

Last Updated : 22 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

If you are building any apps and you want to target this app to a large audience then there are so many factors which you should consider while building your apps. The most important factor while building any application is its size. The size of the app matters a lot. If your app size is very high then users don't like to download such big apps that require such a huge storage space and data charges. So it is very important to maintain your app size for your users while building any Android application. In this article, we will take a look at reducing app size using R8. 

Below are some of the topics which we will look at in this article

  • What is R8?
  • How to use R8 shrinking in your app?
  • How the shrinking in R8 works?

The configuration required for using the R8 app shrinking

  • For enabling R8 in your application you have to use the Android Studio version like 3.4 or higher.
  • The Gradle version inside your project should have a version of around 3.4.0

What is R8? 

What is R8

R8 is an app shrinking tool that is used to reduce the size of your application. This tool present in Android Studio works with the rules of Proguard. R8 will convert your app's code into optimized Dalvik code. During this process of conversion R8 will remove the classes and methods which are unused inside our application. Along with this android applications are having so many unused files that we don't require inside our application so R8 will remove that files and helps in the reduction of app size. The benefit of using R8 is that it will make your app more secure and difficult to reverse engineer. So this will helps us to increase the security of our application. 

Different techniques used by R8 to reduce APK size:

  • Optimization of your code: In this process, R8 will actually optimize the code to reduce app size. So in the process of optimization, it will remove the unused classes, methods, and dead code present inside our application.
  • Identifier Renaming: In this process, R8 will actually rename our class names and variable names. While writing code we use some identifier pattern so that we can understand our code properly. But in this process, R8 will rename all the classes into smaller variable names that will also help to reduce APK size. for example, you have generated a new class named "Constant" for storing some constants which are being used in different parts of our application. R8 will rename that class in any smaller name let's say "a" or in any other format.
  • Shrinking: While writing code we create so many methods inside our application. While creating this method some of the methods are not being used inside our application, those methods are called unreachable methods which are not being used anywhere inside our application. So R8 will remove such unreachable methods to reduce the size of APK.
  • Optimization of 3rd party library: For the implementation of many external features inside our application developers generally prefer to use external libraries inside their application. But while using these libraries this it will install so many files that we don't require for our app. So R8 will optimize the code for that library and keep only that code that is required in our application and will remove the files which are unused.

Now we will move towards the practical implementation of R8 inside our app. 

How to use R8 shrinking in the app? 

Implementation of R8 shrinking is a single-step process. First of all, create a New Project with an empty activity inside Android Studio. For creating a new project in Android Studio. Take a look over this link on How to create a new project in Android Studio. After creating this new project. Navigate to the Gradle scripts > build.gradle(:app) and you will get to see the section of buildTypes. Inside this change minifyEnabled from false to true to enable R8. Below is the section where we have to make a change.

buildTypes {

       release {

           // make change here

           minifyEnabled true

           proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

           }

   }

After changing it true sync your project and R8 is being added inside your application You can now test the APK size by building the APK. You can also test the APK size by adding some external libraries. Test the APK size using R8 and without using R8 you will get to see the difference between the size of APK. 

How shrinking in R8 will work? 

The algorithms which are used by R8 for the reduction of app size will trace for the unused methods and unreachable code in your application and remove those methods. So R8 will start tracing your code from an entry point which we have to declare for any class and from that entry point R8 will starts tracing the code for the unused code. For example, we will take a look at a class which is created below: 

Java
/*package whatever //do not write package name here */  import java.io.*;  // entry point for R8  class GFG {   // Method one is used    // inside our main method.    private void methodone(){             System.out.println("Method 1 ");   }   // Method two is not being   // used in our main method.    private void methodtwo(){             System.out.println("Method 2 ");   }              // Our main method      public static void main (String[] args) {       methodone();     } } 

Inside the proguard we will add the below rule to it as: 

-keep class GFG {

   public static void main (String[] args) {

       }

}

In the above example, the proguard will start scanning your application from the main method and inside this method, the methodone() will be called first and after printing method one the tracing will be stopped. Then R8 will look for the unused method inside our application the unused method is methodtwo() which we are not using inside our main method. So R8 will remove that methodtwo() inside our application. and the methodone() will be renamed with some smaller names and align the code properly. After aligning the code will look like the below example. 

Java
/*package whatever //do not write package name here */  import java.io.*;  class GFG {     public static void main (String[] args) {             System.out.println("Method 1 ");     } } 

So inside our application in Android Studio, there are so many files that are to be traced by R8. So for tracing these files and deciding the entry point for each file aapt2 tool is used. Add the below line below to that of the line where you have enabled your proguard.

minifyEnabled true

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')

But while using this there are some problems with gson files When your code contains some reflection files So it will remove that code but we have to keep that code inside our project. For implementing this add the below line under the minifyEnabled true. 

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-gson.pro', 'proguard-rules.pro'

So to overcome the issues while Gson in Android adds the line which is shown above in your build.gradle file. So in this way we have implemented the R8 app shrinking inside our application and this will helps to reduce the size of our app. 


Next Article
How to use R8 to Reduce APK Size in Android?

C

chaitanyamunje
Improve
Article Tags :
  • Technical Scripter
  • Android
  • Technical Scripter 2020
  • Android-Misc

Similar Reads

    How to Use Proguard to Reduce APK Size in Android?
    App size matters a lot when building any application. If the app size is larger most of the users will not download your app due to its huge size. The large app size will cost your user huge data and he will also require disk space to install your application. So to reduce the size of APK proguard i
    6 min read
    How to Reduce APK Size in Android?
    APK size is one of the most important factors while building any app for any organization or any business. No user would like to install a very large APK and consume his data on downloading that APK. The APK size will make an impact on your app performance about How fast it loads, How much memory it
    7 min read
    How to Resize a Bitmap in Android?
    ImageViews are used within the android application to display different types of images. Many times images are displayed within the ImageView using bitmap instead of drawable files. In this article, we will take a look at How to Resize Bitmap in the android application to resize our image to be disp
    3 min read
    How to Reduce and Compress Image Size in Android Studio?
    Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrains’ IntelliJ IDEA software. Android Studio provides many excellent features that enhance productivity when building Android apps. Here, we are going to reduce and compress an
    2 min read
    How to Generate Unsigned (Shareable) Apk in Android Studio?
    Unsigned Apk, as the name suggests it means it is not signed by any Keystore. A Keystore is basically a binary file that contains a set of private keys. Every App that we install using the Google Play App Store needs to be signed with a Keystore. The signed apk is simply the unsigned apk that has be
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences