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
gabomdq@7793
     1
#!/bin/bash
gabomdq@7793
     2
gabomdq@7793
     3
SOURCES=()
gabomdq@7793
     4
MKSOURCES=""
gabomdq@7793
     5
CURDIR=`pwd -P`
gabomdq@7793
     6
gabomdq@7793
     7
# Fetch sources
gabomdq@7793
     8
if [[ $# -ge 2 ]]; then
gabomdq@7793
     9
    for src in ${@:2}
gabomdq@7793
    10
    do
gabomdq@7793
    11
        SOURCES+=($src)
gabomdq@7793
    12
        MKSOURCES="$MKSOURCES $(basename $src)"
gabomdq@7793
    13
    done
gabomdq@7793
    14
else
gabomdq@7793
    15
    if [ -n "$1" ]; then
gabomdq@7793
    16
        while read src
gabomdq@7793
    17
        do
gabomdq@7793
    18
            SOURCES+=($src)
gabomdq@7793
    19
            MKSOURCES="$MKSOURCES $(basename $src)"
gabomdq@7793
    20
        done
gabomdq@7793
    21
    fi
gabomdq@7793
    22
fi
gabomdq@7793
    23
gabomdq@7793
    24
if [ -z "$1" ] || [ -z "$SOURCES" ]; then
gabomdq@7793
    25
    echo "Usage: androidbuild.sh com.yourcompany.yourapp < sources.list"
gabomdq@7793
    26
    echo "Usage: androidbuild.sh com.yourcompany.yourapp source1.c source2.c ...sourceN.c"
gabomdq@7907
    27
    echo "To copy SDL source instead of symlinking: COPYSOURCE=1 androidbuild.sh ... "
gabomdq@7974
    28
    echo "You can pass additional arguments to ndk-build with the NDKARGS variable: NDKARGS=\"-s\" androidbuild.sh ..."
gabomdq@7793
    29
    exit 1
gabomdq@7793
    30
fi
gabomdq@7793
    31
gabomdq@7793
    32
gabomdq@7793
    33
gabomdq@7793
    34
SDLPATH="$( cd "$(dirname "$0")/.." ; pwd -P )"
gabomdq@7793
    35
slouken@11647
    36
if [ -z "$ANDROID_HOME" ];then
slouken@11647
    37
    echo "Please set the ANDROID_HOME directory to the path of the Android SDK"
slouken@11647
    38
    exit 1
slouken@11647
    39
fi
slouken@11647
    40
gabomdq@7793
    41
NDKBUILD=`which ndk-build`
gabomdq@7793
    42
if [ -z "$NDKBUILD" ];then
gabomdq@7793
    43
    echo "Could not find the ndk-build utility, install Android's NDK and add it to the path"
gabomdq@7793
    44
    exit 1
gabomdq@7793
    45
fi
gabomdq@7793
    46
slouken@11647
    47
ANDROID="$ANDROID_HOME/tools/android"
slouken@11647
    48
if [ ! -f "$ANDROID" ]; then
slouken@11647
    49
    ANDROID=`which android`
slouken@11647
    50
fi
gabomdq@7793
    51
if [ -z "$ANDROID" ];then
gabomdq@7793
    52
    echo "Could not find the android utility, install Android's SDK and add it to the path"
gabomdq@7793
    53
    exit 1
gabomdq@7793
    54
fi
gabomdq@7793
    55
gabomdq@7974
    56
NCPUS="1"
gabomdq@7974
    57
case "$OSTYPE" in
gabomdq@7974
    58
    darwin*)
gabomdq@7974
    59
        NCPU=`sysctl -n hw.ncpu`
gabomdq@7974
    60
        ;; 
gabomdq@7974
    61
    linux*)
gabomdq@7974
    62
        if [ -n `which nproc` ]; then
gabomdq@7974
    63
            NCPUS=`nproc`
gabomdq@7974
    64
        fi  
gabomdq@7974
    65
        ;;
gabomdq@7974
    66
  *);;
gabomdq@7974
    67
esac
gabomdq@7974
    68
gabomdq@7793
    69
APP="$1"
gabomdq@7793
    70
