build-scripts/androidbuild.sh
author Sam Lantinga <slouken@libsdl.org>
Mon, 23 Oct 2017 23:23:47 -0700
changeset 11649 fe6769530709
parent 11647 5816b27073a4
permissions -rwxr-xr-x
Updated Android build tools version, which bumped minimum deployment target to API 14
Also added native code to the Android gradle project, which allows using gradle or Android Studio to build the entire SDL application without a separate ndk-build step.
     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 SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
    32 
    33 if [ -z "$ANDROID_HOME" ];then
    34     echo "Please set the ANDROID_HOME directory to the path of the Android SDK"
    35     exit 1
    36 fi
    37 
    38 if [ ! -d "$ANDROID_HOME/ndk-bundle" -a -z "$ANDROID_NDK_HOME" ]; then
    39     echo "Please set the ANDROID_NDK_HOME directory to the path of the Android NDK"
    40     exit 1
    41 fi
    42 
    43 APP="$1"
    44 APPARR=(${APP//./ })
    45 BUILDPATH="$SDLPATH/build/$APP"
    46 
    47 # Start Building
    48 
    49 rm -rf $BUILDPATH
    50 mkdir -p $BUILDPATH
    51 
    52 cp -r $SDLPATH/android-project/* $BUILDPATH
    53 
    54 # Copy SDL sources
    55 mkdir -p $BUILDPATH/app/jni/SDL
    56 if [ -z "$COPYSOURCE" ]; then
    57     ln -s $SDLPATH/src $BUILDPATH/app/jni/SDL
    58     ln -s $SDLPATH/include $BUILDPATH/app/jni/SDL
    59 else
    60     cp -r $SDLPATH/src $BUILDPATH/app/jni/SDL
    61     cp -r $SDLPATH/include $BUILDPATH/app/jni/SDL
    62 fi
    63 
    64 cp -r $SDLPATH/Android.mk $BUILDPATH/app/jni/SDL
    65 sed -i -e "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/app/jni/src/Android.mk
    66 sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/build.gradle
    67 sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/src/main/AndroidManifest.xml
    68 
    69 # Copy user sources
    70 for src in "${SOURCES[@]}"
    71 do
    72     cp $src $BUILDPATH/app/jni/src
    73 done
    74 
    75 # Create an inherited Activity
    76 cd $BUILDPATH/app/src/main/java
    77 for folder in "${APPARR[@]}"
    78 do
    79     mkdir -p $folder
    80     cd $folder
    81 done
    82 
    83 ACTIVITY="${folder}Activity"
    84 sed -i -e "s|\"SDLActivity\"|\"$ACTIVITY\"|g" $BUILDPATH/app/src/main/AndroidManifest.xml
    85 
    86 # Fill in a default Activity
    87 cat >"$ACTIVITY.java" <<__EOF__
    88 package $APP;
    89 
    90 import org.libsdl.app.SDLActivity;
    91 
    92 public class $ACTIVITY extends SDLActivity
    93 {
    94 }
    95 __EOF__
    96 
    97 # Update project and build
    98 echo "To build and install to a device for testing, run the following:"
    99 echo "cd $BUILDPATH"
   100 echo "./gradlew installDebug"