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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7793
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     1
#!/bin/bash
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     2
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     3
SOURCES=()
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     4
MKSOURCES=""
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     5
CURDIR=`pwd -P`
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     6
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     7
# Fetch sources
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     8
if [[ $# -ge 2 ]]; then
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     9
    for src in ${@:2}
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    10
    do
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    11
        SOURCES+=($src)
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    12
        MKSOURCES="$MKSOURCES $(basename $src)"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    13
    done
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    14
else
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    15
    if [ -n "$1" ]; then
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    16
        while read src
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    17
        do
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    18
            SOURCES+=($src)
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    19
            MKSOURCES="$MKSOURCES $(basename $src)"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    20
        done
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    21
    fi
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    22
fi
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    23
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    24
if [ -z "$1" ] || [ -z "$SOURCES" ]; then
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    25
    echo "Usage: androidbuild.sh com.yourcompany.yourapp < sources.list"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    26
    echo "Usage: androidbuild.sh com.yourcompany.yourapp source1.c source2.c ...sourceN.c"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    27
    exit 1
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    28
fi
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    29
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    30
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    31
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    32
SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    33
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    34
NDKBUILD=`which ndk-build`
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    35
if [ -z "$NDKBUILD" ];then
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    36
    echo "Could not find the ndk-build utility, install Android's NDK and add it to the path"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    37
    exit 1
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    38
fi
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    39
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    40
ANDROID=`which android`
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    41
if [ -z "$ANDROID" ];then
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    42
    echo "Could not find the android utility, install Android's SDK and add it to the path"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    43
    exit 1
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    44
fi
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    45
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    46
ANT=`which ant`
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    47
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    48
if [ -z "$ANT" ];then
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    49
    echo "Could not find the ant utility, install Android's SDK and add it to the path"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    50
    exit 1
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    51
fi
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    52
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    53
APP="$1"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    54
APPARR=(${APP//./ })
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    55
BUILDPATH="$SDLPATH/build/$APP"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    56
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    57
# Start Building
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    58
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    59
rm -rf $BUILDPATH
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    60
mkdir -p $BUILDPATH
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    61
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    62
cp -r $SDLPATH/android-project/* $BUILDPATH
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    63
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    64
# Copy SDL sources
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    65
mkdir -p $BUILDPATH/jni/SDL
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    66
cp -r $SDLPATH/src $BUILDPATH/jni/SDL
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    67
cp -r $SDLPATH/include $BUILDPATH/jni/SDL
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    68
cp $SDLPATH/Android.mk $BUILDPATH/jni/SDL
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    69
sed -i "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/jni/src/Android.mk
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    70
sed -i "s|org\.libsdl\.app|$APP|g" $BUILDPATH/AndroidManifest.xml
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    71
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    72
# Copy user sources
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    73
for src in "${SOURCES[@]}"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    74
do
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    75
    cp $src $BUILDPATH/jni/src
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    76
done
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    77
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    78
# Create an inherited Activity
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    79
cd $BUILDPATH/src
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    80
for folder in "${APPARR[@]}"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    81
do
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    82
    mkdir -p $folder
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    83
    cd $folder
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    84
done
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    85
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    86
ACTIVITY="${folder}Activity"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    87
sed -i "s|SDLActivity|$ACTIVITY|g" $BUILDPATH/AndroidManifest.xml
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    88
sed -i "s|SDLActivity|$APP|g" $BUILDPATH/build.xml
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    89
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    90
# Fill in a default Activity
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    91
echo "package $APP;" >  "$ACTIVITY.java"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    92
echo "import org.libsdl.app.SDLActivity;" >> "$ACTIVITY.java"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    93
echo "public class $ACTIVITY extends SDLActivity {}" >> "$ACTIVITY.java"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    94
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    95
# Update project and build
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    96
cd $BUILDPATH
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    97
android update project --path $BUILDPATH
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    98
$NDKBUILD
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    99
$ANT debug
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   100
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   101
cd $CURDIR
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   102
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   103
APK="$BUILDPATH/bin/$APP-debug.apk"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   104
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   105
if [ -f "$APK" ]; then
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   106
    echo "Your APK is ready at $APK"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   107
    echo "To install to your device: "
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   108
    echo "cd  $BUILDPATH"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   109
    echo "ant debug install"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   110
    exit 0
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   111
fi
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   112
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   113
echo "There was an error building the APK"
6463a850229d Adds test framework to Android.mk and a simple utility to build Android projects
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   114
exit 1