|
| 1 | +--- |
| 2 | +title: "Install .NET for Android dependencies" |
| 3 | +description: "Learn how to install .NET for Android dependencies so you can create native Android applications." |
| 4 | +ms.date: 11/01/2023 |
| 5 | +--- |
| 6 | +# Install .NET for Android dependencies |
| 7 | + |
| 8 | +In order to build .NET for Android applications you need to install the Android SDK and the Java SDK. |
| 9 | + |
| 10 | +## Using "InstallAndroidDependencies" target |
| 11 | + |
| 12 | +The easiest way to install the required dependencies for your Android application is to run the |
| 13 | +[`InstallAndroidDependencies`](../../building-apps/build-targets.md#installandroiddependencies) |
| 14 | +MSBuild target. |
| 15 | + |
| 16 | +This target will examine your application project and install the exact components which are needed. |
| 17 | +If you update your project to target a new Android API you will need to run this target again |
| 18 | +to make sure you get the required components. |
| 19 | + |
| 20 | +For example if you are upgrading your project to target API 34 from API 32, you will only have |
| 21 | +API 32 installed. Running the `InstallAndroidDependencies` target will install API 34 for you. |
| 22 | + |
| 23 | +If you do not have the Android SDK installed at all, this target can also handle installing the SDK |
| 24 | +on a clean machine. You can change the destination of the installation by setting the |
| 25 | +`AndroidSdkDirectory` MSBuild property. It will also install the Java SDK if the `JavaSdkDirectory` |
| 26 | +MSBuild property is provided. |
| 27 | + |
| 28 | +```dotnetcli |
| 29 | +dotnet build -t:InstallAndroidDependencies -f net8.0-android -p:AndroidSdkDirectory=c:\work\android-sdk -p:JavaSdkDirectory=c:\work\jdk -p:AcceptAndroidSdkLicenses=True |
| 30 | +``` |
| 31 | + |
| 32 | +Here are all the arguments which the target will use when installing the dependencies: |
| 33 | + |
| 34 | +* `-p:AndroidSdkDirectory="<PATH>"` installs or updates Android dependencies to the specified path. |
| 35 | + *Note*: You must use an absolute path; Unix developers should not use tilde (`~`), as it is |
| 36 | + not expanded when used *within* a command-line argument. |
| 37 | + |
| 38 | +* `-p:JavaSdkDirectory="<PATH>"` installs Java to the specified path. |
| 39 | + *Note*: You must use an absolute path; Unix developers should not use tilde (`~`), as it is |
| 40 | + not expanded when used *within* a command-line argument. |
| 41 | + |
| 42 | +* `-p:AcceptAndroidSDKLicenses=True` accepts the necessary Android licenses for development. |
| 43 | + |
| 44 | +> [!NOTE] |
| 45 | +> To make development easier try to avoid using paths which contain spaces or non-ASCII characters. |
| 46 | +
|
| 47 | +## Install the Android SDK manually |
| 48 | + |
| 49 | +You might find it necessary to install the Android SDK manually: |
| 50 | + |
| 51 | + 1. Go to [Android Studio download](https://developer.android.com/studio#download). |
| 52 | + Scroll down to the "Command Line Tools only" section and download the zip file for your operating system. |
| 53 | + |
| 54 | + 2. Create an `android-sdk` directory somewhere on your hard drive. To make your life easier create it near to the root of the drive. For example `c:\android-sdk`. |
| 55 | + |
| 56 | + 3. Extract the files from the zip file into this directory. You should end up with a folder structure like |
| 57 | + `android-sdk\cmdline-tools` |
| 58 | + |
| 59 | + 4. Open a terminal or Command Prompt. |
| 60 | + |
| 61 | + 5. Navigate to the `android-sdk\cmdline-tools\bin` directory within the directory you created. |
| 62 | + |
| 63 | + 6. Run the `sdkmanager` command to install the desired components. |
| 64 | + |
| 65 | +For example, to install the latest platform and platform tools, use: |
| 66 | + |
| 67 | +```console |
| 68 | +sdkmanager "platforms;android-34" "platform-tools" "build-tools;34.0.0" "emulator" "system-images;android-34;default;x86_64" "cmdline-tools;11.0" --sdk_root=c:\android-sdk |
| 69 | +``` |
| 70 | + |
| 71 | +Note that double-quotes should be used liberally to enclose the semicolon `;`, which is part of the component names. |
| 72 | + |
| 73 | +You will be prompted to accept the license, after which the Android SDK will install. |
| 74 | + |
| 75 | +You can use `sdkmanager` to install additional components. You can use the `--list` argument to get a list of all the available components. You can then look through the list and find the additional components you want. |
| 76 | + |
| 77 | +```console |
| 78 | +sdkmanager --list |
| 79 | +``` |
| 80 | + |
| 81 | +The following component types are useful to know: |
| 82 | + |
| 83 | + * `platforms;android-XX`: Installs the platform `android-XX` into the sdk. |
| 84 | + Replace *XX* with the API Level of your chosen platform. |
| 85 | + For example `platforms;android-30` will install Android API 30, while |
| 86 | + `platforms;android-21` will install Android API 21. |
| 87 | + |
| 88 | + * `system-images;android-XX;default;x86_64`: Installs an emulator image for |
| 89 | + the specific API level. The `x86_64` can be swapped out for different ABIs |
| 90 | + such as `x86`, `arm64-v8a`, and `x86_64`. These reflect the ABI of the image |
| 91 | + being installed. This can be useful if you have issues on specific ABI's. |
| 92 | + |
| 93 | +It is also good practice to set the `ANDROID_HOME` environment variable, as this |
| 94 | +allows you to use certain tooling from the command line. |
| 95 | + |
| 96 | +## Install Microsoft JDK manually |
| 97 | + |
| 98 | +In order to build .NET for Android applications or libraries you need to have a version of the Java Development Kit installed. |
| 99 | +We recommend you use the Microsoft Open JDK, this has been tested against our .NET for Android builds: |
| 100 | + |
| 101 | + 1. Download [Microsoft OpenJDK 11](/java/openjdk/download#openjdk-11). |
| 102 | + |
| 103 | + 2. Depending on your platform run the appropriate installer. |
| 104 | + |
| 105 | + 3. It is also good practice to set the `JAVA_HOME` environment variable. |
| 106 | + This will allow you to use the JDK from the Command Prompt or Terminal. |
0 commit comments