Skip to content

Commit

Permalink
[build] Remove MONOANDROID, NET, and NETCOREAPP directives (#8434)
Browse files Browse the repository at this point in the history
Removes the conditional compiler directives used to swap between `.NET`
and `MonoAndroid`.  These are no longer needed in main as `.NET` is the
only supported target.
  • Loading branch information
pjcollins committed Oct 20, 2023
1 parent e8cbf7e commit 2ff47e4
Show file tree
Hide file tree
Showing 32 changed files with 22 additions and 323 deletions.
13 changes: 0 additions & 13 deletions build-tools/xaprepare/xaprepare/Application/NullableAttributes.cs

This file was deleted.

2 changes: 0 additions & 2 deletions src/Mono.Android/Android.App/Activity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ partial class Activity {
return this.FindViewById (id)!.JavaCast<T> ();
}

#if NET7_0_OR_GREATER || (NET6_0_OR_GREATER && ANDROID_33) || ANDROID_34
// See: https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java;l=3430
public T RequireViewById<T> (int id)
where T : Android.Views.View
Expand All @@ -23,7 +22,6 @@ public T RequireViewById<T> (int id)
}
return view;
}
#endif // NET7_0_OR_GREATER || (NET6_0_OR_GREATER && ANDROID_33) || ANDROID_34

