A Quick Look at Feature-on-Feature Dependencies in Android Gradle Plugin 4.0.0
At first glance, Feature-on-Feature dependencies appears to be a change meant for niche dynamic features use cases. However, in this post we’ll explore how this change will make it much easier to build and ship instant apps.
In Android Gradle Plugin 4.0.0, dynamic features can now depend on other dynamic features. Now, when your app downloads a dynamic feature, it also downloads any dynamic features that it depends on.
New features in Android Studio Preview | Android Developers
Android Studio 4.0 is currently in the Canary and Dev channels, and Android Studio 3.6 is currently in the Beta…
Your Gradle dependency graph can now look something like this:
At first you may think that this only serves some niche use cases of dynamic features, however this actually helps solves a very real problem that dynamic feature projects currently face in Android Gradle Plugin 3.5.
Suppose you have a project with 3 dynamic features;
:video-player depend on a common library;
:video-data. You may try to set up your Gradle dependencies like this:
If you now try to build this, you’ll get an error that says that 2 dynamic features both package the same library.
[:video-list, :video-player] all package the same library [:video-data].
Multiple APKs packaging the same library can cause runtime errors.
Adding the above library as a dependency of the base module will resolve this
issue by packaging the library with the base APK instead.
In Android Gradle Plugin 3.5, common dependencies have to be provided by base. So instead, your Gradle dependencies will look like this:
This has produced an interesting side effect,
:news now implicitly depends on
:video-data . This becomes a huge problem if you want to make
:news an instant app. Instant apps have a 4MB download size limit, and if you depend on libraries that you don’t actually need, it is going to make it very difficult to get the download size below 4MB. For example,
:video-data may depend on the Cast library from Google Play Services, which is a big dependency if your project doesn’t already use Google Play Services.
Feature-on-Feature dependencies in Android Gradle Plugin 4.0 solves this issue, and you have a couple of different options.
You can make
:video-list depend on
:video-player like so:
Now, you no longer have 2 completely separate dynamic features that depend on
:video-data, and AGP is happy.
You can also make
:video-data itself a dynamic feature, and have
:video-player depend on
Either of these solutions will avoid the common dependency build error, and ensure that
:news doesn’t depend on
:video-data, so that
:news can live happily as a 4MB instant app!