Skip to content

Latest commit

 

History

History
executable file
·
144 lines (116 loc) · 3.17 KB

androidbuild.sh

File metadata and controls

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