public void StartActivityForResult (Type activityType, int requestCode)
{
Expand Down
4 changes: 0 additions & 4 deletions src/Mono.Android/Android.Runtime/AndroidEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,7 @@ static IWebProxy GetDefaultProxy ()
// This is invoked by
// System.Net.Http.dll!System.Net.Http.HttpClient.cctor
// DO NOT REMOVE
#if !MONOANDROID1_0
[DynamicDependency (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof (Xamarin.Android.Net.AndroidMessageHandler))]
#endif
static object GetHttpMessageHandler ()
{
if (httpMessageHandlerType is null) {
Expand All @@ -357,14 +355,12 @@ static object GetHttpMessageHandler ()

static bool IsAcceptableHttpMessageHandlerType (Type handlerType)
{
#if !MONOANDROID1_0
if (Extends (handlerType, "System.Net.Http.HttpClientHandler, System.Net.Http")) {
// It's not possible to construct HttpClientHandler in this method because it would cause infinite recursion
// as HttpClientHandler's constructor calls the GetHttpMessageHandler function
Logger.Log (LogLevel.Warn, "MonoAndroid", $"The type {handlerType.AssemblyQualifiedName} cannot be used as the native HTTP handler because it is derived from System.Net.Htt.HttpClientHandler. Use a type that extends System.Net.Http.HttpMessageHandler instead.");
return false;
}
#endif
if (!Extends (handlerType, "System.Net.Http.HttpMessageHandler, System.Net.Http")) {
Logger.Log (LogLevel.Warn, "MonoAndroid", $"The type {handlerType.AssemblyQualifiedName} set as the default HTTP handler is invalid. Use a type that extends System.Net.Http.HttpMessageHandler.");
return false;
Expand Down
13 changes: 2 additions & 11 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ internal AndroidRuntime (IntPtr jnienv,
classLoader_loadClass,
jniAddNativeMethodRegistrationAttributePresent))
{
#if NETCOREAPP
// This is not ideal, but we need to set this while the runtime is initializing but we can't do it directly from the `JNIEnvInit.Initialize` method, since
// it lives in an assembly that does not reference Mono.Android. So we do it here, because this class is instantiated by JNIEnvInit.Initialize.
AndroidEnvironmentInternal.UnhandledExceptionHandler = AndroidEnvironment.UnhandledException;
#endif
}

public override void FailFast (string? message)
Expand Down Expand Up @@ -268,11 +266,7 @@ protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpl
{
string? j = JNIEnv.TypemapManagedToJava (type);
if (j != null) {
return
#if NET
GetReplacementTypeCore (j) ??
#endif // NET
j;
return GetReplacementTypeCore (j) ?? j;
}
if (JNIEnvInit.IsRunningOnDesktop) {
return JavaNativeTypeManager.ToJniName (type);
Expand All @@ -283,9 +277,8 @@ protected override IEnumerable<Type> GetTypesForSimpleReference (string jniSimpl
protected override IEnumerable<string> GetSimpleReferences (Type type)
{
string? j = JNIEnv.TypemapManagedToJava (type);
#if NET
j = GetReplacementTypeCore (j) ?? j;
#endif // NET

if (JNIEnvInit.IsRunningOnDesktop) {
string? d = JavaNativeTypeManager.ToJniName (type);
if (j != null && d != null) {
Expand All @@ -301,7 +294,6 @@ protected override IEnumerable<string> GetSimpleReferences (Type type)
return Array.Empty<string> ();
}

#if NET
protected override IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference)
{
ReadOnlySpan<char> name = jniSimpleReference;
Expand Down Expand Up @@ -383,7 +375,6 @@ protected override IEnumerable<string> GetSimpleReferences (Type type)
TargetJniMethodInstanceToStatic = method.is_static,
};
}
#endif // NET

delegate Delegate GetCallbackHandler ();

Expand Down
8 changes: 2 additions & 6 deletions src/Mono.Android/Android.Runtime/AndroidRuntimeInternal.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#if INSIDE_MONO_ANDROID_RUNTIME
using System;
using System.Reflection;

Expand All @@ -7,11 +7,7 @@ namespace Android.Runtime
public static class AndroidRuntimeInternal
{
internal static MethodInfo? mono_unhandled_exception_method = null;
#if NETCOREAPP
internal static Action<Exception> mono_unhandled_exception = RuntimeNativeMethods.monodroid_debugger_unhandled_exception;
#else
internal static Action<Exception>? mono_unhandled_exception = null;
#endif

#pragma warning disable CS0649 // Field is never assigned to. This field is assigned from monodroid-glue.cc.
internal static volatile bool BridgeProcessing; // = false
Expand All @@ -38,4 +34,4 @@ public static void WaitForBridgeProcessing ()
}
}
}
#endif // !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#endif // INSIDE_MONO_ANDROID_RUNTIME
25 changes: 0 additions & 25 deletions src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,9 @@ static void ManualJavaObjectDispose (Java.Lang.Object obj)
GC.SuppressFinalize (obj);
}

#if !NETCOREAPP
static Action<AppDomain, UnhandledExceptionEventArgs> AppDomain_DoUnhandledException = null!;
#endif // ndef NETCOREAPP

static void Initialize ()
{
AndroidRuntimeInternal.InitializeUnhandledExceptionMethod ();
#if !NETCOREAPP
if (AppDomain_DoUnhandledException == null) {
var ad_due = typeof (AppDomain)
.GetMethod ("DoUnhandledException",
bindingAttr: BindingFlags.NonPublic | BindingFlags.Instance,
binder: null,
types: new []{typeof (UnhandledExceptionEventArgs)},
modifiers: null);
if (ad_due != null) {
AppDomain_DoUnhandledException = (Action<AppDomain, UnhandledExceptionEventArgs>) Delegate.CreateDelegate (
typeof (Action<AppDomain, UnhandledExceptionEventArgs>), ad_due);
}
}
#endif // ndef NETCOREAPP
}

internal static void PropagateUncaughtException (IntPtr env, IntPtr javaThreadPtr, IntPtr javaExceptionPtr)
Expand All @@ -150,14 +132,7 @@ internal static void PropagateUncaughtException (IntPtr env, IntPtr javaThreadPt
Logger.Log (LogLevel.Info, "MonoDroid", "UNHANDLED EXCEPTION:");
Logger.Log (LogLevel.Info, "MonoDroid", javaException.ToString ());

#if !NETCOREAPP
var args = new UnhandledExceptionEventArgs (innerException ?? javaException, isTerminating: true);
// Disabled until Linker error surfaced in https://github.com/xamarin/xamarin-android/pull/4302#issuecomment-596400025 is resolved
//AppDomain.CurrentDomain.DoUnhandledException (args);
AppDomain_DoUnhandledException?.Invoke (AppDomain.CurrentDomain, args);
#else // ndef NETCOREAPP
RuntimeNativeMethods.monodroid_unhandled_exception (innerException ?? javaException);
#endif // def NETCOREAPP
} catch (Exception e) {
Logger.Log (LogLevel.Error, "monodroid", "Exception thrown while raising AppDomain.UnhandledException event: " + e.ToString ());
}
Expand Down
14 changes: 0 additions & 14 deletions src/Mono.Android/Android.Runtime/JNIEnvInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ internal struct JnienvInitializeArgs {

static AndroidRuntime? androidRuntime;

#if NETCOREAPP
[UnmanagedCallersOnly]
#endif
static unsafe void RegisterJniNatives (IntPtr typeName_ptr, int typeName_len, IntPtr jniClass, IntPtr methods_ptr, int methods_len)
{
string typeName = new string ((char*) typeName_ptr, 0, typeName_len);
Expand All @@ -75,9 +73,7 @@ static unsafe void RegisterJniNatives (IntPtr typeName_ptr, int typeName_len, In
((AndroidTypeManager)androidRuntime!.TypeManager).RegisterNativeMembers (jniType, type, methods);
}

#if NETCOREAPP
[UnmanagedCallersOnly]
#endif
internal static unsafe void Initialize (JnienvInitializeArgs* args)
{
IntPtr total_timing_sequence = IntPtr.Zero;
Expand All @@ -88,19 +84,13 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args)
gref_gc_threshold = args->grefGcThreshold;

jniRemappingInUse = args->jniRemappingInUse;
#if NETCOREAPP
MarshalMethodsEnabled = args->marshalMethodsEnabled;
#endif
java_class_loader = args->grefLoader;

mid_Class_forName = new JniMethodInfo (args->Class_forName, isStatic: true);

LocalRefsAreIndirect = args->localRefsAreIndirect == 1;

#if MONOANDROID1_0
Mono.SystemDependencyProvider.Initialize ();
#endif

bool androidNewerThan10 = args->androidSdkVersion > 10;
BoundExceptionType = (BoundExceptionType)args->ioExceptionType;
androidRuntime = new AndroidRuntime (args->env, args->javaVm, androidNewerThan10, args->grefLoader, args->Loader_loadClass, args->jniAddNativeMethodRegistrationAttributePresent != 0);
Expand All @@ -121,16 +111,12 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args)
}
}

#if !MONOANDROID1_0
SetSynchronizationContext ();
#endif
}

#if !MONOANDROID1_0
// NOTE: prevents Android.App.Application static ctor from running
[MethodImpl (MethodImplOptions.NoInlining)]
static void SetSynchronizationContext () =>
SynchronizationContext.SetSynchronizationContext (Android.App.Application.SynchronizationContext);
#endif
}
}
4 changes: 0 additions & 4 deletions src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ public static partial class JNINativeWrapper
static bool _unhandled_exception (Exception e)
{
if (Debugger.IsAttached || !JNIEnvInit.PropagateExceptions) {
#if NETCOREAPP
AndroidRuntimeInternal.mono_unhandled_exception?.Invoke (e);
#else
JNIEnvInit.mono_unhandled_exception?.Invoke (e);
#endif
return false;
}
return true;
Expand Down
4 changes: 0 additions & 4 deletions src/Mono.Android/Android.Runtime/JNINativeWrapper.g.tt
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,7 @@ namespace Android.Runtime
static bool _unhandled_exception (Exception e)
{
if (Debugger.IsAttached || !JNIEnvInit.PropagateExceptions) {
#if NETCOREAPP
AndroidRuntimeInternal.mono_unhandled_exception?.Invoke (e);
#else
JNIEnvInit.mono_unhandled_exception?.Invoke (e);
#endif
return false;
}
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Mono.Android/Android.Runtime/LogCategories.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#if INSIDE_MONO_ANDROID_RUNTIME
using System;

namespace Android.Runtime
Expand All @@ -20,4 +20,4 @@ internal enum LogCategories {
Netlink = 1 << 9,
}
}
#endif // !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#endif // INSIDE_MONO_ANDROID_RUNTIME
4 changes: 2 additions & 2 deletions src/Mono.Android/Android.Runtime/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#if INSIDE_MONO_ANDROID_RUNTIME
namespace Android.Runtime
{
// Keep in sync with the LogLevel enum in
Expand All @@ -15,4 +15,4 @@ internal enum LogLevel {
Silent = 0x08
}
}
#endif // !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#endif // INSIDE_MONO_ANDROID_RUNTIME
2 changes: 0 additions & 2 deletions src/Mono.Android/Android.Runtime/PreserveAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@

namespace Android.Runtime {

#if NETCOREAPP
[Obsolete ("Please use [System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute]")]
#endif // NETCOREAPP
[AttributeUsage (
AttributeTargets.Class
| AttributeTargets.Struct
Expand Down
4 changes: 2 additions & 2 deletions src/Mono.Android/Android.Runtime/RuntimeConstants.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#if !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#if INSIDE_MONO_ANDROID_RUNTIME
namespace Android.Runtime
{
internal static class RuntimeConstants
{
public const string InternalDllName = "xa-internal-api";
}
}
#endif // !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#endif // INSIDE_MONO_ANDROID_RUNTIME
7 changes: 3 additions & 4 deletions src/Mono.Android/Android.Runtime/RuntimeNativeMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#if INSIDE_MONO_ANDROID_RUNTIME
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -76,13 +76,12 @@ internal static class RuntimeNativeMethods

[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int _monodroid_max_gref_get ();
#if NETCOREAPP

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void monodroid_unhandled_exception (Exception javaException);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe void monodroid_debugger_unhandled_exception (Exception e);
#endif
}
}
#endif // !NETCOREAPP || INSIDE_MONO_ANDROID_RUNTIME
#endif // INSIDE_MONO_ANDROID_RUNTIME
4 changes: 1 addition & 3 deletions src/Mono.Android/Android.Views/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public bool PerformAccessibilityAction (GlobalAction action, Bundle arguments)
return this.FindViewById (id).JavaCast<T> ();
}

#if NET7_0_OR_GREATER || (NET6_0_OR_GREATER && ANDROID_33) || ANDROID_34
// See: https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/view/View.java;l=25322
public T RequireViewById<T> (int id)
where T : Android.Views.View
Expand All @@ -39,7 +38,6 @@ public T RequireViewById<T> (int id)
}
return view;
}
#endif //NET7_0_OR_GREATER || (NET6_0_OR_GREATER && ANDROID_33) || ANDROID_34

