-
Notifications
You must be signed in to change notification settings - Fork 45
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
Fix android detection when python4android is present #277
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test and fix typecheker.
d4b9c33
to
9be26e1
Compare
done |
Use the builtin mActivity of python4android to get hold of our main activity, which is capable of generating a proper application context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests 👍
I've added a test with a mocked android import ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When using platformdirs in an android app located on adopted storage,
_android_folder()
inandroid.py
returnedNone
which lead platformdirs to believe that it doesn't run on android and use theUnix
flavor instead.In android.py#L126
autoclass("android.content.Context")
returns a class instead of an instance, which throws an exception on the next line trying to access an instance method namedgetFilesDir()
.That exception is catched and a fallback implementation invoked, that uses a regular expression to search in
sys.path
for an import path rooted in/data
. Apps installed on adopted storage don't have such paths but begin with/mnt/expand/
instead.The regular expression therefore fails and
_android_folder()
returnsNone
.This pull request fixes the issue at least for cases where python4android is available, by first trying to import the
android
module and using it'smActivity
export to get hold of the main activity. That activity is then used to get a proper application context upon whichgetFilesDir()
can be invoked.If that fails with an exception the original codepath is taken, trying the
autoclass
approach next and if that fails resorting to the regular expression.