Skip to content

Commit 4afa28d

Browse files
[Xamarin.Android.Build.Tasks] introduce $(_AndroidRuntime) property
Context: #9572 (comment) Context: https://github.com/xamarin/xamarin-macios/blob/2009c571aa8a975ab0e0b1785e5484dbd64d6f9b/dotnet/targets/Xamarin.Shared.Sdk.targets#L1004-L1006 To align with xamarin/xamarin-macios, the following public MSBuild properties can be used to select a runtime for iOS, macOS, etc.: * `$(UseMonoRuntime)=true`: MonoVM * `$(UseMonoRuntime)=false`: CoreCLR * `$(PublishAot)=true`: NativeAOT * Defaults if blank, select MonoVM Introduce a private `$(_AndroidRuntime)` property we can use throughout our build to know which runtime is being targetted. This way, if a new property is designed in the future, we can keep using `$(_AndroidRuntime)` and simply update the defaults.
1 parent 7aed4e3 commit 4afa28d

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
See: https://github.com/dotnet/sdk/blob/955c0fc7b06e2fa34bacd076ed39f61e4fb61716/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets#L16
2525
-->
2626
<_GetChildProjectCopyToPublishDirectoryItems>false</_GetChildProjectCopyToPublishDirectoryItems>
27-
<!-- Define a $(_AndroidNativeAot) property, as the logic detecting NativeAOT may change in the future -->
28-
<_AndroidNativeAot Condition=" '$(PublishAot)' == 'true' ">true</_AndroidNativeAot>
29-
<_AndroidNativeAot Condition=" '$(_AndroidNativeAot)' == '' ">false</_AndroidNativeAot>
30-
<UseMonoRuntime Condition=" '$(_AndroidNativeAot)' == 'true' and '$(UseMonoRuntime)' == '' ">false</UseMonoRuntime>
27+
<UseMonoRuntime Condition=" '$(PublishAot)' == 'true' and '$(UseMonoRuntime)' == '' ">false</UseMonoRuntime>
3128
<UseMonoRuntime Condition=" '$(UseMonoRuntime)' == '' ">true</UseMonoRuntime>
29+
<_AndroidRuntime Condition=" '$(PublishAot)' == 'true' and '$(UseMonoRuntime)' != 'true' ">NativeAOT</_AndroidRuntime>
30+
<_AndroidRuntime Condition=" '$(PublishAot)' != 'true' and '$(UseMonoRuntime)' != 'true' ">CoreCLR</_AndroidRuntime>
31+
<_AndroidRuntime Condition=" '$(_AndroidRuntime)' == '' ">MonoVM</_AndroidRuntime>
3232
<!-- HACK: make dotnet restore include Microsoft.NETCore.App.Runtime.NativeAOT.linux-bionic-arm64 -->
33-
<_IsPublishing Condition=" '$(_IsPublishing)' == '' and '$(_AndroidNativeAot)' == 'true' ">true</_IsPublishing>
33+
<_IsPublishing Condition=" '$(_IsPublishing)' == '' and '$(_AndroidRuntime)' == 'NativeAOT' ">true</_IsPublishing>
3434

3535
<!-- Use $(AndroidMinimumSupportedApiLevel) for $(SupportedOSPlatformVersion) if unset -->
3636
<SupportedOSPlatformVersion Condition=" '$(SupportedOSPlatformVersion)' == '' ">$(AndroidMinimumSupportedApiLevel)</SupportedOSPlatformVersion>
@@ -88,7 +88,7 @@
8888
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' and '$(PublishTrimmed)' == 'true' ">SdkOnly</AndroidLinkMode>
8989
<AndroidLinkMode Condition=" '$(AndroidLinkMode)' == '' ">None</AndroidLinkMode>
9090
<!-- For compat with user code not marked trimmable, only trim opt-in by default. -->
91-
<TrimMode Condition=" '$(TrimMode)' == '' and ('$(AndroidLinkMode)' == 'Full' or '$(_AndroidNativeAot)' == 'true') ">full</TrimMode>
91+
<TrimMode Condition=" '$(TrimMode)' == '' and ('$(AndroidLinkMode)' == 'Full' or '$(_AndroidRuntime)' == 'NativeAOT') ">full</TrimMode>
9292
<TrimMode Condition="'$(TrimMode)' == ''">partial</TrimMode>
9393
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' and ('$(TrimMode)' == 'full' or '$(IsAotCompatible)' == 'true') ">false</SuppressTrimAnalysisWarnings>
9494
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>
@@ -97,7 +97,7 @@
9797
<RuntimeIdentifier Condition=" '$(RuntimeIdentifiers)' != '' And '$(RuntimeIdentifier)' != '' " />
9898
<GenerateApplicationManifest Condition=" '$(GenerateApplicationManifest)' == '' ">true</GenerateApplicationManifest>
9999
<!-- Default to Mono's AOT in Release mode -->
100-
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == '' and '$(Configuration)' == 'Release' and '$(_AndroidNativeAot)' != 'true' ">true</RunAOTCompilation>
100+
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == '' and '$(Configuration)' == 'Release' and '$(_AndroidRuntime)' == 'MonoVM' ">true</RunAOTCompilation>
101101
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == 'true' ">true</RunAOTCompilation>
102102
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' ">false</RunAOTCompilation>
103103
<_AndroidXA1029 Condition=" '$(AotAssemblies)' != '' ">true</_AndroidXA1029>

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
<Import Project="Microsoft.Android.Sdk.DefaultProperties.targets" />
2828
<Import Project="$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props"
2929
Condition="Exists('$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props')"/>
30-
<Import Project="Microsoft.Android.Sdk.NativeAOT.targets" Condition=" '$(_AndroidNativeAot)' == 'true' " />
30+
<Import Project="Microsoft.Android.Sdk.NativeAOT.targets" Condition=" '$(_AndroidRuntime)' == 'NativeAOT' " />
3131

