From ad7751f952c64d530e4d13cd0b57fb0b9aa14914 Mon Sep 17 00:00:00 2001
From: Benau <Benau@users.noreply.github.com>
Date: Fri, 4 Dec 2020 10:04:44 +0800
Subject: [PATCH] Allow android 4.1 build

---
 android/Android.mk          | 18 ++++++++++++++++--
 android/AndroidManifest.xml |  4 ++--
 android/make.sh             | 26 +++++++++++++++++++-------
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/android/Android.mk b/android/Android.mk
index 3f9ba1830..0015323c9 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -137,6 +137,16 @@ include $(BUILD_STATIC_LIBRARY)
 include $(CLEAR_VARS)
 
 
+# Graphics engine
+LOCAL_MODULE       := graphics_engine
+LOCAL_PATH         := .
+LOCAL_CPP_FEATURES += rtti
+LOCAL_SRC_FILES    := $(wildcard ../lib/graphics_engine/src/*.c)
+LOCAL_CFLAGS       := -I../lib/graphics_engine/include
+include $(BUILD_STATIC_LIBRARY)
+include $(CLEAR_VARS)
+
+
 # MCPP
 LOCAL_MODULE       := mcpp
 LOCAL_PATH         := .
@@ -179,6 +189,7 @@ LOCAL_CFLAGS       := -I../lib/irrlicht/source/Irrlicht/ \
                       -Iobj/libpng/                      \
                       -Iobj/zlib/                        \
                       -I../lib/sdl2/include/             \
+                      -I../lib/graphics_engine/include   \
                       -DMOBILE_STK                       \
                       -DANDROID_PACKAGE_CALLBACK_NAME=$(PACKAGE_CALLBACK_NAME)
 LOCAL_CPPFLAGS     := -std=gnu++0x
@@ -209,6 +220,8 @@ LOCAL_SRC_FILES    := $(wildcard ../lib/sdl2/src/*.c) \
                       $(wildcard ../lib/sdl2/src/locale/android/*.c) \
                       $(wildcard ../lib/sdl2/src/locale/*.c) \
                       $(wildcard ../lib/sdl2/src/loadso/dlopen/*.c) \
+                      $(wildcard ../lib/sdl2/src/misc/*.c) \
+                      $(wildcard ../lib/sdl2/src/misc/android/*.c) \
                       $(wildcard ../lib/sdl2/src/power/*.c) \
                       $(wildcard ../lib/sdl2/src/power/android/*.c) \
                       $(wildcard ../lib/sdl2/src/filesystem/android/*.c) \
@@ -241,7 +254,7 @@ LOCAL_CPP_FEATURES += rtti exceptions
 LOCAL_SRC_FILES    := $(wildcard ../src/*.cpp)     \
                       $(wildcard ../src/*/*.cpp)   \
                       $(wildcard ../src/*/*/*.cpp)
-LOCAL_LDLIBS       := -llog -landroid -lGLESv1_CM -lGLESv3 -lOpenSLES -ldl -lm
+LOCAL_LDLIBS       := -llog -landroid -lGLESv1_CM -lGLESv2 -lOpenSLES -ldl -lm
 LOCAL_CFLAGS       := -I../lib/angelscript/include      \
                       -I../lib/bullet/src               \
                       -I../lib/sheenbidi/Headers        \
@@ -250,6 +263,7 @@ LOCAL_CFLAGS       := -I../lib/angelscript/include      \
                       -I../lib/irrlicht/include         \
                       -I../lib/irrlicht/source/Irrlicht \
                       -I../lib/graphics_utils           \
+                      -I../lib/graphics_engine/include  \
                       -I../lib/mcpp                     \
                       -I../lib/sdl2/include             \
                       -I../lib/tinygettext/include      \
@@ -277,7 +291,7 @@ LOCAL_CPPFLAGS     := -std=gnu++0x
 LOCAL_STATIC_LIBRARIES := irrlicht bullet enet ifaddrs angelscript mcpp SDL2 \
                           vorbisfile vorbis ogg openal curl libssl libcrypto \
                           c++_static sheenbidi harfbuzz freetype \
-                          tinygettext graphics_utils
+                          tinygettext graphics_utils graphics_engine
 
 include $(BUILD_SHARED_LIBRARY)
 include $(CLEAR_VARS)
diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml
index f22727c81..b3da21890 100644
--- a/android/AndroidManifest.xml
+++ b/android/AndroidManifest.xml
@@ -17,7 +17,7 @@
         <activity android:name=".SuperTuxKartActivity"
                   android:label="@string/app_name"
                   android:launchMode="singleTask"
-                  android:configChanges="fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|uiMode"
+                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|uiMode"
                   android:screenOrientation="sensorLandscape">
 
             <intent-filter>
@@ -28,7 +28,7 @@
         </activity>
     </application>
 
-    <uses-sdk android:minSdkVersion="19" 
+    <uses-sdk android:minSdkVersion="16"
               android:targetSdkVersion="29" />
     <uses-feature android:glEsVersion="0x00020000" />
     <uses-feature android:name="android.software.leanback" android:required="false" />
