Skip to content

Commit 1e322fd

Browse files
Add NativeAOT on Android test (#118037)
Adds a project to run a NativeAOT shared library on Android. The android test builds a single native library that exports the methods that are expected by the monodroid tests (InitRuntime, SetEnv, ExecEntryPoint, FreeUnmanagedResources). JNI struct definitions are copied to get arg strings from the JNIEnv object. ApkBuilder no longer needs to compile the monodroid native library, but it does need to add the .jar from the cryptography library to the apk bundle in order to run crypto operations. Build targets were updated to account for the new NativeAOT tests, and a new job was added for NativeAOT on android tests. Co-authored-by: Filip Navara <[email protected]>
1 parent 942c474 commit 1e322fd

File tree

17 files changed

+663
-84
lines changed

17 files changed

+663
-84
lines changed

eng/pipelines/extra-platforms/runtime-extra-platforms-android.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,35 @@ jobs:
114114
parameters:
115115
creator: dotnet-bot
116116
testRunNamePrefixSuffix: CoreCLR_$(_BuildConfig)
117+
118+
#
119+
# Android arm64 devices and x64 emulators
120+
# Build the whole product using NativeAOT and run functional tests
121+
#
122+
- template: /eng/pipelines/common/platform-matrix.yml
123+
parameters:
124+
jobTemplate: /eng/pipelines/common/global-build-job.yml
125+
helixQueuesTemplate: /eng/pipelines/libraries/helix-queues-setup.yml
126+
buildConfig: Release
127+
runtimeFlavor: coreclr
128+
platforms:
129+
- android_x64
130+
- android_arm64
131+
variables:
132+
# map dependencies variables to local variables
133+
- name: librariesContainsChange
134+
value: $[ stageDependencies.EvaluatePaths.evaluate_paths.outputs['SetPathVars_libraries.containsChange'] ]
135+
- name: coreclrContainsChange
136+
value: $[ stageDependencies.EvaluatePaths.evaluate_paths.outputs['SetPathVars_coreclr.containsChange'] ]
137+
jobParameters:
138+
testGroup: innerloop
139+
nameSuffix: NativeAOT
140+
isExtraPlatforms: ${{ parameters.isExtraPlatformsBuild }}
141+
buildArgs: -s clr.aot+libs+libs.tests -c $(_BuildConfig) /p:ArchiveTests=true /p:RunSmokeTestsOnly=true /p:UseNativeAOTRuntime=true /p:RuntimeFlavor=coreclr /p:TestNativeAOT=true
142+
timeoutInMinutes: 120
143+
# extra steps, run tests
144+
postBuildSteps:
145+
- template: /eng/pipelines/libraries/helix.yml
146+
parameters:
147+
creator: dotnet-bot
148+
testRunNamePrefixSuffix: NativeAOT_$(_BuildConfig)

eng/pipelines/extra-platforms/runtime-extra-platforms-androidemulator.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,37 @@ jobs:
149149
parameters:
150150
creator: dotnet-bot
151151
testRunNamePrefixSuffix: CoreCLR_$(_BuildConfig)
152+
153+
#
154+
# Android arm64 devices and x64 emulators
155+
# Build the whole product using NativeAOT and run functional tests
156+
#
157+
- template: /eng/pipelines/common/platform-matrix.yml
158+
parameters:
159+
jobTemplate: /eng/pipelines/common/global-build-job.yml
160+
helixQueuesTemplate: /eng/pipelines/libraries/helix-queues-setup.yml
161+
buildConfig: Release
162+
runtimeFlavor: coreclr
163+
isExtraPlatformsBuild: ${{ parameters.isExtraPlatformsBuild }}
164+
isAndroidEmulatorOnlyBuild: ${{ parameters.isAndroidEmulatorOnlyBuild }}
165+
platforms:
166+
- android_x64
167+
- android_arm64
168+
variables:
169+
# map dependencies variables to local variables
170+
- name: librariesContainsChange
171+
value: $[ stageDependencies.EvaluatePaths.evaluate_paths.outputs['SetPathVars_libraries.containsChange'] ]
172+
- name: coreclrContainsChange
173+
value: $[ stageDependencies.EvaluatePaths.evaluate_paths.outputs['SetPathVars_coreclr.containsChange'] ]
174+
jobParameters:
175+
testGroup: innerloop
176+
nameSuffix: NativeAOT
177+
isExtraPlatforms: ${{ parameters.isExtraPlatformsBuild }}
178+
buildArgs: -s clr.aot+libs+libs.tests -c $(_BuildConfig) /p:ArchiveTests=true /p:RunSmokeTestsOnly=true /p:UseNativeAOTRuntime=true /p:RuntimeFlavor=coreclr /p:TestNativeAOT=true
179+
timeoutInMinutes: 120
180+
# extra steps, run tests
181+
postBuildSteps:
182+
- template: /eng/pipelines/libraries/helix.yml
183+
parameters:
184+
creator: dotnet-bot
185+
testRunNamePrefixSuffix: NativeAOT_$(_BuildConfig)

eng/testing/tests.android.targets

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<MainLibraryFileName Condition="'$(MainLibraryFileName)' == ''">AndroidTestRunner.dll</MainLibraryFileName>
2929

3030
<AndroidBuildDir>$(PublishDir)</AndroidBuildDir>
31-
<AndroidBundleDir>$(BundleDir)</AndroidBundleDir>
3231
</PropertyGroup>
3332

3433
<ItemGroup>

eng/testing/tests.singlefile.targets

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project>
22
<PropertyGroup>
3-
<OutputType>Exe</OutputType>
3+
<OutputType Condition="!('$(TargetOS)' == 'android' and '$(TestNativeAot)' == 'true')">Exe</OutputType>
44

5-
<BundleDir>$([MSBuild]::NormalizeDirectory('$(OutDir)', 'publish'))</BundleDir>
5+
<BundleDir Condition="'$(BundleDir)' == ''">$([MSBuild]::NormalizeDirectory('$(OutDir)', 'publish'))</BundleDir>
66
<RunScriptOutputPath>$([MSBuild]::NormalizePath('$(BundleDir)', '$(RunScriptOutputName)'))</RunScriptOutputPath>
77
<RuntimeIdentifier>$(TargetRid)</RuntimeIdentifier>
88

@@ -49,7 +49,8 @@
4949

5050
<ItemGroup>
5151
<Compile Include="$(CommonTestPath)SingleFileTestRunner\SingleFileTestRunner.cs"
52-
Link="Common\SingleFileTestRunner\SingleFileTestRunner.cs" />
52+
Link="Common\SingleFileTestRunner\SingleFileTestRunner.cs"
53+
Condition="'$(IsFunctionalTest)' != 'true'" />
5354
</ItemGroup>
5455

5556
<ItemGroup>

src/coreclr/nativeaot/BuildIntegration/Microsoft.DotNet.ILCompiler.SingleEntry.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<_targetOS>$(_originalTargetOS)</_targetOS>
3232
<_linuxToken>linux-</_linuxToken>
3333
<_linuxLibcFlavor Condition="$(_targetOS.StartsWith($(_linuxToken)))">$(_targetOS.SubString($(_linuxToken.Length)))</_linuxLibcFlavor>
34+
<_linuxLibcFlavor Condition="'$(_targetOS)' == 'android'">bionic</_linuxLibcFlavor>
3435
<_targetOS Condition="$(_targetOS.StartsWith($(_linuxToken)))">linux</_targetOS>
3536

3637
<!-- linux-bionic on ARM uses armel (softfp) ABI -->

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ The .NET Foundation licenses this file to you under the MIT license.
4747
<CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-arm'))">armv7</CrossCompileArch>
4848

4949
<CrossCompileAbi>gnu</CrossCompileAbi>
50-
<CrossCompileAbi Condition="$(CrossCompileRid.StartsWith('linux-bionic-'))">android21</CrossCompileAbi>
50+
<CrossCompileAbi Condition="$(CrossCompileRid.StartsWith('linux-bionic-')) or $(CrossCompileRid.StartsWith('android-'))">android21</CrossCompileAbi>
5151
<CrossCompileAbi Condition="$(CrossCompileRid.StartsWith('linux-musl-')) or $(CrossCompileRid.StartsWith('alpine-'))">musl</CrossCompileAbi>
5252
<CrossCompileAbi Condition="'$(CrossCompileRid)' == 'linux-arm'">gnueabihf</CrossCompileAbi>
53-
<CrossCompileAbi Condition="'$(CrossCompileRid)' == 'linux-bionic-arm'">androideabi21</CrossCompileAbi>
53+
<CrossCompileAbi Condition="'$(CrossCompileRid)' == 'linux-bionic-arm' or '$(CrossCompileRid)' == 'android-arm'">androideabi21</CrossCompileAbi>
5454
<CrossCompileAbi Condition="'$(CrossCompileRid)' == 'linux-musl-arm'">musleabihf</CrossCompileAbi>
5555
</PropertyGroup>
5656

src/coreclr/nativeaot/Runtime/interoplibinterface_java.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "threadstore.h"
1414
#include "threadstore.inl"
1515
#include "event.h"
16+
#include "thread.inl"
1617

1718
#include "interoplibinterface.h"
1819

src/coreclr/tools/aot/ILCompiler.Build.Tasks/ComputeManagedAssembliesToCompileToNative.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ public override bool Execute()
130130
continue;
131131
}
132132

133-
if (isFromRuntimePack && taskItem.GetMetadata("AssetType")?.Equals("native", StringComparison.OrdinalIgnoreCase) == true)
133+
if (isFromRuntimePack && taskItem.GetMetadata("AssetType")?.Equals("native", StringComparison.OrdinalIgnoreCase) == true
134+
&& !assemblyFileName.EndsWith(".jar", StringComparison.OrdinalIgnoreCase)
135+
&& !assemblyFileName.EndsWith(".dex", StringComparison.OrdinalIgnoreCase))
134136
{
135137
// Skip the native components of the runtime pack, we don't need them for NativeAOT.
136138
assembliesToSkipPublish.Add(taskItem);

src/libraries/tests.proj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@
572572
<ProjectReference Include="$(MSBuildThisFileDirectory)System.Runtime\tests\System.Globalization.Tests\System.Globalization.Tests.csproj" Condition="'$(OS)' != 'Windows_NT'" />
573573
</ItemGroup>
574574

575-
<ItemGroup Condition="'$(TestNativeAot)' == 'true'">
575+
<ItemGroup Condition="'$(TestNativeAot)' == 'true' and '$(TargetOS)' != 'android'">
576576
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Collections\tests\System.Collections.Tests.csproj" />
577577
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.IO.Compression\tests\System.IO.Compression.Tests.csproj" />
578578
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Linq.Expressions\tests\System.Linq.Expressions.Tests.csproj" />
@@ -594,7 +594,7 @@
594594
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Net.Http\tests\FunctionalTests\System.Net.Http.Functional.Tests.csproj" />
595595
</ItemGroup>
596596

597-
<ItemGroup Condition="'$(TargetOS)' == 'android' and '$(RuntimeFlavor)' == 'CoreCLR'">
597+
<ItemGroup Condition="'$(TargetOS)' == 'android' and '$(RuntimeFlavor)' == 'CoreCLR' and '$(TestNativeAOT)' != 'true'">
598598
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Runtime.InteropServices\tests\System.Runtime.InteropServices.UnitTests\System.Runtime.InteropServices.Tests.csproj" />
599599
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Runtime\tests\System.Globalization.Tests\System.Globalization.Tests.csproj" />
600600
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Net.Mail\tests\Functional\System.Net.Mail.Functional.Tests.csproj" />
@@ -606,6 +606,13 @@
606606
<SmokeTestProject Include="$(RepoRoot)\src\tests\FunctionalTests\Android\Device_Emulator\JIT\*.Test.csproj"/>
607607
</ItemGroup>
608608

609+
<ItemGroup Condition="'$(TargetOS)' == 'android' and '$(TestNativeAOT)' == 'true'">
610+
<SmokeTestProject Include="$(RepoRoot)src\tests\FunctionalTests\Android\Device_Emulator\NativeAOT\*.Test.csproj"/>
611+
</ItemGroup>
612+
<ItemGroup>
613+
<ProjectExclusions Include="$(RepoRoot)\src\tests\FunctionalTests\Android\Device_Emulator\NativeAOT\*.Test.csproj" />
614+
</ItemGroup>
615+
609616
<ItemGroup Condition="('$(TargetOS)' == 'iossimulator' or '$(TargetOS)' == 'tvossimulator') and '$(RuntimeFlavor)' == 'CoreCLR'">
610617
<SmokeTestProject Remove="@(SmokeTestProject)" />
611618
<SmokeTestProject Include="$(RepoRoot)\src\tests\FunctionalTests\iOS\Simulator\CoreCLR.Interpreter\*.Test.csproj"/>

src/mono/msbuild/android/build/AndroidBuild.props

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
<StaticLinkedRuntime Condition="'$(RuntimeFlavor)' == 'coreclr' and '$(StaticLinkedRuntime)' == ''">false</StaticLinkedRuntime>
1818

19+
<!-- Android NDK uses x86_64 as the architecture for naming, even on MacOS arm64 -->
20+
<NdkToolchainPrebuiltOS Condition="$([MSBuild]::IsOSPlatform('Linux'))">linux-x86_64</NdkToolchainPrebuiltOS>
21+
<NdkToolchainPrebuiltOS Condition="$([MSBuild]::IsOSPlatform('OSX'))">darwin-x86_64</NdkToolchainPrebuiltOS>
22+
<NdkToolchainPrebuiltOS Condition="'$(HostOS)' == 'windows'">windows-x86_64</NdkToolchainPrebuiltOS>
23+
<CppCompilerAndLinker>$(ANDROID_NDK_ROOT)\toolchains\llvm\prebuilt\$(NdkToolchainPrebuiltOS)\bin\clang</CppCompilerAndLinker>
24+
1925
<AndroidBuildAfterThisTarget Condition="'$(AndroidBuildAfterThisTarget)' == ''">Publish</AndroidBuildAfterThisTarget>
2026
<AndroidBuildDependsOn Condition="'$(UseMonoRuntime)' == 'true'">
2127
$(_ReadRuntimeComponentsManifestTargetName);
@@ -35,6 +41,11 @@
3541
_AndroidGenerateAppBundle;
3642
_AfterAndroidBuild
3743
</AndroidBuildDependsOn>
44+
<AndroidBuildDependsOn Condition="'$(UseNativeAOTRuntime)' == 'true'">
45+
$(AndroidBuildDependsOn);
46+
CopyNativeBinary;
47+
_CopyAotSymbols
48+
</AndroidBuildDependsOn>
3849

3950
<!-- When building on Helix $(_CommonTargetsDir) will be properly set, otherwise we have to set it to a in-tree location -->
4051
<_CommonTargetsDir Condition="'$(_CommonTargetsDir)' == ''">$([MSBuild]::NormalizeDirectory($(MSBuildThisFileDirectory), '..', '..', 'common'))</_CommonTargetsDir>

0 commit comments

Comments
 (0)