Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crashes if we use 'org.renpy.android.PythonActivity' in p4a new toolchain #249

Closed
germn opened this issue Dec 17, 2016 · 16 comments
Closed

Comments

@germn
Copy link

germn commented Dec 17, 2016

If we use org.renpy.android.PythonActivity (instead new org.kivy.android.PythonActivity) in app built with p4a new toolchain it'll crash. See this issue for example (there's also typical log there):

W/PythonActivity(28456): Accessing org.renpy.android.PythonActivity is deprecated and will be removed in a future version. Please switch to org.kivy.android.PythonActivity.
F/art     (28456): art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: static jfieldID 0x746c91a0 not valid for class java.lang.Class<org.renpy.android.PythonActivity>
F/art     (28456): art/runtime/check_jni.cc:65]     in call to GetStaticObjectField
F/art     (28456): art/runtime/check_jni.cc:65]     from int org.libsdl.app.SDLActivity.nativeInit(java.lang.Object)
F/art     (28456): art/runtime/check_jni.cc:65] "SDLThread" prio=5 tid=15 Runnable
...

It can be temporary solved by replacing org.renpy.android.PythonActivity with org.kivy.android.PythonActivity everywhere in the code manually or with some dirty patch.

But correct solution would be to fix pyjnius not to crash with org.renpy.android.PythonActivity in p4a new toolchain.

@andreinl
Copy link

andreinl commented Jan 31, 2017

Sorry for naive question, but how can I apply your patch?

I tried to add your patch to .buildozer/android/platform/python-for-android-master/pythonforandroid/recipes/pyjnius. So I basically mirrored init.py" and copied ugly_pyjnius_fix.patch, but it produce no effect.

Thank you in advance.

UPDATE: I was able manually download your branch before everything else was downloaded and compiled, unfortunately app still crashes with the same error message.

P.S. Will it be fixed one day?

@germn
Copy link
Author

germn commented Jan 31, 2017

@andreinl

  1. You should have local copy of branch with fix.

Just download and unpack it or better run:

git clone -b ugly_pyjnius_fix https://github.com/germn/python-for-android/

  1. After that you need to specify path to your local p4a in your buildozer.spec

android.p4a_dir = /home/kivy/python-for-android

  1. Just to be sure build would be fully fresh, I would advice to delete hidden folder .buildozer in directory where your buildozer.spec exist.

  2. Now you can run buildozer android_new debug

Probably it would fix error for you.

I don't know how to create correct fix that can be merged in master. You can draw attention to this issue of someone who may know asking about it here.

@andreinl
Copy link

andreinl commented Feb 1, 2017

@germn

Thank you for fast reply. Unfortunately as I already saw yesterday even with your fix App crash with the same error.

Just in case it can be useful for anybody: I compile my App on Mac OS.
App crashes on Android 6.0.
App runs Ok (even without your patch) on BlueStacks emulator (Android 4.4.2).

@germn
Copy link
Author

germn commented Feb 1, 2017

@andreinl are you sure your app contain fix? Problem with manual editing files inside .buildozer is that they may duplicate in different places and may be re-downloaded when you restart toolchain. I would still advice you to try algorithm from my previous comment.

@andreinl
Copy link

andreinl commented Feb 1, 2017

@germn
Yes, I did as you suggested, unfortunately with the same result.

@inclement
Copy link
Member

The fix here is to change your code to reference org.kivy.android.PythonActivity - this isn't a problem with pyjnius, it works fine, but the PythonActivity class is just in a different place with the new toolchain.

@kchomski
Copy link

kchomski commented Mar 4, 2017

@inclement

The fix here is to change your code to reference org.kivy.android.PythonActivity - this isn't a problem with pyjnius, it works fine, but the PythonActivity class is just in a different place with the new toolchain.

Could you please elaborate how plyer should be used then without applying patch to pyjnius?
Because without patching pyjnius with @germn patch it still tries to use renpy and results in error (it works fine after patch is applied).

I have simple app (both plyer and pyjnius are in the current version from master):
main.py:

import plyer
from plyer import flash
print 'Plyer version is:', plyer.__version__
flash.on()

buildozer.spec:

