build-scripts/androidbuild.sh
author Sam Lantinga <slouken@libsdl.org>
Mon, 23 Oct 2017 23:23:47 -0700
changeset 11649 fe6769530709
parent 11647 5816b27073a4
permissions -rwxr-xr-x
Updated Android build tools version, which bumped minimum deployment target to API 14
Also added native code to the Android gradle project, which allows using gradle or Android Studio to build the entire SDL application without a separate ndk-build step.
gabomdq@7793
     1
#!/bin/bash
gabomdq@7793
     2
gabomdq@7793
     3
SOURCES=()
gabomdq@7793
     4
MKSOURCES=""
gabomdq@7793
     5
CURDIR=`pwd -P`
gabomdq@7793
     6
gabomdq@7793
     7
# Fetch sources
gabomdq@7793
     8
if [[ $# -ge 2 ]]; then
gabomdq@7793
     9
    for src in ${@:2}
gabomdq@7793
    10
    do
gabomdq@7793
    11
        SOURCES+=($src)
gabomdq@7793
    12
        MKSOURCES="$MKSOURCES $(basename $src)"
gabomdq@7793
    13
    done
gabomdq@7793
    14
else
gabomdq@7793
    15
    if [ -n "$1" ]; then
gabomdq@7793
    16
        while read src
gabomdq@7793
    17
        do
gabomdq@7793
    18
            SOURCES+=($src)
gabomdq@7793
    19
            MKSOURCES="$MKSOURCES $(basename $src)"
gabomdq@7793
    20
        done
gabomdq@7793
    21
    fi
gabomdq@7793
    22
fi
gabomdq@7793
    23
gabomdq@7793
    24
if [ -z "$1" ] || [ -z "$SOURCES" ]; then
gabomdq@7793
    25
    echo "Usage: androidbuild.sh com.yourcompany.yourapp < sources.list"
gabomdq@7793
    26
    echo "Usage: androidbuild.sh com.yourcompany.yourapp source1.c source2.c ...sourceN.c"
gabomdq@7907
    27
    echo "To copy SDL source instead of symlinking: COPYSOURCE=1 androidbuild.sh ... "
gabomdq@7793
    28
    exit 1
gabomdq@7793
    29
fi
gabomdq@7793
    30
gabomdq@7793
    31
SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
gabomdq@7793
    32
slouken@11647
    33
if [ -z "$ANDROID_HOME" ];then
slouken@11647
    34
    echo "Please set the ANDROID_HOME directory to the path of the Android SDK"
slouken@11647
    35
    exit 1
slouken@11647
    36
fi
slouken@11647
    37
slouken@11649
    38
if [ ! -d "$ANDROID_HOME/ndk-bundle" -a -z "$ANDROID_NDK_HOME" ]; then
slouken@11649
    39
    echo "Please set the ANDROID_NDK_HOME directory to the path of the Android NDK"
gabomdq@7793
    40
    exit 1
gabomdq@7793
    41
fi
gabomdq@7793
    42
gabomdq@7793
    43
APP="$1"
gabomdq@7793
    44
APPARR=(${APP//./ })
gabomdq@7793
    45
BUILDPATH="$SDLPATH/build/$APP"
gabomdq@7793
    46
gabomdq@7793
    47
# Start Building
gabomdq@7793
    48
gabomdq@7793
    49
rm -rf $BUILDPATH
gabomdq@7793
    50
mkdir -p $BUILDPATH
gabomdq@7793
    51
gabomdq@7793
    52
cp -r $SDLPATH/android-project/* $BUILDPATH
gabomdq@7793
    53
gabomdq@7793
    54
# Copy SDL sources
slouken@11647
    55
mkdir -p $BUILDPATH/app/jni/SDL
gabomdq@7907
    56
if [ -z "$COPYSOURCE" ]; then
slouken@11647
    57
    ln -s $SDLPATH/src $BUILDPATH/app/jni/SDL
slouken@11647
    58
    ln -s $SDLPATH/include $BUILDPATH/app/jni/SDL
gabomdq@7907
    59
else
slouken@11647
    60
    cp -r $SDLPATH/src $BUILDPATH/app/jni/SDL
slouken@11647
    61
    cp -r $SDLPATH/include $BUILDPATH/app/jni/SDL
gabomdq@7907
    62
fi
gabomdq@7907
    63
slouken@11647
    64
cp -r $SDLPATH/Android.mk $BUILDPATH/app/jni/SDL
slouken@11647
    65
sed -i -e "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/app/jni/src/Android.mk
slouken@11647
    66
sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/build.gradle
slouken@11647
    67
sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/src/main/AndroidManifest.xml
gabomdq@7793
    68
gabomdq@7793
    69
# Copy user sources
gabomdq@7793
    70
for src in "${SOURCES[@]}"
gabomdq@7793
    71
do
slouken@11647
    72
    cp $src $BUILDPATH/app/jni/src
gabomdq@7793
    73
done
gabomdq@7793
    74
gabomdq@7793
    75
# Create an inherited Activity
slouken@11647
    76
cd $BUILDPATH/app/src/main/java
gabomdq@7793
    77
for folder in "${APPARR[@]}"
gabomdq@7793
    78
do
gabomdq@7793
    79
    mkdir -p $folder
gabomdq@7793
    80
    cd $folder
gabomdq@7793
    81
done
gabomdq@7793
    82
gabomdq@7793
    83
ACTIVITY="${folder}Activity"
slouken@11647
    84
sed -i -e "s|\"SDLActivity\"|\"$ACTIVITY\"|g" $BUILDPATH/app/src/main/AndroidManifest.xml
gabomdq@7793
    85
gabomdq@7793
    86
# Fill in a default Activity
slouken@11647
    87
cat >"$ACTIVITY.java" <<__EOF__
slouken@11647
    88
package $APP;
slouken@11647
    89
slouken@11647
    90
import org.libsdl.app.SDLActivity;
slouken@11647
    91
slouken@11647
    92
public class $ACTIVITY extends SDLActivity
slouken@11647
    93
{
slouken@11647
    94
}
slouken@11647
    95
__EOF__
gabomdq@7793
    96
gabomdq@7793
    97
# Update project and build
slouken@11649
    98
echo "To build and install to a device for testing, run the following:"
slouken@11649
    99
echo "cd $BUILDPATH"
slouken@11649
   100
echo "./gradlew installDebug"