Skip to content

Commit

Permalink
[java-runtime] simplify mono.MonoPackageManager.LoadApplication() (#…
Browse files Browse the repository at this point in the history
…9655)

We currently have code that loads `MonoRuntimeProvider.Bundled.java`
and parses Java code comments between:

	// Mono Runtime Initialization {{{
	android.content.pm.ApplicationInfo applicationInfo = context.getApplicationInfo ();
	String[] 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);
	// }}}

These lines are used for any `Instrumentation` type, such as:

https://github.com/dotnet/java-interop/blob/2c06b3c2a11833aea0e9b51aac2a72195bd64539/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.CallableWrapperMembers/CallableWrapperType.cs#L238-L241

To refactor and cleanup, we can:

  * Make `mono.MonoPackageManager.LoadApplication()` only take in a
    single `Context context` parameter.

  * Move the Java code that looks at `applicationInfo.splitSourceDirs`
    inside `mono.MonoPackageManager.LoadApplication()`.

  * Reduce the code in the `// Mono Runtime Initialization {{{` block
    to a single line.

  * Now we no longer need to load the
    `MonoRuntimeProvider.Bundled.java` file during a build, we can
    simply declare the one line of Java code as a C# string.

This will make refactoring `<GenerateJavaStubs/>` easier in future PRs.
  • Loading branch information
jonathanpeppers authored Jan 7, 2025
1 parent e0bf801 commit 6af0a5d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,7 @@ public boolean onCreate ()
@Override
public void attachInfo (android.content.Context context, android.content.pm.ProviderInfo info)
{
// Mono Runtime Initialization {{{
android.content.pm.ApplicationInfo applicationInfo = context.getApplicationInfo ();
String[] 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);
// }}}
mono.MonoPackageManager.LoadApplication (context);
super.attachInfo (context, info);
}

Expand Down
40 changes: 1 addition & 39 deletions src/Xamarin.Android.Build.Tasks/Utilities/JCWGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ bool ProcessTypes (bool generateCode, string androidSdkPlatform, MarshalMethodsC
throw new ArgumentException ("must not be null or empty", nameof (outputPath));
}

string monoInit = GetMonoInitSource (androidSdkPlatform);
string monoInit = "mono.MonoPackageManager.LoadApplication (context);";
bool hasExportReference = context.ResolvedAssemblies.Any (assembly => Path.GetFileName (assembly.ItemSpec) == "Mono.Android.Export.dll");
bool ok = true;

Expand Down Expand Up @@ -185,44 +185,6 @@ CallableWrapperType CreateGenerator (TypeDefinition type, MarshalMethodsClassifi
return CecilImporter.CreateType (type, context.TypeDefinitionCache, reader_options);
}

static string GetMonoInitSource (string androidSdkPlatform)
{
if (String.IsNullOrEmpty (androidSdkPlatform)) {
throw new ArgumentException ("must not be null or empty", nameof (androidSdkPlatform));
}

// Lookup the mono init section from MonoRuntimeProvider:
// Mono Runtime Initialization {{{
// }}}
var builder = new StringBuilder ();
var runtime = "Bundled";
var api = "";
if (int.TryParse (androidSdkPlatform, out int apiLevel) && apiLevel < 21) {
api = ".20";
}

var assembly = Assembly.GetExecutingAssembly ();
using var s = assembly.GetManifestResourceStream ($"MonoRuntimeProvider.{runtime}{api}.java");
using var reader = new StreamReader (s);
bool copy = false;
string? line;
while ((line = reader.ReadLine ()) != null) {
if (string.CompareOrdinal ("\t\t// Mono Runtime Initialization {{{", line) == 0) {
copy = true;
}

if (copy) {
builder.AppendLine (line);
}

if (string.CompareOrdinal ("\t\t// }}}", line) == 0) {
break;
}
}

return builder.ToString ();
}

public static void EnsureAllArchitecturesAreIdentical (TaskLoggingHelper logger, ConcurrentDictionary<AndroidTargetArch, NativeCodeGenState> javaStubStates)
{
if (javaStubStates.Count <= 1) {
Expand Down
13 changes: 12 additions & 1 deletion src/java-runtime/java/mono/android/MonoPackageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ public class MonoPackageManager {

static android.content.Context Context;

public static void LoadApplication (Context context, ApplicationInfo runtimePackage, String[] apks)
public static void LoadApplication (Context context)
{
synchronized (lock) {
android.content.pm.ApplicationInfo runtimePackage = context.getApplicationInfo ();
String[] apks = null;
String[] splitApks = runtimePackage.splitSourceDirs;
if (splitApks != null && splitApks.length > 0) {
apks = new String[splitApks.length + 1];
apks [0] = runtimePackage.sourceDir;
System.arraycopy (splitApks, 0, apks, 1, splitApks.length);
} else {
apks = new String[] { runtimePackage.sourceDir };
}

if (context instanceof android.app.Application) {
Context = context;
}
Expand Down

0 comments on commit 6af0a5d

Please sign in to comment.