Skip to content

Commit

Permalink
[java-runtime] Simplify and fix split configs detection (#9378)
Browse files Browse the repository at this point in the history
We use [`ApplicationInfo.splitSourceDirs`][0] to detect whether the
application uses [split config `.apk` files][1].  Those files are
created by Android when an `.aab` is used to deploy application, e.g.
via the Google Play Store or manually:

	% dotnet new android
	% dotnet build -c Release
	% java -jar /usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/34.0.94/tools/bundletool.jar \
	  build-apks --bundle bin/Release/net8.0-android/*-Signed.aab \
	  --output=app.apks --connected-device \
	  --aapt2 /usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/34.0.94/tools/Darwin/aapt2
	# app.apks contains the "split" apk files.
	
	# can manually install app.apks by using `bundletool.jar install-apks`:
	% java -jar /usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/34.0.94/tools/bundletool.jar \
	  install-apks --apks=app.apks

When they are present, the "base" `.apk` file in
`splits/base-master.apk` doesn't contain any `lib/` directories and
we can simply skip scanning it for those entries, thus saving on
startup time.  (The `splits/base-<ABI>.apk` file contains the native
libraries of interest to us.)

We used to check whether there are more than one entry in
`splitSourceDirs`, which used to be the case, but it seems that
recent Android versions (at least API-33 and newer) can have just a
single entry in the array.  Because of that, we were scanning all the
`.apk` files on those Android versions, wasting time at startup.

Fix the check by probing whether the array exists and contains at
least a single entry.  Additionally, remove API-21 checks, since this
is our lowest supported API level.

[0]: https://developer.android.com/reference/android/content/pm/ApplicationInfo#splitSourceDirs
[1]: https://developer.android.com/guide/app-bundle
  • Loading branch information
grendello authored Oct 25, 2024
1 parent e98ae9c commit 46f9145
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ public void attachInfo (android.content.Context context, android.content.pm.Prov
// Mono Runtime Initialization {{{
android.content.pm.ApplicationInfo applicationInfo = context.getApplicationInfo ();
String[] apks = null;
if (android.os.Build.VERSION.SDK_INT >= 21) {
String[] splitApks = applicationInfo.splitPublicSourceDirs;
if (splitApks != null && splitApks.length > 0) {
apks = new String[splitApks.length + 1];
apks [0] = applicationInfo.sourceDir;
System.arraycopy (splitApks, 0, apks, 1, splitApks.length);
}
}
if (apks == null) {
String[] splitApks = applicationInfo.splitSourceDirs;
if (splitApks != null && splitApks.length > 0) {
apks = new String[splitApks.length + 1];
apks [0] = applicationInfo.sourceDir;
System.arraycopy (splitApks, 0, apks, 1, splitApks.length);
} else {
apks = new String[] { applicationInfo.sourceDir };
}
mono.MonoPackageManager.LoadApplication (context, applicationInfo, apks);
Expand Down Expand Up @@ -67,4 +64,3 @@ public int update (android.net.Uri uri, android.content.ContentValues values, St
throw new RuntimeException ("This operation is not supported.");
}
}

8 changes: 1 addition & 7 deletions src/java-runtime/java/mono/android/MonoPackageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,7 @@ public static void LoadApplication (Context context, ApplicationInfo runtimePack
// Should the order change here, src/monodroid/jni/SharedConstants.hh must be updated accordingly
//
String[] appDirs = new String[] {filesDir, cacheDir, dataDir};
boolean haveSplitApks = false;

if (android.os.Build.VERSION.SDK_INT >= 21) {
if (runtimePackage.splitSourceDirs != null) {
haveSplitApks = runtimePackage.splitSourceDirs.length > 1;
}
}
boolean haveSplitApks = runtimePackage.splitSourceDirs != null && runtimePackage.splitSourceDirs.length > 0;

//
// Preload DSOs libmonodroid.so depends on so that the dynamic
Expand Down

0 comments on commit 46f9145

Please sign in to comment.