|
| 1 | +# Guide to set up Java/SDK/NDK for Android |
| 2 | + |
| 3 | +Follow this doc if you haven't set up Java/SDK/NDK for Android development |
| 4 | +already. |
| 5 | +This doc provides a CLI tutorial to set them up. Otherwise, you can do the same |
| 6 | +thing with Android Studio GUI. |
| 7 | + |
| 8 | +## Set up Java 17 |
| 9 | +1. Download the archive from Oracle website. |
| 10 | +Make sure you have read and agree with the terms and conditions from the website before downloading. |
| 11 | +```bash |
| 12 | +export DEV_HOME=<path-to-dev> |
| 13 | +cd $DEV_HOME |
| 14 | +``` |
| 15 | +Linux: |
| 16 | +```bash |
| 17 | +curl https://download.oracle.com/java/17/archive/jdk-17.0.10_linux-x64_bin.tar.gz -o jdk-17.0.10.tar.gz |
| 18 | +``` |
| 19 | +macOS: |
| 20 | +```bash |
| 21 | +curl https://download.oracle.com/java/17/archive/jdk-17.0.10_macos-aarch64_bin.tar.gz -o jdk-17.0.10.tar.gz |
| 22 | +``` |
| 23 | +2. Unzip the archive. The directory named `jdk-17.0.10` is the Java root directory. |
| 24 | +```bash |
| 25 | +tar xf jdk-17.0.10.tar.gz |
| 26 | +``` |
| 27 | +3. Set `JAVA_HOME` and update `PATH`. |
| 28 | + |
| 29 | +Linux: |
| 30 | +```bash |
| 31 | +export JAVA_HOME="$DEV_HOME"/jdk-17.0.10 |
| 32 | +export PATH="$JAVA_HOME/bin:$PATH" |
| 33 | +``` |
| 34 | +macOS: |
| 35 | +```bash |
| 36 | +export JAVA_HOME="$DEV_HOME"/jdk-17.0.10.jdk/Contents/Home |
| 37 | +export PATH="$JAVA_HOME/bin:$PATH" |
| 38 | +``` |
| 39 | + |
| 40 | +Note: Oracle has tutorials for installing Java on |
| 41 | +[Linux](https://docs.oracle.com/en/java/javase/17/install/installation-jdk-linux-platforms.html#GUID-4A6BD592-1840-4BB4-A758-4CD49E9EE88B) |
| 42 | +and [macOS](https://docs.oracle.com/en/java/javase/17/install/installation-jdk-macos.html#GUID-E8A251B6-D9A9-4276-ABC8-CC0DAD62EA33). |
| 43 | +Some Linux distributions has JDK package in package manager. For example, Debian users can install |
| 44 | +openjdk-17-jdk package. |
| 45 | + |
| 46 | +## Set up Android SDK/NDK |
| 47 | +Android has a command line tool [sdkmanager](https://developer.android.com/tools/sdkmanager) which |
| 48 | +helps users managing SDK and other tools related to Android development. |
| 49 | + |
| 50 | +1. Go to https://developer.android.com/studio and download the archive from "Command line tools |
| 51 | +only" section. Make sure you have read and agree with the terms and conditions from the website. |
| 52 | + |
| 53 | +Linux: |
| 54 | +```bash |
| 55 | +curl https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -o commandlinetools.zip |
| 56 | +``` |
| 57 | +macOS: |
| 58 | +```bash |
| 59 | +curl https://dl.google.com/android/repository/commandlinetools-mac-11076708_latest.zip -o commandlinetools.zip |
| 60 | +``` |
| 61 | +2. Unzip. |
| 62 | +```bash |
| 63 | +unzip commandlinetools.zip |
| 64 | +``` |
| 65 | +3. Specify a root for Android SDK. For example, we can put it under `$DEV_HOME/sdk`. |
| 66 | + |
| 67 | +``` |
| 68 | +mkdir -p $DEV_HOME/sdk |
| 69 | +export ANDROID_HOME="$(realpath $DEV_HOME/sdk)" |
| 70 | +# Install SDK 34 |
| 71 | +./cmdline-tools/bin/sdkmanager --sdk_root="${ANDROID_HOME}" --install "platforms;android-34" |
| 72 | +# Install NDK |
| 73 | +./cmdline-tools/bin/sdkmanager --sdk_root="${ANDROID_HOME}" --install "ndk;25.0.8775105" |
| 74 | +# The NDK root is then under `ndk/<version>`. |
| 75 | +export ANDROID_NDK="$ANDROID_HOME/ndk/25.0.8775105" |
| 76 | +``` |
| 77 | + |
| 78 | +### (Optional) Android Studio Setup |
| 79 | +If you want to use Android Studio and never set up Java/SDK/NDK before, or if |
| 80 | +you use the newly installed ones, follow these steps to set Android Studio to use |
| 81 | +them. |
| 82 | + |
| 83 | +Copy these output paths to be used by Android Studio |
| 84 | +```bash |
| 85 | +echo $ANDROID_HOME |
| 86 | +echo $ANDROID_NDK |
| 87 | +echo $JAVA_HOME |
| 88 | +``` |
| 89 | + |
| 90 | +Open a project in Android Studio. In Project Structure (File -> Project |
| 91 | +Structure, or `⌘;`) -> SDK Location, |
| 92 | +* Set Android SDK Location to the path of $ANDROID_HOME |
| 93 | +* Set Android NDK Location to the path of $ANDROID_NDK |
| 94 | +* Set JDK location (Click Gradle Settings link) -> Gradle JDK -> Add JDK... to the path of $JAVA_HOME |
0 commit comments