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

1.3.0 breaks android's request_permission #538

Closed
tshirtman opened this issue May 22, 2020 · 23 comments
Closed

1.3.0 breaks android's request_permission #538

tshirtman opened this issue May 22, 2020 · 23 comments

Comments

@tshirtman
Copy link
Member

reported by an user (Matrix0206 on discord).

from android import request_permission, Permission

request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE])
05-22 20:08:20.301 10976 11090 I python  :  Traceback (most recent call last):
05-22 20:08:20.301 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/app/main.py", line 300, in <module>
05-22 20:08:20.301 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/app.py", line 949, in run
05-22 20:08:20.302 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/app.py", line 919, in _run_prepare
05-22 20:08:20.302 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/app/main.py", line 132, in build
05-22 20:08:20.302 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/uix/relativelayout.py", line 274, in __init__
05-22 20:08:20.303 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/uix/floatlayout.py", line 65, in __init__
05-22 20:08:20.303 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/uix/layout.py", line 76, in __init__
05-22 20:08:20.303 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/uix/widget.py", line 359, in __init__
05-22 20:08:20.304 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/uix/widget.py", line 463, in apply_class_lang_rules
05-22 20:08:20.304 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/lang/builder.py", line 539, in apply
05-22 20:08:20.304 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/lang/builder.py", line 661, in _apply_rule
05-22 20:08:20.304 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/lang/builder.py", line 661, in _apply_rule
05-22 20:08:20.305 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/kivy/lang/builder.py", line 657, in _apply_rule
05-22 20:08:20.305 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/app/screens/main_screen/home.py", line 36, in __init__
05-22 20:08:20.305 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/android/permissions.py", line 570, in request_permissions
05-22 20:08:20.305 10976 11090 I python  :    File "/run/media/matrix/New_Volume/Developments/kivy-music-player/.buildozer/android/platform/build-arm64-v8a/build/python-installs/music_player/android/permissions.py", line 513, in request_permissions
05-22 20:08:20.306 10976 11090 I python  :    File "jnius/jnius_export_class.pxi", line 1134, in jnius.jnius.JavaMultipleMethod.__call__
05-22 20:08:20.306 10976 11090 I python  :  jnius.jnius.JavaException: No methods matching your arguments, requested: (['android.permission.READ_EXTERNAL_STORAGE', 'android.permission.WRITE_EXTERNAL_STORAGE'],), available: []

while this worked in 1.2.1.

@giona69
Copy link

giona69 commented Oct 17, 2020

hi guys I have the same problem, how to solve it? it's possible to use an older pyjnius in buildozer build? thanks!

@obfusk
Copy link
Contributor

obfusk commented Oct 19, 2020

hi guys I have the same problem, how to solve it? it's possible to use an older pyjnius in buildozer build? thanks!

Pinning the version using requirements = pyjnius==1.2.1,... should work.

@cmacdonald
Copy link
Contributor

I'm not a Kivy person, but can someone paste a link to the relevant source code?

@giona69
Copy link

giona69 commented Oct 19, 2020

hi guys I have the same problem, how to solve it? it's possible to use an older pyjnius in buildozer build? thanks!

Pinning the version using requirements = pyjnius==1.2.1,... should work.

thanks, infact it worked!

@cmacdonald
Copy link
Contributor

While pegging the Pyjnius version to 1.2.1 may work for this, v1.2.1 has other limitations, so ideally this should be addressed going forward....

@tshirtman
Copy link
Member Author

that would be there https://github.com/kivy/python-for-android/blob/develop/pythonforandroid/recipes/android/src/android/permissions.py#L513 sorry i didn't take the time to investigate this at all.

@cmacdonald
Copy link
Contributor

Its made difficult as we cant easily see what the Activity class name is

@obfusk
Copy link
Contributor

obfusk commented Oct 20, 2020

It looks like python_activity.requestPermissions is a non-static method. But it's being called as if it were static. Which for some reason used to work with older versions of pyjnius. Using python_activity.mActivity.requestPermissions instead seems to work with 1.3.0.

@obfusk
Copy link
Contributor

obfusk commented Oct 20, 2020

So it looks like it's a bug in p4a's android library instead.

@cmacdonald
Copy link
Contributor

Good spot. Indeed; as in Java, calling a non-static method on a static object would not be allowed by the compiler. @obfusk can you update kivy/python-for-android#2304?

