build-scripts/androidbuild.sh
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 05 Nov 2013 20:07:39 -0300
changeset 7907 24b4e98c6010
parent 7793 6463a850229d
child 7974 8af2b2344e9e
permissions -rwxr-xr-x
Adds Joystick support for Android

This bumps the build SDK level to 12 (up from 10). Runtime requirements remain
the same (at API level < 12 joystick support is disabled).

Also enables building SDL for armv7 and x86.
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
gabomdq@7793
    32
gabomdq@7793
    33
SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
gabomdq@7793
    34
gabomdq@7793
    35
NDKBUILD=`which ndk-build`
gabomdq@7793
    36
if [ -z "$NDKBUILD" ];then
gabomdq@7793
    37
    echo "Could not find the ndk-build utility, install Android's NDK and add it to the path"
gabomdq@7793
    38
    exit 1
gabomdq@7793
    39
fi
gabomdq@7793
    40
gabomdq@7793
    41
ANDROID=`which android`
gabomdq@7793
    42
if [ -z "$ANDROID" ];then
gabomdq@7793
    43
    echo "Could not find the android utility, install Android's SDK and add it to the path"
gabomdq@7793
    44
    exit 1
gabomdq@7793
    45
fi
gabomdq@7793
    46
gabomdq@7793
    47
ANT=`which ant`
gabomdq@7793
    48
gabomdq@7793
    49
if [ -z "$ANT" ];then
gabomdq@7793
    50
    echo "Could not find the ant utility, install Android's SDK and add it to the path"
gabomdq@7793
    51
    exit 1
gabomdq@7793
    52
fi
gabomdq@7793
    53
gabomdq@7793
    54
APP="$1"
gabomdq@7793
    55
APPARR=(${APP//./ })
gabomdq@7793
    56
BUILDPATH="$SDLPATH/build/$APP"
gabomdq@7793
    57
gabomdq@7793
    58
# Start Building
gabomdq@7793
    59
gabomdq@7793
    60
rm -rf $BUILDPATH
gabomdq@7793
    61
mkdir -p $BUILDPATH
gabomdq@7793
    62
gabomdq@7793
    63
cp -r $SDLPATH/android-project/* $BUILDPATH
gabomdq@7793
    64
gabomdq@7793
    65
# Copy SDL sources
gabomdq@7793
    66
mkdir -p $BUILDPATH/jni/SDL
gabomdq@7907
    67
if [ -z "$COPYSOURCE" ]; then
gabomdq@7907
    68
    ln -s $SDLPATH/src $BUILDPATH/jni/SDL
gabomdq@7907
    69
    ln -s $SDLPATH/include $BUILDPATH/jni/SDL
gabomdq@7907
    70
else
gabomdq@7907
    71
    cp -r $SDLPATH/src $BUILDPATH/jni/SDL
gabomdq@7907
    72
    cp -r $SDLPATH/include $BUILDPATH/jni/SDL
gabomdq@7907
    73
fi
gabomdq@7907
    74
gabomdq@7907
    75
cp -r $SDLPATH/Android.mk $BUILDPATH/jni/SDL
gabomdq@7793
    76
sed -i "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/jni/src/Android.mk
gabomdq@7793
    77
sed -i "s|org\.libsdl\.app|$APP|g" $BUILDPATH/AndroidManifest.xml
gabomdq@7793
    78
gabomdq@7793
    79
# Copy user sources
gabomdq@7793
    80
for src in "${SOURCES[@]}"
gabomdq@7793
    81
do
gabomdq@7793
    82
    cp $src $BUILDPATH/jni/src
gabomdq@7793
    83
done
gabomdq@7793
    84
gabomdq@7793
    85
# Create an inherited Activity
gabomdq@7793
    86
cd $BUILDPATH/src
gabomdq@7793
    87
for folder in "${APPARR[@]}"
gabomdq@7793
    88
do
gabomdq@7793
    89
    mkdir -p $folder
gabomdq@7793
    90
    cd $folder
gabomdq@7793
    91
done
gabomdq@7793
    92
gabomdq@7793
    93
ACTIVITY="${folder}Activity"
gabomdq@7793
    94
sed -i "s|SDLActivity|$ACTIVITY|g" $BUILDPATH/AndroidManifest.xml
gabomdq@7793
    95
sed -i "s|SDLActivity|$APP|g" $BUILDPATH/build.xml
gabomdq@7793
    96
gabomdq@7793
    97
# Fill in a default Activity
gabomdq@7793
    98
echo "package $APP;" >  "$ACTIVITY.java"
gabomdq@7793
    99
echo "import org.libsdl.app.SDLActivity;" >> "$ACTIVITY.java"
gabomdq@7793
   100
echo "public class $ACTIVITY extends SDLActivity {}" >> "$ACTIVITY.java"
gabomdq@7793
   101
gabomdq@7793
   102
# Update project and build
gabomdq@7793
   103
cd $BUILDPATH
gabomdq@7793
   104
android update project --path $BUILDPATH
gabomdq@7793
   105
$NDKBUILD
gabomdq@7793
   106
$ANT debug
gabomdq@7793
   107
gabomdq@7793
   108
cd $CURDIR
gabomdq@7793
   109
gabomdq@7793
   110
APK="$BUILDPATH/bin/$APP-debug.apk"
gabomdq@7793
   111
gabomdq@7793
   112
if [ -f "$APK" ]; then
gabomdq@7793
   113
    echo "Your APK is ready at $APK"
gabomdq@7793
   114
    echo "To install to your device: "
gabomdq@7793
   115
    echo "cd  $BUILDPATH"
gabomdq@7793
   116
    echo "ant debug install"
gabomdq@7793
   117
    exit 0
gabomdq@7793
   118
fi
gabomdq@7793
   119
gabomdq@7793
   120
echo "There was an error building the APK"
gabomdq@7793
   121
exit 1