build-scripts/androidbuild.sh
author Sam Lantinga
Mon, 23 Oct 2017 15:23:43 -0700
changeset 11647 5816b27073a4
parent 10200 c678dcec4f44
child 11649 fe6769530709
permissions -rwxr-xr-x
Switched to new style gradle Android application build process
     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 if [ -z "$ANDROID_HOME" ];then
    37     echo "Please set the ANDROID_HOME directory to the path of the Android SDK"
    38     exit 1
    39 fi
    40 
    41 NDKBUILD=`which ndk-build`
    42 if [ -z "$NDKBUILD" ];then
    43     echo "Could not find the ndk-build utility, install Android's NDK and add it to the path"
    44     exit 1
    45 fi
    46 
    47 ANDROID="$ANDROID_HOME/tools/android"
    48 if [ ! -f "$ANDROID" ]; then
    49     ANDROID=`which android`
    50 fi
    51 if [ -z "$ANDROID" ];then
    52     echo "Could not find the android utility, install Android's SDK and add it to the path"
    53     exit 1
    54 fi
    55 
    56 NCPUS="1"
    57 case "$OSTYPE" in
    58     darwin*)
    59         NCPU=`sysctl -n hw.ncpu`
    60         ;; 
    61     linux*)
    62         if [ -n `which nproc` ]; then
    63             NCPUS=`nproc`
    64         fi  
    65         ;;
    66   *);;
    67 esac
    68 
    69 APP="$1"
    70 APPARR=(${APP//./ })
    71 BUILDPATH="$SDLPATH/build/$APP"
    72 
    73 # Start Building
    74 
    75 rm -rf $BUILDPATH
    76 mkdir -p $BUILDPATH
    77 
    78 cp -r $SDLPATH/android-project/* $BUILDPATH
    79 
    80 # Copy SDL sources
    81 mkdir -p $BUILDPATH/app/jni/SDL
    82 if [ -z "$COPYSOURCE" ]; then
    83     ln -s $SDLPATH/src $BUILDPATH/app/jni/SDL
    84     ln -s $SDLPATH/include $BUILDPATH/app/jni/SDL
    85 else
    86     cp -r $SDLPATH/src $BUILDPATH/app/jni/SDL
    87     cp -r $SDLPATH/include $BUILDPATH/app/jni/SDL
    88 fi
    89 
    90 cp -r $SDLPATH/Android.mk $BUILDPATH/app/jni/SDL
    91 sed -i -e "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/app/jni/src/Android.mk
    92 sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/build.gradle
    93 sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/src/main/AndroidManifest.xml
    94 
    95 # Copy user sources
    96 for src in "${SOURCES[@]}"
    97 do
    98     cp $src $BUILDPATH/app/jni/src
    99 done
   100 
   101 # Create an inherited Activity
   102 cd $BUILDPATH/app/src/main/java
   103 for folder in "${APPARR[@]}"
   104 do
   105     mkdir -p $folder
   106     cd $folder
   107 done
   108 
   109 ACTIVITY="${folder}Activity"
   110 sed -i -e "s|\"SDLActivity\"|\"$ACTIVITY\"|g" $BUILDPATH/app/src/main/AndroidManifest.xml
   111 
   112 # Fill in a default Activity
   113 cat >"$ACTIVITY.java" <<__EOF__
   114 package $APP;
   115 
   116 import org.libsdl.app.SDLActivity;
   117 
   118 public class $ACTIVITY extends SDLActivity
   119 {
   120 }
   121 __EOF__
   122 
   123 # Update project and build
   124 cd $BUILDPATH
   125 pushd $BUILDPATH/app/jni
   126 $NDKBUILD -j $NCPUS $NDKARGS
   127 popd
   128 
   129 # Start gradle build
   130 $BUILDPATH/gradlew build
   131 
   132 cd $CURDIR
   133 
   134 APK="$BUILDPATH/app/build/outputs/apk/app-debug.apk"
   135 
   136 if [ -f "$APK" ]; then
   137     echo "Your APK is ready at $APK"
   138     echo "To install to your device: "
   139     echo "$ANDROID_HOME/platform-tools/adb install -r $APK"
   140     exit 0
   141 fi
   142 
   143 echo "There was an error building the APK"
   144 exit 1