kivy / python-for-android

Turn your Python application into an Android APK
https://python-for-android.readthedocs.io
MIT License
8.33k stars 1.84k forks source link

args.service_class_name results in link error #2679

Closed RobertFlatt closed 2 years ago

RobertFlatt commented 2 years ago

Checklist

Versions

Description

The p4a service_class_name option allows changing the service base class name, enabling a custom service class implementation. The implementation of the option is here:

https://github.com/kivy/python-for-android/blob/develop/pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java#L5-L8

One of the required methods in this class is run() which in the default implementation calls nativeStart(). https://github.com/kivy/python-for-android/blob/develop/pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java#L178-L187

However nativeStart() is a JNI method hard coded with the default base class name https://github.com/kivy/python-for-android/blob/develop/pythonforandroid/bootstraps/common/build/jni/application/src/start.c#L359.

So if a custom service base class is used to start a service, we get a run time link error. Because java_org_kivy_android_CustomPythonService_nativeStart() does not exist. See buildozer.spec and log snippets below, where we use the option to replace PythonService with CustomPythonService which has unchanged functionality.

The feature was added here https://github.com/kivy/python-for-android/commit/49de42153b137c310b84cd48a57176af08254d53

buildozer.spec

Command:

buildozer android debug

Spec file:

# (str) Full name including package path of the Java class that implements Python Service
# use that parameter to set custom Java class instead of PythonService
android.service_class_name = org.kivy.android.CustomPythonService

# (list) List of Java files to add to the android project (can be java or a
# directory containing the files)
android.add_src =org/kivy/android/CustomPythonService.java

Logs

09-27 16:28:23.517  9923  9944 E AndroidRuntime: java.lang.UnsatisfiedLinkError: No implementation found for void org.kivy.android.CustomPythonService.nativeStart(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) (tried Java_org_kivy_android_CustomPythonService_nativeStart and Java_org_kivy_android_CustomPythonService_nativeStart__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2)
RobertFlatt commented 2 years ago

My error the CustomPythonService should inherit from PythonService, and not replace it.

Though perhaps the text in buildozer.spec "instead of PythonService" is less than clear.