How to install a specific Android NDK version on the Android/Linux stack

If the NDK version which is preinstalled on the Android stack is not the one you want to use, you can install any version via a Script step.

Simply add a Script step as the very first step in the workflow, with the following content:

#!/usr/bin/env bash
set -ex

# set env vars
export ANDROID_NDK_VERSION='r15b'
export ANDROID_NDK_HOME='/opt/android-ndk'
# expose for subsequent steps
envman add --key ANDROID_NDK_HOME --value "$ANDROID_NDK_HOME"


# ------------------------------------------------------
# --- Android NDK

# clean up if a previous version is already installed
rm -rf "$ANDROID_NDK_HOME"

# download
mkdir /opt/android-ndk-tmp
cd /opt/android-ndk-tmp
wget -q https://dl.google.com/android/repository/android-ndk-${ANDROID_NDK_VERSION}-linux-x86_64.zip

# uncompress
unzip -q android-ndk-${ANDROID_NDK_VERSION}-linux-x86_64.zip

# move to its final location
mv ./android-ndk-${ANDROID_NDK_VERSION} ${ANDROID_NDK_HOME}

# remove temp dir
cd ${ANDROID_NDK_HOME}
rm -rf /opt/android-ndk-tmp


# add to PATH
export PATH=${PATH}:${ANDROID_NDK_HOME}
# expose for subsequent steps
envman add --key PATH --value "$PATH"

Don’t forget to specify the version, at the top of the script (ANDROID_NDK_VERSION variable - in this example it’s set to r15b).

That’s all! :slight_smile:

4 Likes

ANDROID_NDK_HOME is already added to PATH in Dockerfile: https://github.com/bitrise-docker/android-ndk/blob/master/Dockerfile#L30

Value of ANDROID_NDK_HOME is not changed so there should be no need to append it again to PATH.

You’re right in case of the bitrise.io environment, but this #how-to was created as a generic guide, which can work even locally on your own Mac/PC :wink:

Hi! Thank you for the script! Nevertheless, I was trying to include it in my builds and faced the following problem:

+ unzip -q android-ndk-r10e-linux-x86_64.zip
replace android-ndk-r10e/platforms/android-21/arch-arm/usr/include/linux/netfilter/xt_CONNMARK.h? [y]es, [n]o, [A]ll, [N]one, [r]ename:  NULL
(EOF or read error, treating as "[N]one" ...)
|                                                                              |
+---+---------------------------------------------------------------+----------+
| x | BITRISE - install ndk (exit code: 1)                          | 129 sec  |
+---+---------------------------------------------------------------+----------+
| Issue tracker: https://github.com/bitrise-io/steps-script/issues             |
| Source: https://github.com/bitrise-io/steps-script                           |
+---+---------------------------------------------------------------+----------+

Any hints on what is failing and how to fix it? Thank you!

Hi @rafaaddison,
Could you send us the build’s URL so we can have a look at the logs?

It seems that you are either calling that unzip twice or there is android-ndk-r10e produced in some other way.

If it is intended that you have an existing android-ndk-r10e directory and want to unzip NDK there you can just add o option to unizip. So it will be unzip -qo android-ndk-${ANDROID_NDK_VERSION}-linux-x86_64.zip.
However, the proper fix seems to be unzipping only once.

Thanks for the reply. Indeed there seems to be already an existing android-ndk-r10e directory.

I ended up using echo "A" | unzip -q android-ndk-r10e-linux-x86_64.zip Which takes the A as the response to the prompt question.

As a side note I needed also to use the -darwin version instead of the linux one.

1 Like

I also made a step that can handle both Linux and Mac: https://github.com/viktorbenei/bitrise-step-install-android-ndk-version

As it’s not shared in the StepLib (we probably will include this logic in another step and not have a separate one just for the NDK install) you can include it with the git:: reference style in your bitrise.yml:

    - git::https://github.com/viktorbenei/bitrise-step-install-android-ndk-version.git@master:
        inputs:
        - android_ndk_version: r12b

Once added to the bitrise.yml you can use the UI to change the version, or just change the version in the YML (in this example above it’ll install r12b).

1 Like

Thank you very much.
Using install-missing-android-tools@2.3.5 and specifying r19b as NDK version, it failed with following error:

Ensure required Android SDK components

Retrying...

Failed to ensure android components, error: output: > Configure project :app

NDK is missing a "platforms" directory.

But after I added they install ndk step from Viktor, everything works fine.

Thanks for this “custom” step.

Regards

1 Like