build-scripts/androidbuild.sh
author Gabriel Jacobo <gabomdq@gmail.com>
Wed, 13 Nov 2013 09:41:21 -0300
changeset 7974 8af2b2344e9e
parent 7907 24b4e98c6010
child 9823 9fb8a612dd7c
permissions -rwxr-xr-x
Updated androidbuild.sh to enable parallel builds and ndk-build parameters
     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     echo "You can pass additional arguments to ndk-build with the NDKARGS variable: NDKARGS=\"-s\" androidbuild.sh ..."
    29     exit 1
    30 fi
    31 
    32 
    33 
    34 SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
    35 
    36 NDKBUILD=`which ndk-build`
    37 if [ -z "$NDKBUILD" ];then
    38     echo "Could not find the ndk-build utility, install Android's NDK and add it to the path"
    39     exit 1
    40 fi
    41 
    42 ANDROID=`which android`
    43 if [ -z "$ANDROID" ];then
    44     echo "Could not find the android utility, install Android's SDK and add it to the path"
    45     exit 1
    46 fi
    47 
    48 ANT=`which ant`
    49 
    50 if [ -z "$ANT" ];then
    51     echo "Could not find the ant utility, install Android's SDK and add it to the path"
    52     exit 1
    53 fi
    54 
    55 NCPUS="1"
    56 case "$OSTYPE" in
    57     darwin*)
    58         NCPU=`sysctl -n hw.ncpu`
    59         ;; 
    60     linux*)
    61         if [ -n `which nproc` ]; then
    62             NCPUS=`nproc`
    63         fi  
    64         ;;
    65   *);;
    66 esac
    67 
    68 APP="$1"
    69 APPARR=(${APP//./ })
    70 BUILDPATH="$SDLPATH/build/$APP"
    71 
    72 # Start Building
    73 
    74 rm -rf $BUILDPATH
    75 mkdir -p $BUILDPATH
    76 
    77 cp -r $SDLPATH/android-project/* $BUILDPATH
    78 
    79 # Copy SDL sources
    80 mkdir -p $BUILDPATH/jni/SDL
    81 if [ -z "$COPYSOURCE" ]; then
    82     ln -s $SDLPATH/src $BUILDPATH/jni/SDL
    83     ln -s $SDLPATH/include $BUILDPATH/jni/SDL
    84 else
    85     cp -r $SDLPATH/src $BUILDPATH/jni/SDL
    86     cp -r $SDLPATH/include $BUILDPATH/jni/SDL
    87 fi
    88 
    89 cp -r $SDLPATH/Android.mk $BUILDPATH/jni/SDL
    90 sed -i "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/jni/src/Android.mk
    91 sed -i "s|org\.libsdl\.app|$APP|g" $BUILDPATH/AndroidManifest.xml
    92 
    93 # Copy user sources
    94 for src in "${SOURCES[@]}"
    95 do
    96     cp $src $BUILDPATH/jni/src
    97 done
    98 
    99 # Create an inherited Activity
   100 cd $BUILDPATH/src
   101 for folder in "${APPARR[@]}"
   102 do
   103     mkdir -p $folder
   104     cd $folder
   105 done
   106 
   107 ACTIVITY="${folder}Activity"
   108 sed -i "s|SDLActivity|$ACTIVITY|g" $BUILDPATH/AndroidManifest.xml
   109 sed -i "s|SDLActivity|$APP|g" $BUILDPATH/build.xml
   110 
   111 # Fill in a default Activity
   112 echo "package $APP;" >  "$ACTIVITY.java"
   113 echo "import org.libsdl.app.SDLActivity;" >> "$ACTIVITY.java"
   114 echo "public class $ACTIVITY extends SDLActivity {}" >> "$ACTIVITY.java"
   115 
   116 # Update project and build
   117 cd $BUILDPATH
   118 android update project --path $BUILDPATH
   119 $NDKBUILD -j $NCPUS $NDKARGS
   120 $ANT debug
   121 
   122 cd $CURDIR
   123 
   124 APK="$BUILDPATH/bin/$APP-debug.apk"
   125 
   126 if [ -f "$APK" ]; then
   127     echo "Your APK is ready at $APK"
   128     echo "To install to your device: "
   129     echo "cd  $BUILDPATH"
   130     echo "ant debug install"
   131     exit 0
   132 fi
   133 
   134 echo "There was an error building the APK"
   135 exit 1