The SDK and NDK packages can be installed via Android Studio or the sdkmanager command line tool.
Android Studio is more convenient but a larger installation.
The command line tools are smaller and usage can be scripted, but are a little more complicated to setup. They also require a Java runtime environment to be available.
* If necessary install the Java Runtime Environment and set the JAVA_HOME environment variable to point to it
* https://www.java.com/en/download/
* Windows note: You MUST install the 64-bit version (https://www.java.com/en/download/manual.jsp) otherwise sdkmanager will only list x86 packages and the latest NDK is x64 only.
* For sdkmanager to work it needs a certain directory structure. First create the top level directory for the Android infrastructure.
* in our example we'll call that `.../Android/`
* Download the command line tools from the 'Command line tools only' section towards the bottom of https://developer.android.com/studio
* Create a directory called 'cmdline-tools' under your top level directory
* giving `.../Android/cmdline-tools`
* Extract the 'tools' directory from the command line tools zip file into this directory
* giving `.../Android/cmdline-tools/tools`
* Windows note: preferably extract using 7-zip. If using the built in Windows zip extract tool you will need to fix the directory structure by moving the jar files from `tools\lib\_` up to `tools\lib`
* See https://stackoverflow.com/questions/27364963/could-not-find-or-load-main-class-com-android-sdkmanager-main
* You should now be able to run Android/cmdline-tools/bin/sdkmanager[.bat] successfully
* if you see an error about it being unable to save settings and the sdkmanager help text,
your directory structure is incorrect.
* see the final steps in this answer to double check: https://stackoverflow.com/a/61176718
* Run `.../Android/cmdline-tools/bin/sdkmanager --list` to see the packages available
* Install the SDK Platform
* Generally installing the latest is fine. You pick an API level when compiling the code and the latest platform will support many recent API levels e.g.
Android Archive (AAR) files, which can be imported directly in Android Studio, will be generated in your_build_dir/java/build/android/outputs/aar, by using the above building commands with `--build_java`
#### Note: Proguard rules for R8 minimization Android app builds to work
For Android consumers using the library with R8-minimized builds, currently you need to add the following line to your `proguard-rules.pro` file inside your Android project to use package `com.microsoft.onnxruntime:onnxruntime-android` (for Full build) or `com.microsoft.onnxruntime:onnxruntime-mobile` (for Mobile build) to avoid runtime crashes:
```
-keep class ai.onnxruntime.** { *; }
```
*Reference to similar issues:
There are similar open issues for this on the [protobuf Github project](https://github.com/protocolbuffers/protobuf/issues/6463) and [R8](https://issuetracker.google.com/issues/144631039).