Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nativeaot] support for Application subclasses #9716

Merged
merged 16 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions samples/NativeAOT/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Android.Runtime;
using Android.Util;
using System.Reflection;
using System.Runtime.InteropServices;

Expand All @@ -10,6 +11,8 @@ public class MainActivity : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
Log.Debug ("NativeAOT", "Application.OnCreate()");

base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource
Expand Down
22 changes: 22 additions & 0 deletions samples/NativeAOT/MainApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Android.Runtime;
using Android.Util;

/// <summary>
/// NOTE: This class is not required, but used for testing Android.App.Application subclasses.
/// </summary>
[Register ("my/MainApplication")] // Required for typemap in NativeAotTypeManager
[Application]
public class MainApplication : Application
{
public MainApplication (IntPtr handle, JniHandleOwnership transfer)
: base (handle, transfer)
{
}

public override void OnCreate ()
{
Log.Debug ("NativeAOT", "Application.OnCreate()");

base.OnCreate ();
}
}
2 changes: 2 additions & 0 deletions samples/NativeAOT/NativeAotRuntimeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public boolean onCreate() {
public void attachInfo(android.content.Context context, android.content.pm.ProviderInfo info) {
Log.d(TAG, "NativeAotRuntimeProvider.attachInfo(): calling JavaInteropRuntime.init()…");
JavaInteropRuntime.init();
// NOTE: only required for custom applications
net.dot.jni.ApplicationRegistration.registerApplications();
super.attachInfo (context, info);
}

Expand Down
1 change: 1 addition & 0 deletions samples/NativeAOT/NativeAotTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ partial class NativeAotTypeManager : JniRuntime.JniTypeManager {
["android/os/Bundle"] = typeof (Android.OS.Bundle),
["android/view/ContextThemeWrapper"] = typeof (Android.Views.ContextThemeWrapper),
["my/MainActivity"] = typeof (MainActivity),
["my/MainApplication"] = typeof (MainApplication),
};

public NativeAotTypeManager ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mono.android.app;
package JAVA_PACKAGE_NAME;

public class ApplicationRegistration {

Expand Down
36 changes: 21 additions & 15 deletions src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,19 @@ Dictionary<string, ITaskItem> MaybeGetArchAssemblies (Dictionary<AndroidTargetAr

void GenerateAdditionalProviderSources (NativeCodeGenState codeGenState, IList<string> additionalProviders)
{
if (androidRuntime != Xamarin.Android.Tasks.AndroidRuntime.MonoVM) {
bool isMonoVm = androidRuntime == Xamarin.Android.Tasks.AndroidRuntime.MonoVM;
if (!isMonoVm) {
Log.LogDebugMessage ($"Skipping MonoRuntimeProvider generation for: {androidRuntime}");
return;
}

// Create additional runtime provider java sources.
string providerTemplateFile = "MonoRuntimeProvider.Bundled.java";
string providerTemplate = GetResource (providerTemplateFile);

foreach (var provider in additionalProviders) {
var contents = providerTemplate.Replace ("MonoRuntimeProvider", provider);
var real_provider = Path.Combine (OutputDirectory, "src", "mono", provider + ".java");
Files.CopyIfStringChanged (contents, real_provider);
} else {
// Create additional runtime provider java sources.
string providerTemplateFile = "MonoRuntimeProvider.Bundled.java";
string providerTemplate = GetResource (providerTemplateFile);

foreach (var provider in additionalProviders) {
var contents = providerTemplate.Replace ("MonoRuntimeProvider", provider);
var real_provider = Path.Combine (OutputDirectory, "src", "mono", provider + ".java");
Files.CopyIfStringChanged (contents, real_provider);
}
}

// Create additional application java sources.
Expand All @@ -326,21 +326,27 @@ void GenerateAdditionalProviderSources (NativeCodeGenState codeGenState, IList<s

string javaKey = JavaNativeTypeManager.ToJniName (type, codeGenState.TypeCache).Replace ('/', '.');
regCallsWriter.WriteLine (
"\t\tmono.android.Runtime.register (\"{0}\", {1}.class, {1}.__md_methods);",
isMonoVm ?
"\t\tmono.android.Runtime.register (\"{0}\", {1}.class, {1}.__md_methods);" :
"\t\tnet.dot.jni.ManagedPeer.registerNativeMembers ({1}.class, {1}.__md_methods);",
type.GetAssemblyQualifiedName (codeGenState.TypeCache),
javaKey
);
}
}
regCallsWriter.Close ();

var real_app_dir = Path.Combine (OutputDirectory, "src", "mono", "android", "app");
var real_app_dir = isMonoVm ?
Path.Combine (OutputDirectory, "src", "mono", "android", "app") :
Path.Combine (OutputDirectory, "src", "net", "dot", "jni");
string applicationTemplateFile = "ApplicationRegistration.java";
SaveResource (
applicationTemplateFile,
applicationTemplateFile,
real_app_dir,
template => template.Replace ("// REGISTER_APPLICATION_AND_INSTRUMENTATION_CLASSES_HERE", regCallsWriter.ToString ())
template => template
.Replace ("JAVA_PACKAGE_NAME", isMonoVm ? "mono.android.app" : "net.dot.jni")
.Replace ("// REGISTER_APPLICATION_AND_INSTRUMENTATION_CLASSES_HERE", regCallsWriter.ToString ())
);
}

Expand Down
Loading