Skip to content

Commit a77aa33

Browse files
authored
[Java.Interop] Conditionally call ManagedPeer.Init() (#761)
Context: dotnet/android#5400 Context: dotnet/android#5444 Don't call `ManagedPeer.Init()` from the `JniRuntime` constructor when `XA_JI_EXCLUDE` is `#define`d. The presence of `ManagedPeer.Init()` requires that *all* Xamarin.Android apps contain `System.Linq.Expressions` types after linking, because `ManagedPeer.Construct()` calls `JniEnvironment.Runtime.MarshalMemberBuilder.CreateConstructActivationPeerFunc()` which pulls in all the `System.Linq.Expressions` infrastructure. Currently, *nothing* requires `ManagedPeer` *at all*; Java.Interop- "style" "Java Activation" (8c83f64) is not used outside of unit tests. Consequently, there is no need to pay the cost of System.Linq.Expressions within all Xamarin.Android apps. Wrap the `ManagedPeer.Init()` invocation with `XA_JI_EXCLUDE` so that the Xamarin.Android build of `Java.Interop.dll` (893562c) *excludes* the `ManagedPeer.Init()` invocation. This allows every Xamarin.Android app to link out System.Linq.Expressions, unless the app is otherwise using that namespace. `XA_JI_EXCLUDE` is intended to be set by `$(JavaInteropDefineConstants)` MSBUild property within the optional file `bin/Build$(Configuration)/XAConfig.props`, which the xamarin-android build will create before building `Java.Interop.csproj`. Cleanup the `src/Java.Interop` source code to remove use of the `XA_INTEGRATION` symbol. This symbol is no longer of use, and now only adds noise.
1 parent da12df4 commit a77aa33

11 files changed

+16
-40
lines changed

src/Java.Interop/Java.Interop-MonoAndroid.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
1919
</PropertyGroup>
2020
<Import Project="..\..\Directory.Build.props" />
21+
<PropertyGroup>
22+
<XAConfigPath>..\..\bin\Build$(Configuration)\XAConfig.props</XAConfigPath>
23+
</PropertyGroup>
24+
<Import Condition="Exists ('$(XAConfigPath)')" Project="$(XAConfigPath)" />
2125
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2226
<DebugSymbols>true</DebugSymbols>
2327
<DebugType>full</DebugType>
2428
<Optimize>false</Optimize>
2529
<OutputPath>..\..\bin\Debug</OutputPath>
26-
<DefineConstants>DEBUG;INTEROP;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
30+
<DefineConstants>DEBUG;INTEROP;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES;$(JavaInteropDefineConstants)</DefineConstants>
2731
<ErrorReport>prompt</ErrorReport>
2832
<WarningLevel>4</WarningLevel>
2933
<ConsolePause>false</ConsolePause>
@@ -39,7 +43,7 @@
3943
<ErrorReport>prompt</ErrorReport>
4044
<WarningLevel>4</WarningLevel>
4145
<ConsolePause>false</ConsolePause>
42-
<DefineConstants>INTEROP;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
46+
<DefineConstants>INTEROP;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES;$(JavaInteropDefineConstants)</DefineConstants>
4347
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4448
<Nullable>enable</Nullable>
4549
<DocumentationFile>..\..\bin\Release\Java.Interop.xml</DocumentationFile>

src/Java.Interop/Java.Interop.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<XAConfigPath>..\..\bin\Build$(Configuration)\XAConfig.props</XAConfigPath>
5+
</PropertyGroup>
6+
<Import Condition="Exists ('$(XAConfigPath)')" Project="$(XAConfigPath)" />
37
<PropertyGroup>
48
<TargetFrameworks>netstandard2.0;netcoreapp3.1</TargetFrameworks>
59
<NoWarn>1591</NoWarn>
610
<SignAssembly>true</SignAssembly>
711
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
8-
<DefineConstants>INTEROP;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
12+
<DefineConstants>INTEROP;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES;$(JavaInteropDefineConstants)</DefineConstants>
913
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1014
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
1115
<IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)\$(TargetFramework.ToLowerInvariant())\</IntermediateOutputPath>