diff --git a/android/make.sh b/android/make.sh
index 36c5d3842..7119e42d1 100755
--- a/android/make.sh
+++ b/android/make.sh
@@ -20,8 +20,8 @@ export NDK_CPPFLAGS="-O3 -g"
 export NDK_ABI_ARMV7=armeabi-v7a
 export ARCH_ARMV7=arm
 export HOST_ARMV7=arm-linux-androideabi
-export NDK_PLATFORM_ARMV7=android-19
-export MIN_SDK_VERSION_ARMV7=19
+export NDK_PLATFORM_ARMV7=android-16
+export MIN_SDK_VERSION_ARMV7=16
 export TARGET_SDK_VERSION_ARMV7=29
 export COMPILE_SDK_VERSION_ARMV7=29
 
@@ -36,8 +36,8 @@ export COMPILE_SDK_VERSION_AARCH64=29
 export NDK_ABI_X86=x86
 export ARCH_X86=x86
 export HOST_X86=i686-linux-android
-export NDK_PLATFORM_X86=android-19
-export MIN_SDK_VERSION_X86=19
+export NDK_PLATFORM_X86=android-16
+export MIN_SDK_VERSION_X86=16
 export TARGET_SDK_VERSION_X86=29
 export COMPILE_SDK_VERSION_X86=29
 
@@ -314,6 +314,13 @@ if [ ! -f "$DIRNAME/obj/make_standalone_toolchain.stamp" ]; then
     echo $COMPILE_ARCH > "$DIRNAME/obj/compile_arch"
 fi
 
+# Last ndk supporting android 14 is 17c which has gcc-4.9, and most
+# dependencies require clang to build
+if [ -f "$DIRNAME/obj/bin/$HOST-gcc-4.9" ]; then
+    cp "$DIRNAME/obj/bin/clang" "$DIRNAME/obj/bin/$HOST-gcc"
+    cp "$DIRNAME/obj/bin/clang++" "$DIRNAME/obj/bin/$HOST-g++"
+fi
+
 echo "$PROJECT_VERSION" > "$DIRNAME/obj/project_version"
 
 # Zlib
@@ -338,11 +345,16 @@ if [ ! -f "$DIRNAME/obj/libpng.stamp" ]; then
     cp -a -f "$DIRNAME/../lib/libpng/"* "$DIRNAME/obj/libpng"
 
     cd "$DIRNAME/obj/libpng"
+    MLIBRARY="$DIRNAME/obj/sysroot/usr/lib/$HOST/libm.a"
+    # For ndk 17c
+    if [ ! -f "$MLIBRARY" ]; then
+        MLIBRARY="$DIRNAME/obj/sysroot/usr/lib/libm.a"
+    fi
     cmake . -DCMAKE_TOOLCHAIN_FILE=../../../cmake/Toolchain-android.cmake \
             -DHOST=$HOST -DARCH=$ARCH                                     \
             -DZLIB_LIBRARY="$DIRNAME/obj/zlib/libz.a"                     \
             -DZLIB_INCLUDE_DIR="$DIRNAME/obj/zlib/"                       \
-            -DM_LIBRARY="$DIRNAME/obj/sysroot/usr/lib/$HOST/libm.a"       \
+            -DM_LIBRARY="$MLIBRARY"                                       \
             -DPNG_TESTS=0 -DCMAKE_C_FLAGS="-fpic" &&
     make $@
     check_error
@@ -359,7 +371,7 @@ if [ ! -f "$DIRNAME/obj/freetype_bootstrap.stamp" ]; then
     ZLIB_CFLAGS="-I$DIRNAME/obj/zlib/" ZLIB_LIBS="$DIRNAME/obj/zlib/libz.a"\
     LIBPNG_CFLAGS="-I$DIRNAME/obj/libpng/" LIBPNG_LIBS="$DIRNAME/obj/libpng/libpng.a"\
     ./configure --host=$HOST --enable-shared=no \
-                --without-harfbuzz --without-brotli &&
+                --without-harfbuzz --without-brotli --without-bzip2 &&
     make $@
     check_error
     # We need to rebuild freetype after harfbuzz is compiled
@@ -395,7 +407,7 @@ if [ ! -f "$DIRNAME/obj/freetype.stamp" ]; then
     ZLIB_CFLAGS="-fpic -I$DIRNAME/obj/zlib/" ZLIB_LIBS="$DIRNAME/obj/zlib/libz.a" \
     LIBPNG_CFLAGS="-I$DIRNAME/obj/libpng/" LIBPNG_LIBS="$DIRNAME/obj/libpng/libpng.a" \
     HARFBUZZ_CFLAGS="-I$DIRNAME/obj/harfbuzz/src/" HARFBUZZ_LIBS="$DIRNAME/obj/harfbuzz/src/.libs/libharfbuzz.a" \
-    ./configure --host=$HOST --enable-shared=no --without-brotli
+    ./configure --host=$HOST --enable-shared=no --without-brotli --without-bzip2
     make $@
     check_error
     touch "$DIRNAME/obj/freetype.stamp"