Skip to content

Commit 6af0a5d

Browse files
[java-runtime] simplify mono.MonoPackageManager.LoadApplication() (#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.
1 parent e0bf801 commit 6af0a5d

File tree

3 files changed

+14
-53
lines changed

3 files changed

+14
-53
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,7 @@ public boolean onCreate ()
1818
@Override
1919
public void attachInfo (android.content.Context context, android.content.pm.ProviderInfo info)
2020
{
21-
// Mono Runtime Initialization {{{
22-
android.content.pm.ApplicationInfo applicationInfo = context.getApplicationInfo ();
23-
String[] 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 {
30-
apks = new String[] { applicationInfo.sourceDir };
31-
}
32-
mono.MonoPackageManager.LoadApplication (context, applicationInfo, apks);
33-
// }}}
21+
mono.MonoPackageManager.LoadApplication (context);
3422
super.attachInfo (context, info);
3523
}
3624

src/Xamarin.Android.Build.Tasks/Utilities/JCWGenerator.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ bool ProcessTypes (bool generateCode, string androidSdkPlatform, MarshalMethodsC
9999
throw new ArgumentException ("must not be null or empty", nameof (outputPath));
100100
}
101101

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

@@ -185,44 +185,6 @@ CallableWrapperType CreateGenerator (TypeDefinition type, MarshalMethodsClassifi
185185
return CecilImporter.CreateType (type, context.TypeDefinitionCache, reader_options);
186186
}
187187

188-
static string GetMonoInitSource (string androidSdkPlatform)
189-
{
190-
if (String.IsNullOrEmpty (androidSdkPlatform)) {
191-
throw new ArgumentException ("must not be null or empty", nameof (androidSdkPlatform));
192-
}
193-
194-
// Lookup the mono init section from MonoRuntimeProvider:
195-
// Mono Runtime Initialization {{{
196-
// }}}
197-
var builder = new StringBuilder ();
198-
var runtime = "Bundled";
199-
var api = "";
200-
if (int.TryParse (androidSdkPlatform, out int apiLevel) && apiLevel < 21) {
201-
api = ".20";
202-
}
203-
204-
var assembly = Assembly.GetExecutingAssembly ();
205-
using var s = assembly.GetManifestResourceStream ($"MonoRuntimeProvider.{runtime}{api}.java");
206-
using var reader = new StreamReader (s);
207-
bool copy = false;
208-
string? line;
209-
while ((line = reader.ReadLine ()) != null) {
210-
if (string.CompareOrdinal ("\t\t// Mono Runtime Initialization {{{", line) == 0) {
211-
copy = true;
212-
}
213-
214-
if (copy) {
215-
builder.AppendLine (line);
216-
}
217-
218-
if (string.CompareOrdinal ("\t\t// }}}", line) == 0) {
219-
break;
220-
}
221-
}
222-
223-
return builder.ToString ();
224-
}
225-
226188
public static void EnsureAllArchitecturesAreIdentical (TaskLoggingHelper logger, ConcurrentDictionary<AndroidTargetArch, NativeCodeGenState> javaStubStates)
227189
{
228190
if (javaStubStates.Count <= 1) {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,20 @@ public class MonoPackageManager {
2626

2727
static android.content.Context Context;
2828

29-
public static void LoadApplication (Context context, ApplicationInfo runtimePackage, String[] apks)
29+
public static void LoadApplication (Context context)
3030
{
3131
synchronized (lock) {
32+
android.content.pm.ApplicationInfo runtimePackage = context.getApplicationInfo ();
33+
String[] apks = null;
34+
String[] splitApks = runtimePackage.splitSourceDirs;
35+
if (splitApks != null && splitApks.length > 0) {
36+
apks = new String[splitApks.length + 1];
37+
apks [0] = runtimePackage.sourceDir;
38+
System.arraycopy (splitApks, 0, apks, 1, splitApks.length);
39+
} else {
40+
apks = new String[] { runtimePackage.sourceDir };
41+
}
42+
3243
if (context instanceof android.app.Application) {
3344
Context = context;
3445
}

0 commit comments

Comments
 (0)