Skip to content

Instantly share code, notes, and snippets.

@tonY1883
Last active February 1, 2019 05:37
Show Gist options
  • Save tonY1883/a338e012a59d1cc07051bf0ac28e4dae to your computer and use it in GitHub Desktop.
Save tonY1883/a338e012a59d1cc07051bf0ac28e4dae to your computer and use it in GitHub Desktop.
A script for building OpenCV4Android SDK painlessly.
#!/bin/sh
OPENCV_VERSION='3.4.4'
WITH_CONTRIB=true
NDK_VERSION='17'
SDK_TOOLS_VERSION='25.2.5'
BUILD_TOOLS_VERSION='25.0.1'
PLATFORM_TOOLS_VERSION='21'
ANDROID_VERSION='21'
echo "==============================================="
echo " OpenCV Android SDK Builder"
echo "==============================================="
sleep 1
echo "OpenCV version to be built: $OPENCV_VERSION"
echo "Build with contrib modules: $WITH_CONTRIB"
echo "Build with NDK: $NDK_VERSION"
echo "Build with SDK Tools: $SDK_TOOLS_VERSION"
echo "Build with Build Tools: $BUILD_TOOLS_VERSION"
echo "Build with Platform Tools: $PLATFORM_TOOLS_VERSION"
echo "Build with API Level: $ANDROID_VERSION"
echo "==============================================="
mkdir OpenCV4Android
cd OpenCV4Android
sleep 5
echo "Preparing system..."
sleep 3
sudo apt-get -y update
sleep 3
echo "Checking dependencies..."
sleep 3
sudo apt-get install -y build-essential cmake
sudo apt-get install -y python-dev python-tk python-numpy python3-dev python3-tk python3-numpy
sudo apt-get install -y ant openjdk-8-jdk
sudo apt-get install -y ccache ninja-build
sudo apt-get install -y unzip wget
sleep 3
echo "Downloading OpenCV source..."
sleep 3
wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip
unzip ${OPENCV_VERSION}.zip
rm ${OPENCV_VERSION}.zip
mv opencv-${OPENCV_VERSION} OpenCV
if [ "$WITH_CONTRIB" = true ] ; then
wget https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip
unzip ${OPENCV_VERSION}.zip
rm ${OPENCV_VERSION}.zip
mv opencv_contrib-${OPENCV_VERSION} contrib
mv contrib OpenCV
fi
sleep 3
echo "Downloading Android SDK..."
sleep 3
mkdir /tmp/android-sdk
wget http://dl-ssl.google.com/android/repository/tools_r${SDK_TOOLS_VERSION}-linux.zip
unzip tools_r${SDK_TOOLS_VERSION}-linux.zip
rm tools_r${SDK_TOOLS_VERSION}-linux.zip
mv tools /tmp/android-sdk
wget http://dl-ssl.google.com/android/repository/build-tools_r${BUILD_TOOLS_VERSION}-linux.zip
unzip build-tools_r${BUILD_TOOLS_VERSION}-linux.zip
rm build-tools_r${BUILD_TOOLS_VERSION}-linux.zip
mv android-* ${BUILD_TOOLS_VERSION}
mkdir /tmp/android-sdk/build-tools
mv ${BUILD_TOOLS_VERSION} /tmp/android-sdk/build-tools
wget https://dl-ssl.google.com/android/repository/android-ndk-r${NDK_VERSION}b-linux-x86_64.zip
unzip android-ndk-r${NDK_VERSION}b-linux-x86_64.zip
mv android-ndk-r${NDK_VERSION}b android-ndk
mv android-ndk /tmp
wget https://dl-ssl.google.com/android/repository/android-${ANDROID_VERSION}_r01.zip
mkdir /tmp/android-sdk/platforms
unzip android-${ANDROID_VERSION}_r01.zip -d /tmp/android-sdk/platforms
rm android-${ANDROID_VERSION}_r01.zip
wget https://dl.google.com/android/repository/platform-tools_r${PLATFORM_TOOLS_VERSION}-linux.zip
unzip platform-tools_r${PLATFORM_TOOLS_VERSION}-linux.zip
mv platform-tools /tmp/android-sdk
rm platform-tools_r${PLATFORM_TOOLS_VERSION}-linux.zip
sleep 3
echo "Preparing for build..."
sleep 3
sudo update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
export ANDROID_SDK=/tmp/android-sdk
export ANDROID_NDK=/tmp/android-ndk
export ANDROID_SDK_ROOT=/tmp/android-sdk
PATH=/tmp/android-sdk/tools:/tmp/android-sdk/platform-tools:${PATH}
export PATH
sleep 3
echo "Building OpenCV4Android..."
sleep 3
if [ "$WITH_CONTRIB" = true ] ; then
python OpenCV/platforms/android/build_sdk.py --extra_modules_path=OpenCV/contrib/modules --ndk_path=/tmp/android-ndk --sdk_path=/tmp/android-sdk --config=ndk-17.config.py build OpenCV
else
python OpenCV/platforms/android/build_sdk.py --ndk_path=/tmp/android-ndk --sdk_path=/tmp/android-sdk --config=ndk-${NDK_VERSION}.config.py build OpenCV
fi
sleep 3
echo "Cleaning up..."
sleep 3
echo 0 | sudo update-alternatives --config java
sleep 3
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment