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