Search code examples
androidgoogle-play-consoleandroid-app-bundleandroid-app-signingandroid-multiple-apk

How to know app bundle (.aab) config after at run time?


  bundle {
       density {
           enableSplit true
       }
       language {
           enableSplit true
       }
       abi {
           enableSplit true
       }
   }

We are going to upload .aab app bundle on Google Play Store. And after app installation, our backend needs to know which specific ABI version is installed for the App.

Is there a way to know which ABI, Density, or Language version is installed at run time?


Solution

  • You can query the names of the split APKs installed on a device via the PackageManager.

    See https://developer.android.com/reference/kotlin/android/content/pm/PackageInfo?hl=en#splitnames

    /** Returns the set of installed APKs or an empty set if a single APK is installed. */
    private Set<String> getInstalledSplits() {
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return Collections.emptySet();
      }
    
      PackageInfo packageInfo;
      try {
        PackageManager pm = context.getPackageManager();
        packageInfo = pm.getPackageInfo(context.getPackageName(), GET_META_DATA);
      } catch (NameNotFoundException e) {
        throw new RuntimeException(e);
      }
        
      String[] installedSplits = packageInfo.splitNames;
      if (installedSplits == null) {
        return Collections.emptySet();
      }
    
      return new HashSet<>(Arrays.asList(installedSplits));
    }
    

    You could also use the name of the APKs if that's more convenient by reading ApplicationInfo#splitSourceDirs

    WARNING: Keep in mind that all the split* attributes only have a value when splits are installed; they otherwise return null when a single APK is installed. So if you target pre-L devices, you may want to handle the case of the standalone APK separately, in which case, you can't use the name of installed splits to determine which ABI has been served, you have to actually inspect the APK.