Skip to content

Commit aac3e9a

Browse files
authored
[Java.Interop, Java.Base] Fix xamarin-android integration issues (#929)
Context: dotnet/android#6549 Context: 7f1a5ab...bc5bcf4 When attempting to bump xamarin-android from 7f1a5ab to bc5bcf4, the build failed for a number of reasons, including because bc5bcf4 added a "duplicate" type `Java.Interop.JavaTypeParametersAttribute`, which caused unit tests to fail: error CS0433: The type 'JavaTypeParametersAttribute' exists in both 'Java.Interop, Version=0.1.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' and 'Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065'. The plan was to use type forwarders for this type from `Mono.Android.dll` to `Java.Interop.dll`, but in retrospect this sounds like a potential "forward-incompatible ABI break" (e56a8c8), as assemblies built against the newer `Mono.Android.dll` (with type forwarders) won't be usable on previous Xamarin.Android SDKs. Make `JavaTypeParametersAttribute` conditional on `NET`, so that it's only included in .NET 6 builds. .NET SDK for Android will then use type forwarders, which won't be an issue as there's no previous .NET release to be forward compatible with. Update `tests/generator-Tests` so that `JavaTypeParametersAttribute` is once again provided for non-`NET` builds. Additionally, commit 111ebca enabled `$(TreatWarningsAsErrors)`=True, which had an inadvertent side effect of preventing VS2019 from building the Java.Interop repo, as warning CS8032 is now an error: error CS8032: An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\6.0.0\analyzers\dotnet\cs\System.Text.Json.SourceGeneration.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. As it happens, the xamarin-android Windows PR builds are on VS2019, and thus promptly broke. Fix this error by adding CS8032 to `$(NoWarn)`, and updating various `.csproj` files so that the `$(NoWarn)` in `Directory.Build.props` is used everywhere needed. Finally, update `Java.Base.targets` so that it looks for `class-parse.dll` and `generator.dll` from `$(UtilityOutputFullPath)` *not* `$(ToolOutputFullPath)`. `class-parse.csproj` and `generator.csproj` both set `$(OutputPath)` to `$(UtilityOutputFullPath)`, and the correct path must be used in order for `Java.Base.targets` to find the required utilities.
1 parent bc5bcf4 commit aac3e9a

File tree

8 files changed

+44
-5
lines changed

8 files changed

+44
-5
lines changed

Directory.Build.props

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@
9191
<_RunJNIEnvGen Condition=" '$(JIBuildingForNetCoreApp)' != 'True' ">$(Runtime) "$(_JNIEnvGenPath)"</_RunJNIEnvGen>
9292
</PropertyGroup>
9393

94+
<!--
95+
When building on a bot w/ VS2019:
96+
97+
warning CS8032: An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from
98+
99+
however, with `$(TreatWarningsAsErrors)`=True, this becomes an error,
100+
meaning we can't build on VS2019.
101+
102+
Ignore CS8032 so that we can build on VS2019.
103+
-->
104+
<PropertyGroup>
105+
<NoWarn>$(NoWarn);CS8032</NoWarn>
106+
</PropertyGroup>
107+
94108
<!-- The net6.0 versions of these are stricter and require overloads not available in .NET Framework, so start with just .NET Framework -->
95109
<PropertyGroup Condition=" '$(JIBuildingForNetCoreApp)' != 'True' ">
96110
<AnalysisMode>AllEnabledByDefault</AnalysisMode>

src/Java.Base/Java.Base.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net6.0</TargetFrameworks>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<Nullable>enable</Nullable>
7-
<NoWarn>8764</NoWarn>
7+
<NoWarn>$(NoWarn);8764</NoWarn>
88
</PropertyGroup>
99

1010
<ItemGroup>

src/Java.Base/Java.Base.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<GeneratorPath>$(ToolOutputFullPath)generator.dll</GeneratorPath>
4+
<GeneratorPath>$(UtilityOutputFullPath)generator.dll</GeneratorPath>
55
</PropertyGroup>
66

77
<Target Name="_GetJavaBaseJmodPath">
@@ -17,7 +17,7 @@
1717
Outputs="$(IntermediateOutputPath)\mcw\api.xml">
1818
<MakeDir Directories="$(IntermediateOutputPath)mcw" />
1919
<PropertyGroup>
20-
<_ClassParse>"$(ToolOutputFullPath)class-parse.dll"</_ClassParse>
20+
<_ClassParse>"$(UtilityOutputFullPath)class-parse.dll"</_ClassParse>
2121
<_Input>"$(_JavaBaseJmod)"</_Input>
2222
<_Output>"-o=$(IntermediateOutputPath)/mcw/api.xml"</_Output>
2323
</PropertyGroup>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<RootNamespace>Java.Interop</RootNamespace>
1212
<AssemblyName>Java.Interop</AssemblyName>
1313
<TargetFrameworkVersion Condition=" '$(TargetFrameworkVersion)' == '' ">v4.5</TargetFrameworkVersion>
14-
<NoWarn>1591</NoWarn>
14+
<NoWarn>$(NoWarn);1591</NoWarn>
1515
<SignAssembly>true</SignAssembly>
1616
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
1717
<LangVersion>8.0</LangVersion>

src/Java.Interop/Java.Interop.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
1818
</PropertyGroup>
1919
<PropertyGroup>
20-
<NoWarn>1591</NoWarn>
20+
<NoWarn>$(NoWarn);1591</NoWarn>
2121
<SignAssembly>true</SignAssembly>
2222
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
2323
<DefineConstants>INTEROP;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES;$(JavaInteropDefineConstants)</DefineConstants>

src/Java.Interop/Java.Interop/JavaTypeParametersAttribute.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
#if NET
4+
35
namespace Java.Interop
46
{
57
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method,
@@ -14,3 +16,5 @@ public JavaTypeParametersAttribute (string [] typeParameters)
1416
public string [] TypeParameters { get; }
1517
}
1618
}
19+
20+
#endif // NET

tests/generator-Tests/Integration-Tests/Compiler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public static Assembly Compile (Xamarin.Android.Binder.CodeGeneratorOptions opti
4040
if (options.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1) {
4141
preprocessorSymbols.Add ("JAVA_INTEROP1");
4242
}
43+
#if NET
44+
preprocessorSymbols.Add ("NET");
45+
#endif // NET
4346

4447
var parseOptions = new CSharpParseOptions (preprocessorSymbols:preprocessorSymbols);
4548

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#if !NET
2+
3+
using System;
4+
5+
namespace Java.Interop
6+
{
7+
public class JavaTypeParametersAttribute : Attribute
8+
{
9+
public JavaTypeParametersAttribute (string [] typeParameters)
10+
{
11+
TypeParameters = typeParameters;
12+
}
13+
14+
public string [] TypeParameters { get; set; }
15+
}
16+
}
17+
18+
#endif // !NET

0 commit comments

Comments
 (0)