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