Skip to content

Commit

Permalink
Workaround Go issue causing the app to crash on older Android versions
Browse files Browse the repository at this point in the history
* Fix app crashes when using Go > 1.23 and Android < 12
* See golang/go#70508
  • Loading branch information
Martchus committed Jan 23, 2025
1 parent cb065f2 commit a81e6d4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tray/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,21 @@ if (ANDROID)
endif ()

if (USE_LIBSYNCTHING)
# bundle internal Syncthing library
set_property(
TARGET ${META_TARGET_NAME}
APPEND
PROPERTY QT_ANDROID_EXTRA_LIBS "${SYNCTHINGINTERNAL_LIBRARY_PATH}")

# workaround https://github.com/golang/go/issues/70508
set(ANDROID_SIGNAL_HANDLER_LIBRARY "androidsignalhandler")
add_library("${ANDROID_SIGNAL_HANDLER_LIBRARY}" MODULE android/signalhandler.cpp)
find_library(ANDROID_LOG_LIB log)
target_link_libraries("${ANDROID_SIGNAL_HANDLER_LIBRARY}" "${ANDROID_LOG_LIB}")
set_property(
TARGET ${META_TARGET_NAME}
APPEND
PROPERTY QT_ANDROID_EXTRA_LIBS "${CMAKE_CURRENT_BINARY_DIR}/lib${ANDROID_SIGNAL_HANDLER_LIBRARY}.so")
endif ()

set(QT_ANDROID_SIGN_APK ON)
Expand Down
36 changes: 36 additions & 0 deletions tray/android/signalhandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <android/log.h>
#include <jni.h>

#include <csignal>

#define ST_LIB_LOG_TAG "SyncthingLibrary"
#define ST_LIB_LOG_INF(...) __android_log_print(ANDROID_LOG_INFO, ST_LIB_LOG_TAG, __VA_ARGS__)

static void handleSigsys(int signum, siginfo_t *info, void *context)
{
// ignore the signal to prevent the app from crashing
ST_LIB_LOG_INF("SIGSYS signal received and ignored");
}

static void initSigsysHandler()
{
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handleSigsys;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGSYS, &sa, nullptr) == -1) {
ST_LIB_LOG_INF("Failed to set up SIGSYS handler");
} else {
ST_LIB_LOG_INF("SIGSYS handler set up successfully");
}
}

extern "C" {

JNIEXPORT void JNICALL
Java_io_github_martchus_syncthingtray_Activity_initSigsysHandler(JNIEnv *env, jobject thiz)
{
initSigsysHandler();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public int fontWeightAdjustment() {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Creating");

// workaround https://github.com/golang/go/issues/70508
System.loadLibrary("androidsignalhandler");
initSigsysHandler();

super.onCreate(savedInstanceState);

// read font scale as Qt does not handle this automatically on Android
Expand Down Expand Up @@ -200,4 +205,5 @@ private void sendAndroidIntentToQtQuickApp(String page, boolean fromNotification

private static native void handleAndroidIntent(String page, boolean fromNotification);
private static native void stopLibSyncthing();
private static native void initSigsysHandler();
}

0 comments on commit a81e6d4

Please sign in to comment.