Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<RuntimeIdentifier Condition=" '$(RuntimeIdentifier)' == '' ">linux-bionic-arm64</RuntimeIdentifier>
<!-- Needed for cross-compilation, e.g. build linux-bionic-arm64 from osx-x64 -->
<PlatformTarget>AnyCPU</PlatformTarget>
<!-- Needed so that we can use NativeAOT to cross-compile for Android (linux) from Windows -->
<DisableUnsupportedError Condition=" '$(OS)' == 'Windows_NT' ">true</DisableUnsupportedError>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@
<_NdkClangPrefix Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-arm64' ">aarch64-linux-android21-</_NdkClangPrefix>
<_NdkSysrootAbi Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-x64' ">x86_64-linux-android</_NdkSysrootAbi>
<_NdkClangPrefix Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-x64' ">x86_64-linux-android21-</_NdkClangPrefix>
<_NdkPrebuiltAbi Condition=" '$(NETCoreSdkRuntimeIdentifier)' == 'osx-x64' ">darwin-x86_64</_NdkPrebuiltAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('osx')) ">darwin-x86_64</_NdkPrebuiltAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('linux')) ">linux-x86_64</_NdkPrebuiltAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('windows')) ">windows-x86_64</_NdkPrebuiltAbi>
<_NdkSysrootLibDir>$(AndroidNdkDirectory)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)</_NdkSysrootLibDir>
<_NdkBinDir>$(AndroidNdkDirectory)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin</_NdkBinDir>
</PropertyGroup>

<PropertyGroup Condition="$(RuntimeIdentifier.StartsWith('linux-bionic'))">
<CppCompilerAndLinker>$(_NdkBinDir)/$(_NdkClangPrefix)clang</CppCompilerAndLinker>
<ObjCopyName>$(_NdkBinDir)/llvm-objcopy</ObjCopyName>
<CppCompilerAndLinker Condition=" '$(OS)' == 'Windows_NT' ">$(_NdkClangPrefix)clang.cmd</CppCompilerAndLinker>
<CppCompilerAndLinker Condition=" '$(OS)' != 'Windows_NT' ">$(_NdkBinDir)/$(_NdkClangPrefix)clang</CppCompilerAndLinker>
<ObjCopyName Condition=" '$(OS)' == 'Windows_NT' ">llvm-objcopy.exe</ObjCopyName>
<ObjCopyName Condition=" '$(OS)' != 'Windows_NT' ">$(_NdkBinDir)/llvm-objcopy</ObjCopyName>
</PropertyGroup>

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

<PropertyGroup>
<_HomeDir Condition=" '$(OS)' != 'Windows_NT' ">$(HOME)/</_HomeDir>
<_HomeDir Condition=" '$(OS)' == 'Windows_NT' ">$(HOMEDRIVE)$(HOMEPATH)\</_HomeDir>
</PropertyGroup>

<Target Name="_ValidateEnvironment"
BeforeTargets="Build">
<Error
Expand All @@ -43,6 +52,20 @@
Condition=" '$(AndroidSdkDirectory)' == '' Or !Exists($(AndroidSdkDirectory)) "
Text="Set the %24(AndroidSdkDirectory) MSBuild property or the %24ANDROID_HOME environment variable to the path of the Android SDK."
/>
<PropertyGroup>
<_DebugKeystorePath>$(_HomeDir).android/debug.keystore</_DebugKeystorePath>
</PropertyGroup>
<Error
Condition=" !Exists($(_DebugKeystorePath)) "
Text="Keystore file `$(_DebugKeystorePath)` does not exist. This is needed to sign .apk files. See README.md for details."
/>
<PropertyGroup>
<_WinNdkBinDir>$(_NdkBinDir.Replace('/', '%5c'))</_WinNdkBinDir>
</PropertyGroup>
<Error
Condition=" '$(OS)' == 'Windows_NT' And !$(PATH.Contains($(_WinNdkBinDir))) "
Text="The %PATH% environment variable must contain: $(_WinNdkBinDir)"
/>
</Target>

<ItemGroup>
Expand Down
40 changes: 40 additions & 0 deletions samples/Hello-NativeAOTFromAndroid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/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)

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.
Expand Down