From beb28ff32a945a75c6f22e5cb71aaa7d343586f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Sat, 14 Oct 2023 23:24:30 +0200 Subject: [PATCH 1/2] Properly initialize class loader on Android https://github.com/OpenRCT2/OpenRCT2/pull/20502 changed how startup is handled. This affected Android as well and changed AndroidClassLoader to be initialized statically, but this turns out to be problematic due to JVM not being fully initialized in our context by this time. To fix this, move AndroidClassLoader initialization to JNI_OnLoad call, where JVM is fully available. Additionally, guard against multiple calls to JNI_OnLoad, an issue present on Linux-like systems (including Android). --- distribution/changelog.txt | 1 + src/openrct2/platform/Platform.Android.cpp | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/distribution/changelog.txt b/distribution/changelog.txt index b20dbb7b5c..c530f1c585 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -10,6 +10,7 @@ - Fix: [#18199] Dots in the game save's name no longer get truncated. - Fix: [#19722] “Forbid tree removal” restriction doesn't forbid removal of large scenery tree items. - Fix: [#20356] Cannot set tertiary colour on small scenery. +- Fix: [#20679] Android: game crashes at launch. - Fix: [#20737] Spent money in player window underflows when getting refunds. - Fix: [#20778] [Plugin] Incorrect target api when executing custom actions. - Fix: [#20807] Tertiary colour not copied with small scenery. diff --git a/src/openrct2/platform/Platform.Android.cpp b/src/openrct2/platform/Platform.Android.cpp index 068432d18d..54c9350978 100644 --- a/src/openrct2/platform/Platform.Android.cpp +++ b/src/openrct2/platform/Platform.Android.cpp @@ -27,7 +27,9 @@ AndroidClassLoader::~AndroidClassLoader() jobject AndroidClassLoader::_classLoader; jmethodID AndroidClassLoader::_findClassMethod; -static std::shared_ptr acl = std::make_shared(); +// Initialized in JNI_OnLoad. Cannot be initialized here as JVM is not +// available until after JNI_OnLoad is called. +static std::shared_ptr acl; namespace Platform { @@ -181,6 +183,19 @@ namespace Platform } } // namespace Platform +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* pjvm, void* reserved) +{ + // Due to an issue where JNI_OnLoad could be called multiple times, we need + // to make sure it is only initialized once. + // https://issuetracker.google.com/issues/220523932 + // Otherwise JVM complains about jobject-s having incorrect serial numbers. + if (!acl) + { + acl = std::make_shared(); + } + return JNI_VERSION_1_6; +} + AndroidClassLoader::AndroidClassLoader() { LOG_INFO("Obtaining JNI class loader"); From 929e2e5790ccb30d77e7178dcb37b305916c3a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Sun, 15 Oct 2023 00:30:46 +0200 Subject: [PATCH 2/2] Let gradle use 4GB --- src/openrct2-android/gradle.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/src/openrct2-android/gradle.properties b/src/openrct2-android/gradle.properties index acf164f6cd..fa2c20f971 100644 --- a/src/openrct2-android/gradle.properties +++ b/src/openrct2-android/gradle.properties @@ -18,3 +18,4 @@ # org.gradle.parallel=true android.enableJetifier=true android.useAndroidX=true +org.gradle.jvmargs=-Xmx4096m