3232
</Project>

src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public class GenerateJavaStubs : AndroidTask
9393
[Output]
9494
public ITaskItem[] GeneratedBinaryTypeMaps { get; set; }
9595

96-
public bool NativeAot { get; set; }
96+
[Required]
97+
public string AndroidRuntime { get; set; } = "";
9798

9899
internal const string AndroidSkipJavaStubGeneration = "AndroidSkipJavaStubGeneration";
99100

@@ -296,8 +297,8 @@ Dictionary<string, ITaskItem> MaybeGetArchAssemblies (Dictionary<AndroidTargetAr
296297

297298
void GenerateAdditionalProviderSources (NativeCodeGenState codeGenState, IList<string> additionalProviders)
298299
{
299-
if (NativeAot) {
300-
Log.LogDebugMessage ("Skipping MonoRuntimeProvider generation for NativeAot");
300+
if (!string.Equals (AndroidRuntime, "MonoVM", StringComparison.OrdinalIgnoreCase)) {
301+
Log.LogDebugMessage ($"Skipping MonoRuntimeProvider generation for {AndroidRuntime}");
301302
return;
302303
}
303304

@@ -354,7 +355,7 @@ IList<string> MergeManifest (NativeCodeGenState codeGenState, Dictionary<string,
354355
Debug = Debug,
355356
MultiDex = MultiDex,
356357
NeedsInternet = NeedsInternet,
357-
NativeAot = NativeAot,
358+
AndroidRuntime = AndroidRuntime,
358359
};
359360
// Only set manifest.VersionCode if there is no existing value in AndroidManifest.xml.
360361
if (manifest.HasVersionCode) {
@@ -388,7 +389,7 @@ IList<string> MergeManifest (NativeCodeGenState codeGenState, Dictionary<string,
388389
(List<TypeDefinition> allJavaTypes, List<TypeDefinition> javaTypesForJCW) = ScanForJavaTypes (resolver, tdCache, assemblies, userAssemblies, useMarshalMethods);
389390
var jcwContext = new JCWGeneratorContext (arch, resolver, assemblies.Values, javaTypesForJCW, tdCache, useMarshalMethods);
390391
var jcwGenerator = new JCWGenerator (Log, jcwContext) {
391-
NativeAot = NativeAot,
392+
CodeGenerationTarget = string.Equals (AndroidRuntime, "MonoVM", StringComparison.OrdinalIgnoreCase) ? JavaPeerStyle.XAJavaInterop1 : JavaPeerStyle.JavaInterop1
392393
};
393394
bool success;
394395

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class JCWGenerator
4444
readonly TaskLoggingHelper log;
4545
readonly JCWGeneratorContext context;
4646

47-
public bool NativeAot { get; set; }
47+
public JavaPeerStyle CodeGenerationTarget { get; set; } = JavaPeerStyle.XAJavaInterop1;
4848

4949
public MarshalMethodsClassifier? Classifier { get; private set; }
5050

@@ -127,7 +127,7 @@ bool GenerateCode (CallableWrapperType generator, TypeDefinition type, string ou
127127
bool ok = true;
128128
using var writer = MemoryStreamPool.Shared.CreateStreamWriter ();
129129
var writer_options = new CallableWrapperWriterOptions {
130-
CodeGenerationTarget = NativeAot ? JavaPeerStyle.JavaInterop1 : JavaPeerStyle.XAJavaInterop1
130+
CodeGenerationTarget = CodeGenerationTarget
131131
};
132132

133133
try {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ internal class ManifestDocument
9494
public bool ForceDebuggable { get; set; }
9595
public string VersionName { get; set; }
9696
public IVersionResolver VersionResolver { get; set; } = new MonoAndroidHelperVersionResolver ();
97-
public bool NativeAot { get; set; }
97+
public string AndroidRuntime { get; set; } = "";
9898

9999
string versionCode;
100100

@@ -673,8 +673,8 @@ XElement CreateApplicationElement (XElement manifest, string applicationClass, L
673673

674674
IList<string> AddMonoRuntimeProviders (XElement app)
675675
{
676-
if (NativeAot) {
677-
//TODO: implement NativeAOT provider logic
676+
if (!string.Equals (AndroidRuntime, "MonoVM", StringComparison.OrdinalIgnoreCase)) {
677+
//TODO: implement provider logic for non-Mono runtimes
678678
return [];
679679
}
680680

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

+6-6
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
329329
<AndroidUseAssemblyStore Condition=" '$(AndroidUseAssemblyStore)' == '' ">true</AndroidUseAssemblyStore>
330330
<AndroidAotEnableLazyLoad Condition=" '$(AndroidAotEnableLazyLoad)' == '' And '$(AotAssemblies)' == 'true' And '$(AndroidIncludeDebugSymbols)' != 'true' ">True</AndroidAotEnableLazyLoad>
331331
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' and ('$(UsingMicrosoftNETSdkRazor)' == 'true') ">False</AndroidEnableMarshalMethods>
332-
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' and '$(_AndroidNativeAot)' != 'true' ">True</AndroidEnableMarshalMethods>
332+
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' and '$(_AndroidRuntime)' != 'NativeAOT' ">True</AndroidEnableMarshalMethods>
333333
<!-- NOTE: temporarily disable for NativeAOT for now, to get build passing -->
334-
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' and '$(_AndroidNativeAot)' == 'true' ">False</AndroidEnableMarshalMethods>
334+
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' and '$(_AndroidRuntime)' == 'NativeAOT' ">False</AndroidEnableMarshalMethods>
335335
<_AndroidUseMarshalMethods Condition=" '$(AndroidIncludeDebugSymbols)' == 'True' ">False</_AndroidUseMarshalMethods>
336336
<_AndroidUseMarshalMethods Condition=" '$(AndroidIncludeDebugSymbols)' != 'True' ">$(AndroidEnableMarshalMethods)</_AndroidUseMarshalMethods>
337337
</PropertyGroup>
@@ -1379,7 +1379,7 @@ because xbuild doesn't support framework reference assemblies.
13791379
DependsOnTargets="_CollectRuntimeJarFilenames;$(_BeforeAddStaticResources);_GetMonoPlatformJarPath">
13801380
<CopyResource ResourceName="machine.config" OutputPath="$(MonoAndroidIntermediateAssemblyDir)machine.config" />
13811381
<CopyResource
1382-
Condition=" '$(_AndroidNativeAot)' != 'true' "
1382+
Condition=" '$(_AndroidRuntime)' == 'MonoVM' "
13831383
ResourceName="MonoRuntimeProvider.Bundled.java"
13841384
OutputPath="$(_AndroidIntermediateJavaSourceDirectory)mono\MonoRuntimeProvider.java"
13851385
/>
@@ -1498,7 +1498,7 @@ because xbuild doesn't support framework reference assemblies.
14981498
</ItemGroup>
14991499

15001500
<GenerateJavaStubs
1501-
NativeAot="$(_AndroidNativeAot)"
1501+
AndroidRuntime="$(_AndroidRuntime)"
15021502
ResolvedAssemblies="@(_ResolvedAssemblies)"
15031503
ResolvedUserAssemblies="@(_ResolvedUserMonoAndroidAssemblies)"
15041504
ErrorOnCustomJavaObject="$(AndroidErrorOnCustomJavaObject)"
@@ -1728,7 +1728,7 @@ because xbuild doesn't support framework reference assemblies.
17281728
</Target>
17291729

17301730
<Target Name="_GeneratePackageManagerJava"
1731-
Condition=" '$(_AndroidNativeAot)' != 'true' "
1731+
Condition=" '$(_AndroidRuntime)' != 'NativeAOT' "
17321732
DependsOnTargets="$(_GeneratePackageManagerJavaDependsOn)"
17331733
Inputs="@(_GeneratePackageManagerJavaInputs)"
17341734
Outputs="$(_AndroidStampDirectory)_GeneratePackageManagerJava.stamp">
@@ -1947,7 +1947,7 @@ because xbuild doesn't support framework reference assemblies.
19471947

19481948
<!-- Shrink Mono.Android.dll by removing attribute only needed for GenerateJavaStubs -->
19491949
<RemoveRegisterAttribute
1950-
Condition="'$(AndroidLinkMode)' != 'None' and '$(AndroidIncludeDebugSymbols)' != 'true' and '$(AndroidStripILAfterAOT)' != 'true' and '$(_AndroidNativeAot)' != 'true' "
1950+
Condition="'$(AndroidLinkMode)' != 'None' and '$(AndroidIncludeDebugSymbols)' != 'true' and '$(AndroidStripILAfterAOT)' != 'true' and '$(_AndroidRuntime)' != 'NativeAOT' "
19511951
ShrunkFrameworkAssemblies="@(_ShrunkAssemblies)" />
19521952

19531953
<MakeDir Directories="$(MonoAndroidIntermediateAssemblyDir)shrunk" />

0 commit comments

Comments
 (0)