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