Python-for-android: Can't build a package with bcrypt as requirement

Created on 9 Jul 2019  ·  15Comments  ·  Source: kivy/python-for-android

Everything is in the title. If you attempt to build a package with bcrypt in the requirements, you'll get an error when it tries to import the "encodings" module.

Most helpful comment

Here's the recipe I created for BCrypt - it's probably not optimal by any means, but hey, WorksForMe™ so I guess it's GoodEnough™ ? :-)
("created" is an overstatement, it's basically copypasta from CryptographyRecipe hehe)

from pythonforandroid.recipe import CompiledComponentsPythonRecipe, Recipe


class BCryptRecipe(CompiledComponentsPythonRecipe):
    name = 'bcrypt'
    version = '3.1.7'
    url = 'https://github.com/pyca/bcrypt/archive/{version}.tar.gz'
    depends = ['openssl', 'cffi']
    call_hostpython_via_targetpython = False

    def get_recipe_env(self, arch):
        env = super(BCryptRecipe, self).get_recipe_env(arch)

        openssl_recipe = Recipe.get_recipe('openssl', self.ctx)
        env['CFLAGS'] += openssl_recipe.include_flags(arch)
        env['LDFLAGS'] += openssl_recipe.link_dirs_flags(arch)
        env['LIBS'] = openssl_recipe.link_libs_flags()

        return env


recipe = BCryptRecipe()

All 15 comments

This is due to SDL2 build blacklist: /pythonforandroid/bootstraps/sdl2/build/blacklist.txt

Adding the relevant files to a custom whitelist.txt doesn't seem to solve everything however, as now it fails on hashlib. Adding pycrypto to requirements doesn't seem to help.

This possibly runs quite deep and has been going on for a long time: https://stackoverflow.com/questions/24256134/using-hashlib-with-kivy-buildozer

https://github.com/kivy/python-for-android/issues/1260 this told me to add openssl to requirements, which I did. Now, I get a ModuleNotFoundError: No module named 'binascii' from hostpython3/Lib/zipfile.py...

Erf. A buildozer android clean debug seems to have fixed this one. :-)

So in short, it seems the only required steps are to add openssl to requirements in buildozer.spec, and manage the whitelist.txt to revert the blacklisted encodings.

The file looks as follows:

lib-dynload/*codec*
encodings/cp*.pyo
encodings/tis*
encodings/shift*
encodings/bz2*
encodings/iso*
encodings/undefined*
encodings/johab*
encodings/p*
encodings/m*
encodings/euc*
encodings/k*
encodings/unicode_internal*
encodings/quo*
encodings/gb*
encodings/big5*
encodings/hp*
encodings/hz*

And in buildozer.spec:
android.whitelist_src = whitelist.txt

The exact name of the config entry might be different in the future, I know it has changed in the past.

Oops: ImportError: dlopen failed: "/data/data/com.digitalfactoryparis.scs.app/files/app/_python_bundle/site-packages/bcrypt/_bcrypt.so" is 64-bit instead of 32-bit

I think I'll have to build a recipe, hehe.

Here's the recipe I created for BCrypt - it's probably not optimal by any means, but hey, WorksForMe™ so I guess it's GoodEnough™ ? :-)
("created" is an overstatement, it's basically copypasta from CryptographyRecipe hehe)

from pythonforandroid.recipe import CompiledComponentsPythonRecipe, Recipe


class BCryptRecipe(CompiledComponentsPythonRecipe):
    name = 'bcrypt'
    version = '3.1.7'
    url = 'https://github.com/pyca/bcrypt/archive/{version}.tar.gz'
    depends = ['openssl', 'cffi']
    call_hostpython_via_targetpython = False

    def get_recipe_env(self, arch):
        env = super(BCryptRecipe, self).get_recipe_env(arch)

        openssl_recipe = Recipe.get_recipe('openssl', self.ctx)
        env['CFLAGS'] += openssl_recipe.include_flags(arch)
        env['LDFLAGS'] += openssl_recipe.link_dirs_flags(arch)
        env['LIBS'] = openssl_recipe.link_libs_flags()

        return env


recipe = BCryptRecipe()

Great work :clap:
Why don't you pull request it?

Sorry man hectic time at work, coming very soon 😁

Also it doesn't include the whitelist in the recipe and I'm not quite sure
how to achieve that so your input is welcome 😊

No rush, you already provided the solution in this ticket. So if people are in a urge to see it in the tree, they know what to do :smile:
As for the whitelist, I think it could also just be a docstring comment in the recipe listing the modules that need to be whitelisted and pointing to this issue.
Good luck at work

I also had the
Oops: ImportError: dlopen failed: "/data/data/com.digitalfactoryparis.scs.app/files/app/_python_bundle/site-packages/bcrypt/_bcrypt.so" is 64-bit instead of 32-bit

issue!
Trying to compile now with your new recipe

I don't think this will work with arm64-v8a builds yet, I need to fix this properly at some point, can anyone provide some feedback on this? Thanks in advance :-)

I also had the
Error: dlopen failed: "/data/data/com.pablogod.eica.eica/files/app/_python_bundle/site-packages/bcrypt/_bcrypt.so" is 64-bit instead of 32-bit
How can I fix it guys?
Some useful information:
image

Please, can you explain what's a recipe and where I need to compile?

@ZurMaD check this
https://python-for-android.readthedocs.io/en/latest/recipes/#creating-your-own-recipe
Let me know if you need more help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmedfgad picture ahmedfgad  ·  7Comments

excepterror picture excepterror  ·  9Comments

etc0de picture etc0de  ·  9Comments

obfusk picture obfusk  ·  7Comments

benson-basis picture benson-basis  ·  6Comments