Skip to content

Instantly share code, notes, and snippets.

@jorgenpt
Last active February 21, 2023 06:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jorgenpt/1961404 to your computer and use it in GitHub Desktop.
Save jorgenpt/1961404 to your computer and use it in GitHub Desktop.
Shell script to check for Android NDK version.
#!/bin/bash
# Bash script to assert that the current version of the NDK is at least the
# specified version. Prints 'true' to standard out if it's the right version,
# 'false' if it's not.
#
# Typically used like this, in your jni/Android.mk:
#
# ifneq ($(shell $(LOCAL_PATH)/assert_ndk_version.sh "r5c"),true)
# $(error NDK version r5c or greater required)
# endif
#
# See https://gist.github.com/2878774 for asserting SDK version.
#
# Copyright 2012, Lookout, Inc. <jtjerno@mylookout.com>
# Licensed under the BSD license. See the zLICENSE file for information.
#
# Includes contributions from alexs.mac@gmail.com (Alex Stewart)
# Extracts 'r5c' into '5 c', also handles newer versions of the form
# 'r9d (64-bit)' and versions >= 10.
function get_major_minor() {
# r9d (64-bit) -> '9d', also handle versions >= 10.
local version=$(echo "$1" | sed 's/r\([0-9]\{1,2\}[a-z]\).*/\1/')
local major=$(echo "$version" | sed 's/\([0-9]\{1,2\}\).*/\1/')
local minor=$(echo "$version" | sed 's/^[0-9]*//')
echo "$major $minor"
}
if [[ -z "$1" ]]; then
echo "Usage: $0 <required version>" >&2
echo " For example: $0 r5c" >&2
exit 1
fi
# Assert that the expected version is at least 4.
declare -a expected_version
expected_version=( $(get_major_minor "$1") )
if [[ ${expected_version[0]} -le 4 ]]; then
echo "Cannot test for versions less than r5: r4 doesn't have a version file." >&2
echo false
exit 1
fi
if [[ ! -d "$ANDROID_NDK_ROOT" ]]; then
# Attempt to find ndk-build on the path.
ANDROID_NDK_ROOT=$(dirname $(which ndk-build))
if [ ! -s "$ANDROID_NDK_ROOT" ]; then
echo "Failed to find either ANDROID_NDK_ROOT or ndk-build."
echo false
exit 1
fi
fi
release_file="$ANDROID_NDK_ROOT/RELEASE.TXT"
# NDK version r4 or earlier doesn't have a RELEASE.txt, and we just asserted
# that the person was looking for r5 or above, so that implies that this is an
# invalid version.
if [ ! -s "$release_file" ]; then
echo false
exit 0
fi
# Make sure the data is at least kinda sane.
version=$(grep '^r' $release_file)
declare -a actual_version
actual_version=( $(get_major_minor "$version") )
if [ -z "$version" ] || [ -z "${actual_version[0]}" ]; then
echo "Invalid RELEASE.txt: $(cat $release_file)" >&2
echo false
exit 1
fi
if [[ ${actual_version[0]} -lt ${expected_version[0]} ]]; then
echo "false"
elif [[ ${actual_version[0]} -eq ${expected_version[0]} ]]; then
# This uses < and not -lt because they're string identifiers (a, b, c, etc)
if [[ "${actual_version[1]}" < "${expected_version[1]}" ]]; then
echo "false"
else
echo "true"
fi
else
echo "true"
fi
Copyright (c) 2012, Lookout, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@docgravel
Copy link

If $ANDROID_NDK_ROOT is undefined, you get an unclear error (it assumes you're using older than r4 since it can't find RELEASE.TXT)

@jorgenpt
Copy link
Author

Thanks, updated script.

@jorgenpt
Copy link
Author

jorgenpt commented Jun 4, 2014

Updated the script to support the newer version newer format (architecture included in version, and versions >= 10.) Also released it under BSD to allow inclusion in the Ceres Solver project.

Thanks to Alex Stewart of the Ceres Solver project for the bug fix. (https://ceres-solver.googlesource.com/ceres-solver/+/52bad6223bf41598804387ad073e55c672c8d294)

@jorgenpt
Copy link
Author

jorgenpt commented Jun 4, 2014

Also include Alex Stewart's improvement to look for ndk-build in $PATH if $ANDROID_NDK_ROOT is not set.

@philip-lamb
Copy link

As of NDK r11, RELEASE.TXT is gone, and NDK version appears to be embedded in $ANDROID_NDK_ROOT/source.properties under key 'Pkg.Revision'.

@agnostic-apollo
Copy link

agnostic-apollo commented Aug 1, 2019

Hi, @jorgenpt,
I added support for NDK version greater than or equal to 11 for your script. Also created a get_ndk_version to get full/major/minor/build version of the NDK. You can check it out here. Released under the same license as yours and gave you credits as well.
Thanks for the original script.

@jorgenpt
Copy link
Author

jorgenpt commented Aug 4, 2019

@agnostic-apollo, that's great! I haven't been using this for a few years, glad to hear it's still useful 👍

@agnostic-apollo
Copy link

yeah finding out the ndk version was really necessary to generate standalone toolchain, compiler and bintool prefixes depending on arch... It was a mess, but got it working for any ndk > 10e and <=20 atleast. The link for that project is mentioned in the repo if anyone interested...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment