Skip to content

Commit 46f9145

Browse files
authored
[java-runtime] Simplify and fix split configs detection (#9378)
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
1 parent e98ae9c commit 46f9145

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

src/Xamarin.Android.Build.Tasks/Resources/MonoRuntimeProvider.Bundled.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ public void attachInfo (android.content.Context context, android.content.pm.Prov
2121
// Mono Runtime Initialization {{{
2222
android.content.pm.ApplicationInfo applicationInfo = context.getApplicationInfo ();
2323
String[] apks = null;
24-
if (android.os.Build.VERSION.SDK_INT >= 21) {
25-
String[] splitApks = applicationInfo.splitPublicSourceDirs;
26-
if (splitApks != null && splitApks.length > 0) {
27-
apks = new String[splitApks.length + 1];
28-
apks [0] = applicationInfo.sourceDir;
29-
System.arraycopy (splitApks, 0, apks, 1, splitApks.length);
30-
}
31-
}
32-
if (apks == null) {
24+
String[] splitApks = applicationInfo.splitSourceDirs;
25+
if (splitApks != null && splitApks.length > 0) {
26+
apks = new String[splitApks.length + 1];
27+
apks [0] = applicationInfo.sourceDir;
28+
System.arraycopy (splitApks, 0, apks, 1, splitApks.length);
29+
} else {
3330
apks = new String[] { applicationInfo.sourceDir };
3431
}
3532
mono.MonoPackageManager.LoadApplication (context, applicationInfo, apks);
@@ -67,4 +64,3 @@ public int update (android.net.Uri uri, android.content.ContentValues values, St
6764
throw new RuntimeException ("This operation is not supported.");
6865
}
6966
}
70-

src/java-runtime/java/mono/android/MonoPackageManager.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,7 @@ public static void LoadApplication (Context context, ApplicationInfo runtimePack
5858
// Should the order change here, src/monodroid/jni/SharedConstants.hh must be updated accordingly
5959
//
6060
String[] appDirs = new String[] {filesDir, cacheDir, dataDir};
61-
boolean haveSplitApks = false;
62-
63-
if (android.os.Build.VERSION.SDK_INT >= 21) {
64-
if (runtimePackage.splitSourceDirs != null) {
65-
haveSplitApks = runtimePackage.splitSourceDirs.length > 1;
66-
}
67-
}
61+
boolean haveSplitApks = runtimePackage.splitSourceDirs != null && runtimePackage.splitSourceDirs.length > 0;
6862

6963
//
7064
// Preload DSOs libmonodroid.so depends on so that the dynamic

0 commit comments

Comments
 (0)