public bool Post (Action action)
{
Expand Down Expand Up @@ -91,7 +89,7 @@ public bool FitsSystemWindows ()
}
#endif

#if NET && ANDROID_34
#if ANDROID_34
[global::System.Runtime.Versioning.ObsoletedOSPlatform ("android30.0", "These flags are deprecated. Use WindowInsetsController instead.")]
public SystemUiFlags SystemUiFlags {
get => (SystemUiFlags) SystemUiVisibility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Android.Views
{
partial class WindowManagerLayoutParams
{
#if NET && ANDROID_34
#if ANDROID_34
[global::System.Runtime.Versioning.ObsoletedOSPlatform ("android30.0", "These flags are deprecated. Use WindowInsetsController instead.")]
public SystemUiFlags SystemUiFlags {
get => (SystemUiFlags) SystemUiVisibility;
Expand Down
2 changes: 0 additions & 2 deletions src/Mono.Android/Android/LinkerSafeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Android
{
#if NETCOREAPP
[Obsolete ("For .NET 6+, please use: [assembly: global::System.Reflection.AssemblyMetadata(\"IsTrimmable\", \"True\")]")]
#endif // NETCOREAPP
[AttributeUsage (AttributeTargets.Assembly)]
public sealed class LinkerSafeAttribute : Attribute
{
Expand Down
18 changes: 0 additions & 18 deletions src/Mono.Android/Java.Interop/JavaTypeParametersAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
using System;

#if NET
using System.Runtime.CompilerServices;

// PublicApiAnalyzers doesn't like TypeForwards
#pragma warning disable RS0016 // Symbol is not part of the declared API
[assembly: TypeForwardedTo (typeof (Java.Interop.JavaTypeParametersAttribute))]
#pragma warning restore RS0016 // Symbol is not part of the declared API

#else // !NET

namespace Java.Interop
{
public class JavaTypeParametersAttribute : Attribute
{
public JavaTypeParametersAttribute (string [] typeParameters)
{
TypeParameters = typeParameters;
}

public string [] TypeParameters { get; set; }
}
}

#endif // !NET
Loading

0 comments on commit 2ff47e4

Please sign in to comment.