src/Java.Interop/Java.Interop/JniEnvironment.Errors.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public static void ThrowNew (JniObjectReference klass, string message)
3030
throw new InvalidOperationException (string.Format ("Could not raise an exception; JNIEnv::ThrowNew() returned {0}.", r));
3131
}
3232

33-
#if !XA_INTEGRATION
3433
public static void Throw (Exception e)
3534
{
3635
if (e == null)
@@ -41,7 +40,6 @@ public static void Throw (Exception e)
4140
}
4241
Throw (je.PeerReference);
4342
}
44-
#endif // !XA_INTEGRATION
4543
}
4644
}
4745
}

src/Java.Interop/Java.Interop/JniEnvironment.References.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ public static void PushLocalFrame (int capacity)
5959
throw new InvalidOperationException (string.Format ("Could not push a frame; JNIEnv::PushLocalFrame() returned {0}.", r));
6060
}
6161

62-
#if !XA_INTEGRATION
6362
public static int GetIdentityHashCode (JniObjectReference value)
6463
{
6564
return JniSystem.IdentityHashCode (value);
6665
}
67-
#endif // !XA_INTEGRATION
6866

6967
public static IntPtr NewReturnToJniRef (IJavaPeerable value)
7068
{

src/Java.Interop/Java.Interop/JniEnvironment.Strings.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public static unsafe JniObjectReference NewString (string? value)
1717
return NewString (s, value.Length);
1818
}
1919

20-
#if !XA_INTEGRATION
2120
public static string? ToString (IntPtr reference)
2221
{
2322
return ToString (new JniObjectReference (reference));
@@ -28,7 +27,6 @@ public static unsafe JniObjectReference NewString (string? value)
2827
Debug.Assert (targetType == typeof (string), "Expected targetType==typeof(string); was: " + targetType);
2928
return ToString (ref reference, transfer);
3029
}
31-
#endif // !XA_INTEGRATION
3230

3331
public static unsafe string? ToString (JniObjectReference value)
3432
{

src/Java.Interop/Java.Interop/JniObjectReferenceOptions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ partial struct JniObjectReference {
1919
const JniObjectReferenceOptions DisposeSource = (JniObjectReferenceOptions) (1 << 1);
2020
}
2121

22-
#if !XA_INTEGRATION
2322
partial class JniRuntime {
2423
partial class JniValueManager {
2524
const JniObjectReferenceOptions DoNotRegisterTarget = (JniObjectReferenceOptions) (1 << 2);
2625
}
2726
}
28-
#endif // !XA_INTEGRATION
2927
}
30-

src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ public unsafe JniObjectReference StartCreateInstance (string constructorSignatur
111111
return r;
112112
}
113113

114-
#if !XA_INTEGRATION
115114
internal JniObjectReference AllocObject (Type declaringType)
116115
{
117116
var r = GetConstructorsForType (declaringType)
@@ -120,7 +119,6 @@ internal JniObjectReference AllocObject (Type declaringType)
120119
r.Flags = JniObjectReferenceFlags.Alloc;
121120
return r;
122121
}
123-
#endif // !XA_INTEGRATION
124122

125123
internal unsafe JniObjectReference NewObject (string constructorSignature, Type declaringType, JniArgumentValue* parameters)
126124
{

src/Java.Interop/Java.Interop/JniPeerMembers.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ public JniPeerMembers (string jniPeerTypeName, Type managedPeerType)
2424
if (!typeof (IJavaPeerable).IsAssignableFrom (managedPeerType))
2525
throw new ArgumentException ("'managedPeerType' must implement the IJavaPeerable interface.", nameof (managedPeerType));
2626

27-
#if !XA_INTEGRATION
2827
Debug.Assert (
2928
JniEnvironment.Runtime.TypeManager.GetTypeSignature (managedPeerType).SimpleReference == jniPeerTypeName,
3029
string.Format ("ManagedPeerType <=> JniTypeName Mismatch! javaVM.GetJniTypeInfoForType(typeof({0})).JniTypeName=\"{1}\" != \"{2}\"",
3130
managedPeerType.FullName,
3231
JniEnvironment.Runtime.TypeManager.GetTypeSignature (managedPeerType).SimpleReference,
3332
jniPeerTypeName));
34-
#endif // !XA_INTEGRATION
3533

3634
ManagedPeerType = managedPeerType;
3735
}
@@ -47,14 +45,12 @@ public JniPeerMembers (string jniPeerTypeName, Type managedPeerType)
4745
if (!typeof (IJavaPeerable).IsAssignableFrom (managedPeerType))
4846
throw new ArgumentException ("'managedPeerType' must implement the IJavaPeerable interface.", nameof (managedPeerType));
4947

