4

It looks like right now that Android Instant Apps are supported in Android 5.0 or later. However, App Links (which I understood that Instant Apps depend on) are only supported in 6.0 or later. I've searched online but couldn't find clear answer on this.

In general it looks like we'd want, to support Instant apps, to use app links to navigate between activities in different feature modules, but also need in most cases to use those modules to build installable apk that works on versions below 5.0
Does this mean that code needs to check the API level and use different approaches depending on version (e.g calling startActivity with explicit intent if < 5.0)?

This is the info I've found in the Instant Apps documentation:

Both your instant and installable versions of your app must implement the Android App Links feature introduced in Android 6.0. App Links provide the primary mechanism for connecting URLs to discrete activities within your app.

and

an instant app cannot launch an activity in another feature directly; instead, it must request the URL address that corresponds to the other other feature's entry-point activity.

and then from https://developer.android.com/topic/instant-apps/index.html

Android Instant Apps supports the latest Android devices from Android 5.0 (API level 21) through Android O

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
  • At first glance, it looks like your post actually lacks a question itself. Ok, I've founded it after rereading - stay tuned for an asnwer :) – Volo Oct 14 '17 at 21:42
  • Yeah, it's a bit rambling I admit :) and is sort of 2 related questions to add extra confusion (will try and rephrase)! – John O'Reilly Oct 14 '17 at 21:50

1 Answers1

4

Android App Links just provide a way for Android system to uniquely associate your http deep-links with your application (without showing the disambiguation dialog for the user to select which app to open the link). It doesn't give you any new API to start an activity. Thus you will need to call startActivity in any case. You just need to use an implicit intent if you want to open an activity belonging to another Instant App feature module.

For navigation inside of the same feature module (or in case your Instant App consists of just one base feature) an explicit intent could be freely used.

It looks like right now that Android Instant Apps are supported in Android 5.0 or later. However, App Links (which I understood that Instant Apps depend on) are only supported in 6.0 or later

Yes, that's true. But the Instant App supervisor (which is installed internally by the Google Play Services and used to run Instant Apps on Android before 8.0) will ensure that app links registered to your verified Instant App domain will be forwarded directly to your Instant App.

Does this mean that code needs to check the API level and use different approaches depending on version (e.g calling startActivity if < 5.0)

Yes, if you want to be 100% sure your user won't be shown a disambiguation (aka "chooser") dialog like this, while browsing between the activities of your app (and most probably you would like to prevent such a weird user experience). If you use dependency injection, you can have an interface used for navigation in your app, and then different implementations for the installable and the instant app.

interface Navigation {
   void startActivityFromModuleA();
   void startActivityFromModuleB();
   …
}

class InstallableAppNavigation implements Navigation {
   public void startActivityFromModuleA() {
       // explicit intent
       Intent intent = new Intent(context, ActivityFromModuleA.class);
       context.startActivity(intent);
   }
   …
}

class InstantAppNavigation implements Navigation {
   public void startActivityFromModuleA() {
       // implicit intent
       Intent intent = new Intent(Intent.ACTION_VIEW,  
               Uri.parse("https://your.app.com/moduleA/smth"));
       context.startActivity(intent);
   }
   …
}
Volo
  • 28,673
  • 12
  • 97
  • 125
  • 1
    one thing about the use of explicit intents....it will require a degree of coupling that wouldn't otherwise be needed (if navigation code is in base module for example). Do you have recommendation on way to avoid this? – John O'Reilly Oct 15 '17 at 07:57
  • 1
    @JohnO'Reilly Sure, the simplest thing you can do to avoid coupling is to use `ComponentName` instead of class reference. Another option is to use an implicit intent but with action starting with your package name (e.g. `com.yourapp.ACTION_A`) - this would minimize the risk of accidental chooser dialog popups. If you wish to have a more proper OOP solution, I'd suggest to check out the [Dagger multibindings](https://google.github.io/dagger/multibindings). (An idea is to delegate navigation to some interface instances, which are added into multibinding map by the components of feature modules.) – Volo Oct 16 '17 at 10:56
  • Would you put the implementations in the instant and apk modules respectively? – dazza5000 Nov 01 '17 at 00:01
  • Thank you for this. This is the approach I took and I keep the navigation implementations in the base module. :) – dazza5000 Feb 16 '18 at 18:20
  • Would you be able to point me to more information on that Google Play Services Instant App Supervisor that you mentioned in your answer? I am trying to learn more and look more in depth on what happens right after a user clicks on a link, how does Android know that's an instant app and how does it launch it. If you'd like to give a look at this I'd appreciate it: https://stackoverflow.com/questions/51760641/how-are-android-instant-apps-started-loaded – Emilian Cebuc Aug 10 '18 at 09:29