[app]
title = App
package.name = app
package.domain = org.test
source.dir = test/.
source.include_exts = py
version = 0.1
requirements = flask,pyjnius,plyer
android.bootstrap = webview
orientation = portrait
fullscreen = 1
android.permissions = INTERNET, VIBRATE, CAMERA, FLASHLIGHT

[buildozer]
log_level = 2
warn_on_root = 1

Log from device (Samsung J5 (2016)):

03-04 12:46:04.813 31995 32037 I python  : Run user program, change dir and execute entrypoint
03-04 12:46:04.813 31995 32037 I python  : main.py
03-04 12:46:04.813 31995 32042 W AudioCapabilities: Unsupported mime audio/evrc
03-04 12:46:04.833 31995 32042 W VideoCapabilities: Unsupported mime video/wvc1
03-04 12:46:04.833 31995 32042 W VideoCapabilities: Unsupported mime video/x-ms-wmv
03-04 12:46:04.863 31995 32042 W VideoCapabilities: Unrecognized profile/level 32768/2 for video/mp4v-es
03-04 12:46:04.863 31995 32037 I python  : Plyer version is: 1.2.5dev
03-04 12:46:04.863 31995 32042 W VideoCapabilities: Unsupported mime video/wvc1
03-04 12:46:04.863 31995 32042 W VideoCapabilities: Unsupported mime video/x-ms-wmv
03-04 12:46:04.873 31995 32037 W linker  : /data/data/org.test.app/files/lib/python2.7/site-packages/jnius/jnius.so: is missing DT_SONAME will use basename as a replacement: "jnius.so"
03-04 12:46:04.873 31995 32042 W VideoCapabilities: Unsupported mime video/x-ms-wmv7
03-04 12:46:04.873 31995 32042 W VideoCapabilities: Unsupported mime video/x-ms-wmv8
03-04 12:46:04.883 31995 32042 W VideoCapabilities: Unsupported mime video/mp43
03-04 12:46:04.883 31995 32039 V WebViewLoader: Could not ping localhost:5000
03-04 12:46:04.923 31995 32042 I VideoCapabilities: Unsupported profile 4 for video/mp4v-es
03-04 12:46:04.933  4018  6810 V WindowStateAnimator: Finishing drawing window Window{62ab5c9 u0 d0 org.test.app/org.kivy.android.PythonActivity}: mDrawState=DRAW_PENDING
03-04 12:46:04.933 31995 31995 D ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0, 0) or=1
03-04 12:46:04.933  4018  4776 D InputMethodManagerService: windowGainedFocus mCurrentFocusedUserId - 0 and mSecureKeypadEnabled-false
03-04 12:46:04.943  4018  4209 D KnoxTimeoutHandler: notifyActivityDrawn [MsgParam] userId: 0 fullscreen is true showWhenlocked is false
03-04 12:46:04.943  4018  4018 D KnoxTimeoutHandler: activityDrawn [MsgParam] userId: 0 fullscreen is true showWhenlocked is false
03-04 12:46:04.943  4018  4209 I ActivityManager: Displayed org.test.app/org.kivy.android.PythonActivity: +1s841ms
03-04 12:46:04.943  4018  4209 I Timeline: Timeline: Activity_windows_visible id: ActivityRecord{1f9d0ed u0 org.test.app/org.kivy.android.PythonActivity t88} time:48372138
03-04 12:46:04.943  4018  4209 D CustomFrequencyManagerService: releaseDVFSLockLocked : Getting Lock type frm List : DVFS_MIN_LIMIT  frequency : 1190400  uid : 1000  pid : 4018  tag : ACTIVITY_RESUME_BOOSTER@7
03-04 12:46:04.943  4018  4209 D ActivityManager: mDVFSHelper.release()
03-04 12:46:04.943  4018  4139 D CustomFrequencyManagerService: acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1190400  uid : 1000  pid : 4018  pkgName : ACTIVITY_RESUME_BOOSTER@13
03-04 12:46:04.953  4018  4209 D ViewRootImpl: #3 mView = null
03-04 12:46:04.953  4018  4018 I KnoxTimeoutHandler: SD activityfalse
03-04 12:46:04.953  4018  4018 I KnoxTimeoutHandler: Fullscreen and mCurrent is not KNOX user. Hence hide keyguard
03-04 12:46:04.953 24901 24901 D SamsungIME: IMPL finishInput
03-04 12:46:04.953  4018  4038 V WindowStateAnimator: Finishing drawing window Window{62ab5c9 u0 d0 org.test.app/org.kivy.android.PythonActivity}: mDrawState=HAS_DRAWN
03-04 12:46:04.983 31995 32039 V WebViewLoader: Could not ping localhost:5000
03-04 12:46:05.083 31995 31995 W cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 31995
03-04 12:46:05.083 31995 32039 V WebViewLoader: Could not ping localhost:5000
03-04 12:46:05.133 31995 31995 I chromium: [INFO:CONSOLE(24)] "called!", source: file:///data/user/0/org.test.app/files/_load.html (24)
03-04 12:46:05.143   343  6822 I SurfaceFlinger: id=692 Removed bpp (5/8)
03-04 12:46:05.153   343  2071 I SurfaceFlinger: id=692 Removed bpp (-2/8)
03-04 12:46:05.183 31995 32039 V WebViewLoader: Could not ping localhost:5000
03-04 12:46:05.293 31995 32039 V WebViewLoader: Could not ping localhost:5000
03-04 12:46:05.393 31995 32039 V WebViewLoader: Could not ping localhost:5000
03-04 12:46:05.493 31995 32039 V WebViewLoader: Could not ping localhost:5000
03-04 12:46:05.553  4018  4018 D CustomFrequencyManagerService: releaseDVFSLockLocked : Getting Lock type frm List : DVFS_MIN_LIMIT  frequency : 1190400  uid : 1000  pid : 4018  tag : ACTIVITY_RESUME_BOOSTER@13
03-04 12:46:05.553  4018  4018 D CustomFrequencyManagerService: FrequencyrequestList.getNextCStateDisableRequest, index: 0
03-04 12:46:05.593 31995 32039 V WebViewLoader: Could not ping localhost:5000
03-04 12:46:05.633 31995 32037 W PythonActivity: Accessing org.renpy.android.PythonActivity is deprecated and will be removed in a future version. Please switch to org.kivy.android.PythonActivity.
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: static jfieldID 0xb4766278 not valid for class java.lang.Class<org.renpy.android.PythonActivity>
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]     in call to GetStaticObjectField
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]     from int org.kivy.android.PythonActivity.nativeInit(java.lang.Object)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410] "PythonThread" prio=5 tid=23 Runnable
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   | group="main" sCount=0 dsCount=0 obj=0x12ff74c0 self=0xb8226c60
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   | sysTid=32037 nice=0 cgrp=default sched=0/0 handle=0x9f90a930
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   | state=R schedstat=( 822978519 127366843 988 ) utm=76 stm=6 core=1 HZ=100
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   | stack=0x9f808000-0x9f80a000 stackSize=1038KB
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   | held mutexes= "mutator lock"(shared held)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #00 pc 0035d32d  /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiPKcPNS_9ArtMethodEPv+116)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #01 pc 0033de0b  /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+138)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #02 pc 0024ffb9  /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+760)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #03 pc 00250657  /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+54)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #04 pc 000fc16b  /system/lib/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+30)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #05 pc 00112bbf  /system/lib/libart.so (_ZN3art11ScopedCheck16CheckFieldAccessERNS_18ScopedObjectAccessEP8_jobjectP9_jfieldIDbNS_9Primitive4TypeE+434)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #06 pc 00114421  /system/lib/libart.so (_ZN3art8CheckJNI8GetFieldEPKcP7_JNIEnvP8_jobjectP9_jfieldIDbNS_9Primitive4TypeE+488)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #07 pc 0011496d  /system/lib/libart.so (_ZN3art8CheckJNI20GetStaticObjectFieldEP7_JNIEnvP7_jclassP9_jfieldID+24)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   native: #08 pc 0003129b  /data/data/org.test.app/files/lib/python2.7/site-packages/jnius/jnius.so (???)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   at org.kivy.android.PythonActivity.nativeInit(Native method)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   at org.kivy.android.PythonMain.run(PythonActivity.java:430)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410]   at java.lang.Thread.run(Thread.java:818)
03-04 12:46:05.663 31995 32037 F art     : art/runtime/java_vm_ext.cc:410] 
03-04 12:46:06.073  4018  4261 D PackageManager: [MSG] WRITE_PACKAGE_RESTRICTIONS