50-
#if !XA_INTEGRATION
5148
Debug.Assert (
5249
JniEnvironment.Runtime.TypeManager.GetTypeSignature (managedPeerType).SimpleReference == jniPeerTypeName,
5350
string.Format ("ManagedPeerType <=> JniTypeName Mismatch! javaVM.GetJniTypeInfoForType(typeof({0})).JniTypeName=\"{1}\" != \"{2}\"",
5451
managedPeerType.FullName,
5552
JniEnvironment.Runtime.TypeManager.GetTypeSignature (managedPeerType).SimpleReference,
5653
jniPeerTypeName));
57-
#endif // !XA_INTEGRATION
5854
}
5955

6056
JniPeerTypeName = jniPeerTypeName;

src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ IEnumerable<Type> CreateGetTypesEnumerator (JniTypeSignature typeSignature)
201201
yield return type;
202202
continue;
203203
}
204-
#if !XA_INTEGRATION
204+
205205
if (typeSignature.ArrayRank > 0) {
206206
var rank = typeSignature.ArrayRank;
207207
var arrayType = type;
@@ -214,7 +214,7 @@ IEnumerable<Type> CreateGetTypesEnumerator (JniTypeSignature typeSignature)
214214
}
215215
yield return arrayType;
216216
}
217-
#endif // !XA_INTEGRATION
217+
218218
if (typeSignature.ArrayRank > 0) {
219219
var rank = typeSignature.ArrayRank;
220220
var arrayType = type;

src/Java.Interop/Java.Interop/JniRuntime.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ protected JniRuntime (CreationOptions options)
248248
}
249249
}
250250

251-
#if !XA_INTEGRATION
251+
#if !XA_JI_EXCLUDE
252252
ManagedPeer.Init ();
253-
#endif // !XA_INTEGRATION
253+
#endif // !XA_JI_EXCLUDE
254254
}
255255

256256
T SetRuntime<T> (T value)
@@ -325,11 +325,9 @@ protected virtual void Dispose (bool disposing)
325325
JniObjectReference.Dispose (ref ClassLoader);
326326

327327
ClearTrackedReferences ();
328-
#if !XA_INTEGRATION
329328
ValueManager.Dispose ();
330329
marshalMemberBuilder?.Dispose ();
331330
TypeManager.Dispose ();
332-
#endif // !XA_INTEGRATION
333331
ObjectReferenceManager.Dispose ();
334332

335333
var environments = JniEnvironment.Info.Values;
@@ -389,11 +387,7 @@ public void DestroyRuntime ()
389387

390388
public virtual Exception? GetExceptionForThrowable (ref JniObjectReference reference, JniObjectReferenceOptions options)
391389
{
392-
#if XA_INTEGRATION
393-
throw new NotSupportedException ("Do not know h ow to convert a JniObjectReference to a System.Exception!");
394-
#else // !XA_INTEGRATION
395390
return ValueManager.GetValue<Exception> (ref reference, options);
396-
#endif // !̣XA_INTEGRATION
397391
}
398392

399393
public int GlobalReferenceCount {
@@ -436,13 +430,7 @@ partial class JniRuntime {
436430

437431
public virtual void RaisePendingException (Exception pendingException)
438432
{
439-
#if XA_INTEGRATION
440-
if (pendingException == null)
441-
throw new ArgumentNullException (nameof (pendingException));
442-
throw new NotSupportedException ("Do not know how to marshal System.Exception instances.");
443-
#else // !XA_INTEGRATION
444433
JniEnvironment.Exceptions.Throw (pendingException);
445-
#endif // !XA_INTEGRATION
446434
}
447435
}
448436
}

0 commit comments

Comments
 (0)