I do think the Pyjnius error could be clearer also - ie the name of the method was not found and in what object!

@obfusk
Copy link
Contributor

obfusk commented Oct 20, 2020

I'm rather surprised this used to work. What instance was it being called on? A null pointer? Or some actual instance of the class?

@obfusk
Copy link
Contributor

obfusk commented Oct 20, 2020

Could this be related to these FIXMEs?

def __get__(self, obj, objtype):
if obj is None:
return self
# XXX FIXME we MUST not change our own j_self, but return a "bound"
# method here, as python does!
cdef JavaClass jc = obj
self.j_self = jc.j_self
return self

def __get__(self, obj, objtype):
if obj is None:
self.j_self = None
return self
# XXX FIXME we MUST not change our own j_self, but return a "bound"
# method here, as python does!
cdef JavaClass jc = obj
self.j_self = jc.j_self
return self

@obfusk
Copy link
Contributor

obfusk commented Oct 20, 2020

I do think the Pyjnius error could be clearer also - ie the name of the method was not found and in what object!

👍

@cmacdonald
Copy link
Contributor

I do think the Pyjnius error could be clearer also - ie the name of the method was not found and in what object!

👍

PR sent - see #558.

@cmacdonald
Copy link
Contributor

So the summary for anyone reading this in the future:

@obfusk
Copy link
Contributor

obfusk commented Oct 22, 2020

the patch has been merged

Not yet; but it has been approved :)

@obfusk
Copy link
Contributor

obfusk commented Oct 22, 2020

I think those FIXMEs are indeed related. Since __get__ sets the j_self instead of returning a bound method and simply returns self when called on a class instead of an instance, calling a non-static method on a class will use the last instance it was called on (if any). Of course, calling a non-static method on a class should never have been allowed (and no longer is).

@cmacdonald
Copy link
Contributor

I think those FIXMEs are indeed related. Since __get__ sets the j_self instead of returning a bound method and simply returns self when called on a class instead of an instance, calling a non-static method on a class will use the last instance it was called on (if any). Of course, calling a non-static method on a class should never have been allowed (and no longer is).

Can you show a simpler test case, and then we make a new issue?

@obfusk
Copy link
Contributor

obfusk commented Oct 22, 2020

Can you show a simpler test case, and then we make a new issue?

With 1.2.1:

>>> from jnius import autoclass
>>> s = autoclass("java.lang.String")
>>> s.toString()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "jnius/jnius_export_class.pxi", line 755, in jnius.JavaMethod.__call__
  File "jnius/jnius_export_class.pxi", line 699, in jnius.JavaMethod.ensure_method
jnius.JavaException: Unable to find a None method!
classname: None, definition: ()Ljava/lang/String;
>>> s("foo").toString()
'foo'
>>> s.toString()
'foo'

With 1.3.0:

>>> from jnius import autoclass
>>> s = autoclass("java.lang.String")
>>> s("foo").toString()
'foo'
>>> s.toString()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "jnius/jnius_export_class.pxi", line 1134, in jnius.JavaMultipleMethod.__call__
jnius.JavaException: No methods matching your arguments, requested: (), available: []

Since calling a non-static method on a class no longer works, the problem's already been fixed.
And now we understand the cause of the problem.
So I don't think we need a new issue.

@cmacdonald
Copy link
Contributor

Or: as no-one seems aware of how this was fixed in 1.3.0, we add a testcase to prevent a future regression back to the previous behaviour. The testcase can expect the exception in the case above.

And can we remove the FIXME's then?

@obfusk
Copy link
Contributor

obfusk commented Oct 22, 2020

Or: as no-one seems aware of how this was fixed in 1.3.0, we add a testcase to prevent a future regression back to the previous behaviour. The testcase can expect the exception in the case above.

I've actually figured it out now. And the problem was only partially fixed 😅. See #567

And can we remove the FIXME's then?

Not yet I think. See #563.

@xloem
Copy link

xloem commented Dec 13, 2020

Just a note, I changed my p4a branch to 'develop' and still had this issue. I wiped my .buildozer folder and rebuilt, and the 'develop' branch won't build fresh for me without linking errors (2020-12-13, 16:22 -0500) (EDIT: sorry, this was a different issue. I can't seem to run buildozer successfully on my system, at all, for now.)

I think the way to go for working around this in client apps is to manually make the pyjnius call, so the client codebase will work equally well on the different versions. Obviously releases that fix this are sorely needed here.

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