APPARR=(${APP//./ })
gabomdq@7793
    71
BUILDPATH="$SDLPATH/build/$APP"
gabomdq@7793
    72
gabomdq@7793
    73
# Start Building
gabomdq@7793
    74
gabomdq@7793
    75
rm -rf $BUILDPATH
gabomdq@7793
    76
mkdir -p $BUILDPATH
gabomdq@7793
    77
gabomdq@7793
    78
cp -r $SDLPATH/android-project/* $BUILDPATH
gabomdq@7793
    79
gabomdq@7793
    80
# Copy SDL sources
slouken@11647
    81
mkdir -p $BUILDPATH/app/jni/SDL
gabomdq@7907
    82
if [ -z "$COPYSOURCE" ]; then
slouken@11647
    83
    ln -s $SDLPATH/src $BUILDPATH/app/jni/SDL
slouken@11647
    84
    ln -s $SDLPATH/include $BUILDPATH/app/jni/SDL
gabomdq@7907
    85
else
slouken@11647
    86
    cp -r $SDLPATH/src $BUILDPATH/app/jni/SDL
slouken@11647
    87
    cp -r $SDLPATH/include $BUILDPATH/app/jni/SDL
gabomdq@7907
    88
fi
gabomdq@7907
    89
slouken@11647
    90
cp -r $SDLPATH/Android.mk $BUILDPATH/app/jni/SDL
slouken@11647
    91
sed -i -e "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/app/jni/src/Android.mk
slouken@11647
    92
sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/build.gradle
slouken@11647
    93
sed -i -e "s|org\.libsdl\.app|$APP|g" $BUILDPATH/app/src/main/AndroidManifest.xml
gabomdq@7793
    94
gabomdq@7793
    95
# Copy user sources
gabomdq@7793
    96
for src in "${SOURCES[@]}"
gabomdq@7793
    97
do
slouken@11647
    98
    cp $src $BUILDPATH/app/jni/src
gabomdq@7793
    99
done
gabomdq@7793
   100
gabomdq@7793
   101
# Create an inherited Activity
slouken@11647
   102
cd $BUILDPATH/app/src/main/java
gabomdq@7793
   103
for folder in "${APPARR[@]}"
gabomdq@7793
   104
do
gabomdq@7793
   105
    mkdir -p $folder
gabomdq@7793
   106
    cd $folder
gabomdq@7793
   107
done
gabomdq@7793
   108
gabomdq@7793
   109
ACTIVITY="${folder}Activity"
slouken@11647
   110
sed -i -e "s|\"SDLActivity\"|\"$ACTIVITY\"|g" $BUILDPATH/app/src/main/AndroidManifest.xml
gabomdq@7793
   111
gabomdq@7793
   112
# Fill in a default Activity
slouken@11647
   113
cat >"$ACTIVITY.java" <<__EOF__
slouken@11647
   114
package $APP;
slouken@11647
   115
slouken@11647
   116
import org.libsdl.app.SDLActivity;
slouken@11647
   117
slouken@11647
   118
public class $ACTIVITY extends SDLActivity
slouken@11647
   119
{
slouken@11647
   120
}
slouken@11647
   121
__EOF__
gabomdq@7793
   122
gabomdq@7793
   123
# Update project and build
gabomdq@7793
   124
cd $BUILDPATH
slouken@11647
   125
pushd $BUILDPATH/app/jni
gabomdq@7974
   126
$NDKBUILD -j $NCPUS $NDKARGS
slouken@11647
   127
popd
slouken@11647
   128
slouken@11647
   129
# Start gradle build
slouken@11647
   130
$BUILDPATH/gradlew build
gabomdq@7793
   131
gabomdq@7793
   132
cd $CURDIR
gabomdq@7793
   133
slouken@11647
   134
APK="$BUILDPATH/app/build/outputs/apk/app-debug.apk"
gabomdq@7793
   135
gabomdq@7793
   136
if [ -f "$APK" ]; then
gabomdq@7793
   137
    echo "Your APK is ready at $APK"
gabomdq@7793
   138
    echo "To install to your device: "
slouken@11647
   139
    echo "$ANDROID_HOME/platform-tools/adb install -r $APK"
gabomdq@7793
   140
    exit 0
gabomdq@7793
   141
fi
gabomdq@7793
   142
gabomdq@7793
   143
echo "There was an error building the APK"
slouken@11647
   144
exit 1