From 2f9c6e5e513d9a71c4637a1617c0894af3b2ddf4 Mon Sep 17 00:00:00 2001 From: "Jonathan Pryor (HE/HIM)" Date: Tue, 11 Mar 2025 13:13:30 -0400 Subject: [PATCH 1/2] [Hello-NativeAOTFromAndroid] Add instructions for building on Windows --- .../Hello-NativeAOTFromAndroid.csproj | 2 + .../Hello-NativeAOTFromAndroid.targets | 29 ++++++++++++-- samples/Hello-NativeAOTFromAndroid/README.md | 40 +++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.csproj b/samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.csproj index 77ce881da..3b9bb1d03 100644 --- a/samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.csproj +++ b/samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.csproj @@ -17,6 +17,8 @@ linux-bionic-arm64 AnyCPU + + true diff --git a/samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.targets b/samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.targets index c9f836e93..9be78249c 100644 --- a/samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.targets +++ b/samples/Hello-NativeAOTFromAndroid/Hello-NativeAOTFromAndroid.targets @@ -15,20 +15,29 @@ <_NdkClangPrefix Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-arm64' ">aarch64-linux-android21- <_NdkSysrootAbi Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-x64' ">x86_64-linux-android <_NdkClangPrefix Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-x64' ">x86_64-linux-android21- - <_NdkPrebuiltAbi Condition=" '$(NETCoreSdkRuntimeIdentifier)' == 'osx-x64' ">darwin-x86_64 + <_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('osx')) ">darwin-x86_64 + <_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('linux')) ">linux-x86_64 + <_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('windows')) ">windows-x86_64 <_NdkSysrootLibDir>$(AndroidNdkDirectory)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi) <_NdkBinDir>$(AndroidNdkDirectory)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin - $(_NdkBinDir)/$(_NdkClangPrefix)clang - $(_NdkBinDir)/llvm-objcopy + $(_NdkClangPrefix)clang.cmd + $(_NdkBinDir)/$(_NdkClangPrefix)clang + llvm-objcopy.exe + $(_NdkBinDir)/llvm-objcopy + + <_HomeDir Condition=" '$(OS)' != 'Windows_NT' ">$(HOME)/ + <_HomeDir Condition=" '$(OS)' == 'Windows_NT' ">$(HOMEDRIVE)$(HOMEPATH)\ + + + + <_DebugKeystorePath>$(_HomeDir).android/debug.keystore + + + + <_WinNdkBinDir>$(_NdkBinDir.Replace('/', '%5c')) + + diff --git a/samples/Hello-NativeAOTFromAndroid/README.md b/samples/Hello-NativeAOTFromAndroid/README.md index 628121a7a..84834f811 100644 --- a/samples/Hello-NativeAOTFromAndroid/README.md +++ b/samples/Hello-NativeAOTFromAndroid/README.md @@ -6,6 +6,46 @@ by a Java Virtual Machine (JVM). Extend this idea for Android! +## Prerequisites + +In order to build this sample, the Android SDK and Android NDK must be present. +*An* easy way to provision these is to build dotnet/anddroid: + + * [Windows build instructions](https://github.com/dotnet/android/blob/155709f9917666ca046c79a4e9769924ff4ab9bb/Documentation/building/windows/instructions.md) + * [Linux and macOS build instructions](https://github.com/dotnet/android/blob/main/Documentation/building/unix/instructions.md) + +Once you've run `make prepar` or `dotnet msbuild Xamarin.Android.sln -t:Prepare`, +then an `android-toolchain` directory will exist in your home directory. + +A `.android/debug.keystore` file must also exist within your home directory. +This file can be created by using +[`keytool -genkeypair`](https://docs.oracle.com/en/java/javase/11/tools/keytool.html). + + * On **Windows**, run the following command within a `CMD.EXE` window: + + ```cmd + %HOMEDRIVE%%HOMEPATH%\android-toolchain\jdk-17\bin\keytool.exe -genkeypair ^ + -keyalg RSA -validity 10950 -dname "CN=Android Debug,O=Android,C=US" ^ + -keystore %HOMEDRIVE%%HOMEPATH%\.android\debug.keystore ^ + -alias androiddebugkey -storepass android -keypass android + ``` + + * On **Linux** and **macOS**, run: + + ```sh + $HOME/android-toolchain/jdk-17/bin/keytool -genkeypair \ + -keyalg RSA -validity 10950 -dname "CN=Android Debug,O=Android,C=US" \ + -keystore $HOME/.android/debug.keystore \ + -alias androiddebugkey -storepass android -keypass android + ``` + +The NativeAOT toolchain on Windows also requires that the NDK "prebuilt" +directory be located within `%PATH%` + +```cmd +set PATH=%HOMEDRIVE%%HOMEPATH%\android-toolchain\ndk\toolchains\llvm\prebuilt\windows-x86_64\bin;%PATH% +``` + ## Building Building a native library with NativeAOT requires a Release configuration build. From b90e226f8ce26a08d9d0ba49eaecb5a215df6d5a Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Tue, 11 Mar 2025 13:49:06 -0400 Subject: [PATCH 2/2] Update samples/Hello-NativeAOTFromAndroid/README.md Co-authored-by: Filip Navara --- samples/Hello-NativeAOTFromAndroid/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Hello-NativeAOTFromAndroid/README.md b/samples/Hello-NativeAOTFromAndroid/README.md index 84834f811..e988722a5 100644 --- a/samples/Hello-NativeAOTFromAndroid/README.md +++ b/samples/Hello-NativeAOTFromAndroid/README.md @@ -9,7 +9,7 @@ Extend this idea for Android! ## Prerequisites In order to build this sample, the Android SDK and Android NDK must be present. -*An* easy way to provision these is to build dotnet/anddroid: +*An* easy way to provision these is to build dotnet/android: * [Windows build instructions](https://github.com/dotnet/android/blob/155709f9917666ca046c79a4e9769924ff4ab9bb/Documentation/building/windows/instructions.md) * [Linux and macOS build instructions](https://github.com/dotnet/android/blob/main/Documentation/building/unix/instructions.md)