Home > Enterprise >  How to implement only a single component from a dependency in Android Studio?
How to implement only a single component from a dependency in Android Studio?

Time:09-26

I have the following dependency:

implementation 'com.google.android.material:material:1.5.0-alpha02' 

from this repository. However it turns out I only need the CircularProgressIndicator from it, now I don't want the entire dependency shipped with my app (I need only what I need from it). How can I accomplish this? I am thinking along the lines of:

implementation 'com.google.android.material:material:1.5.0-alpha02:AddOnlyCircularProgressIndicatorRelatedComponents'

CodePudding user response:

How can I accomplish this?

Your options are:

  • Fork the library, copying only the pieces that make up CircularProgressIndicator. Note that there may be dozens or hundreds of such pieces: Java classes, resources, etc.

  • Include the library, and rely on R8/ProGuard to remove unused code from the library, as it does for everything else in your app

CodePudding user response:

If your concern is app size, you need not worry, implement the full dependency and allow ProGuard do the magic.

Under the hood, Proguard removes all unused code from your compiled project thereby, and reduces your app size significantly. To learn how to integrate Proguard into your application, check this out.

PS: Other benefits of using Proguard include:

  • Code obfuscation: making your app extremely difficult to reverse engineer.
  • Reducing method count: there is a 64k limit for the number of methods an Android app can have and this number is inclusive of functions from your libraries and dependencies. Using Proguard greatly reduces your chances of hitting this number.
  • Related