@inclement
Copy link
Member

inclement commented Mar 4, 2017 via email

@kchomski
Copy link

kchomski commented Mar 5, 2017

It worked after changing org.renpy.android to org.kivy.android in the file you mentioned. Should I make a pull request for that? Or is there any reason plyer is still using the old value?
Thanks for your help.

Here's a diff:

diff --git a/plyer/platforms/android/__init__.py b/plyer/platforms/android/__init__.py
index 8fe1263..252f8b4 100644
--- a/plyer/platforms/android/__init__.py
+++ b/plyer/platforms/android/__init__.py
@@ -8,7 +8,7 @@ try:
     from android import config
     ns = config.JAVA_NAMESPACE
 except (ImportError, AttributeError):
-    ns = 'org.renpy.android'
+    ns = 'org.kivy.android'
 
 if 'PYTHON_SERVICE_ARGUMENT' in environ:
     PythonService = autoclass(ns + '.PythonService')

@inclement
Copy link
Member

plyer needs to determine which java class path is the correct one to use. Currently it has code to do that, but only if the android module is available (which it won't be unless added to the p4a requirements argument). Instead, it should be appropriate to do something like:

from jnius import autoclass, JavaException
activity_classes = ('org.kivy.android.PythonActivity', 'org.renpy.android.PythonActivity')
for activity in activity_classes:
    try:
        activity = autoclass(activity)
    except JavaException:
        pass
    else:  # the class was found
        break
else:
    raise ValueError('Could not autoclass any Java activity from {}'.format(activity_classes))

I haven't tested it but something like this should be sound.

@matphysrin
Copy link

Sorry for a stupid question: If I start from the webview bootstrap, the app can't find either 'org.kivy.android.PythonActivity' or 'org.renpy.android.PythonActivity'. Do I have to include one of them explicitly at build?

@kchomski
Copy link

kchomski commented Mar 10, 2017

@matphysrin: You have to include pyjnius in your requirements so buildozer/p4a could include it in your .apk.
Please post a full log if you still have problems.

@inclement
Copy link
Member

@matphysrin With the webview bootstrap, the activity class os org.kivy.android.Pythonactivity.

@matphysrin
Copy link

Thanks for the answers. I already include pyjnius, and import org.kivy.android.PythonActivity, but still get an error.

This is the log I get with p4a adb logcat:

I/python  ( 4329):     import luscinia_android_test as lat
I/python  ( 4329):   File "/sdcard/luscinia/luscinia_android_test.zip/luscinia_android_test.py", line 3, in <module>
I/python  ( 4329):   File "/data/data/org.example.luscinia/files/lib/python2.7/site-packages/jnius/reflect.py", line 154, in autoclass
I/python  ( 4329):     c = find_javaclass(clsname)
I/python  ( 4329):   File "jnius/jnius_export_func.pxi", line 25, in jnius.jnius.find_javaclass (jnius/jnius.c:16608)
I/python  ( 4329): JavaException: Class not found 'org/kivy/android/PythonActivity'

The actual source code where it crashes reads:

from jnius import autoclass, cast

PythonActivity = autoclass('org.kivy.android.PythonActivity')

The result with org.renpy.android.PythonActivity is the same.

I build the app like this:

python-for-android apk --private . --dist_name=webview_01 --android-api=15 --ndk-version=r11b  --requirements=cherrypy,sqlalchemy,sqlite3,pyjnius --package=org.example.luscinia --name "Luscinia" --permission WRITE_EXTERNAL_STORAGE --permission INTERNET --add-source=luscinia --add-source=unidecode --version=0.26 --port=9371 

where the environment webview_01 is built using

python-for-android create --dist_name=webview_01 --bootstrap=webview --requirements=python2,sqlite3,cherrypy,sqlalchemy,pyjnius

@inclement
Copy link
Member

inclement commented Mar 10, 2017 via email

@matphysrin
Copy link

matphysrin commented Mar 10, 2017

It seems the problem is caused by setting the PythonActivity within a webserver (cherrypy) subthread, rather than in the main app thread. When I call it before the server starts, it works.

Thanks for your help, guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants