Skip to content

Commit 9f48ecf

Browse files
[Hello-NativeAOTFromAndroid] Add instructions for building on Windows (#1320)
Co-authored-by: Filip Navara <[email protected]>
1 parent 93c3872 commit 9f48ecf

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<RuntimeIdentifier Condition=" '$(RuntimeIdentifier)' == '' ">linux-bionic-arm64</RuntimeIdentifier>
1818
<!-- Needed for cross-compilation, e.g. build linux-bionic-arm64 from osx-x64 -->
1919
<PlatformTarget>AnyCPU</PlatformTarget>
20+
<!-- Needed so that we can use NativeAOT to cross-compile for Android (linux) from Windows -->
21+
<DisableUnsupportedError Condition=" '$(OS)' == 'Windows_NT' ">true</DisableUnsupportedError>
2022
</PropertyGroup>
2123

2224
<ItemGroup>

samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.targets

+26-3
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,29 @@
1515
<_NdkClangPrefix Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-arm64' ">aarch64-linux-android21-</_NdkClangPrefix>
1616
<_NdkSysrootAbi Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-x64' ">x86_64-linux-android</_NdkSysrootAbi>
1717
<_NdkClangPrefix Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-x64' ">x86_64-linux-android21-</_NdkClangPrefix>
18-
<_NdkPrebuiltAbi Condition=" '$(NETCoreSdkRuntimeIdentifier)' == 'osx-x64' ">darwin-x86_64</_NdkPrebuiltAbi>
18+
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('osx')) ">darwin-x86_64</_NdkPrebuiltAbi>
19+
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('linux')) ">linux-x86_64</_NdkPrebuiltAbi>
20+
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('windows')) ">windows-x86_64</_NdkPrebuiltAbi>
1921
<_NdkSysrootLibDir>$(AndroidNdkDirectory)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)</_NdkSysrootLibDir>
2022
<_NdkBinDir>$(AndroidNdkDirectory)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin</_NdkBinDir>
2123
</PropertyGroup>
2224

2325
<PropertyGroup Condition="$(RuntimeIdentifier.StartsWith('linux-bionic'))">
24-
<CppCompilerAndLinker>$(_NdkBinDir)/$(_NdkClangPrefix)clang</CppCompilerAndLinker>
25-
<ObjCopyName>$(_NdkBinDir)/llvm-objcopy</ObjCopyName>
26+
<CppCompilerAndLinker Condition=" '$(OS)' == 'Windows_NT' ">$(_NdkClangPrefix)clang.cmd</CppCompilerAndLinker>
27+
<CppCompilerAndLinker Condition=" '$(OS)' != 'Windows_NT' ">$(_NdkBinDir)/$(_NdkClangPrefix)clang</CppCompilerAndLinker>
28+
<ObjCopyName Condition=" '$(OS)' == 'Windows_NT' ">llvm-objcopy.exe</ObjCopyName>
29+
<ObjCopyName Condition=" '$(OS)' != 'Windows_NT' ">$(_NdkBinDir)/llvm-objcopy</ObjCopyName>
2630
</PropertyGroup>
2731

2832
<ItemGroup Condition="$(RuntimeIdentifier.StartsWith('linux-bionic'))">
2933
<LinkerArg Include="-Wl,--undefined-version" />
3034
</ItemGroup>
3135

36+
<PropertyGroup>
37+
<_HomeDir Condition=" '$(OS)' != 'Windows_NT' ">$(HOME)/</_HomeDir>
38+
<_HomeDir Condition=" '$(OS)' == 'Windows_NT' ">$(HOMEDRIVE)$(HOMEPATH)\</_HomeDir>
39+
</PropertyGroup>
40+
3241
<Target Name="_ValidateEnvironment"
3342
BeforeTargets="Build">
3443
<Error
@@ -43,6 +52,20 @@
4352
Condition=" '$(AndroidSdkDirectory)' == '' Or !Exists($(AndroidSdkDirectory)) "
4453
Text="Set the %24(AndroidSdkDirectory) MSBuild property or the %24ANDROID_HOME environment variable to the path of the Android SDK."
4554
/>
55+
<PropertyGroup>
56+
<_DebugKeystorePath>$(_HomeDir).android/debug.keystore</_DebugKeystorePath>
57+
</PropertyGroup>
58+
<Error
59+
Condition=" !Exists($(_DebugKeystorePath)) "
60+
Text="Keystore file `$(_DebugKeystorePath)` does not exist. This is needed to sign .apk files. See README.md for details."
61+
/>
62+
<PropertyGroup>
63+
<_WinNdkBinDir>$(_NdkBinDir.Replace('/', '%5c'))</_WinNdkBinDir>
64+
</PropertyGroup>
65+
<Error
66+
Condition=" '$(OS)' == 'Windows_NT' And !$(PATH.Contains($(_WinNdkBinDir))) "
67+
Text="The %PATH% environment variable must contain: $(_WinNdkBinDir)"
68+
/>
4669
</Target>
4770

4871
<ItemGroup>

samples/Hello-NativeAOTFromAndroid/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@ by a Java Virtual Machine (JVM).
66

77
Extend this idea for Android!
88

9+
## Prerequisites
10+
11+
In order to build this sample, the Android SDK and Android NDK must be present.
12+
*An* easy way to provision these is to build dotnet/android:
13+
14+
* [Windows build instructions](https://github.com/dotnet/android/blob/155709f9917666ca046c79a4e9769924ff4ab9bb/Documentation/building/windows/instructions.md)
15+
* [Linux and macOS build instructions](https://github.com/dotnet/android/blob/main/Documentation/building/unix/instructions.md)
16+
17+
Once you've run `make prepar` or `dotnet msbuild Xamarin.Android.sln -t:Prepare`,
18+
then an `android-toolchain` directory will exist in your home directory.
19+
20+
A `.android/debug.keystore` file must also exist within your home directory.
21+
This file can be created by using
22+
[`keytool -genkeypair`](https://docs.oracle.com/en/java/javase/11/tools/keytool.html).
23+
24+
* On **Windows**, run the following command within a `CMD.EXE` window:
25+
26+
```cmd
27+
%HOMEDRIVE%%HOMEPATH%\android-toolchain\jdk-17\bin\keytool.exe -genkeypair ^
28+
-keyalg RSA -validity 10950 -dname "CN=Android Debug,O=Android,C=US" ^
29+
-keystore %HOMEDRIVE%%HOMEPATH%\.android\debug.keystore ^
30+
-alias androiddebugkey -storepass android -keypass android
31+
```
32+
33+
* On **Linux** and **macOS**, run:
34+
35+
```sh
36+
$HOME/android-toolchain/jdk-17/bin/keytool -genkeypair \
37+
-keyalg RSA -validity 10950 -dname "CN=Android Debug,O=Android,C=US" \
38+
-keystore $HOME/.android/debug.keystore \
39+
-alias androiddebugkey -storepass android -keypass android
40+
```
41+
42+
The NativeAOT toolchain on Windows also requires that the NDK "prebuilt"
43+
directory be located within `%PATH%`
44+
45+
```cmd
46+
set PATH=%HOMEDRIVE%%HOMEPATH%\android-toolchain\ndk\toolchains\llvm\prebuilt\windows-x86_64\bin;%PATH%
47+
```
48+
949
## Building
1050

1151
Building a native library with NativeAOT requires a Release configuration build.

0 commit comments

Comments
 (0)