Repository: kivy/python-for-android

Issue:
Psycopg2 error after the apk installation.
![erro psycopg](https://user-images.githubusercontent.com/39860703/52805033-d8002480-306c-11e9-931b-87bc567e70f0.PNG)

I got this error while debugging the android apk. I associate this to Buildozer because I specified into the buildozer.spec requirements the psycopg2 library. It means that is not working.

How do I fix it? I know that is a recipe for psycopg2 here: https://github.com/kivy/python-for-android/blob/master/pythonforandroid/recipes/psycopg2/__init__.py

How can I add this recipe to my project, to buildozer compile it successfully ?


Discussion:
Usually you should share as much info as possible. e.g. your `buildozer.spec` your `main.py` code, your build log...
But yes it's more like a `p4a` related things to me. Have you tried extracting the APK to see what's in the `site-packages`?
I could reproduce the issue, after extracting the APK it turns out that `psycopg` is here, but in the wrong location `_python_bundle/site-packages/lib/python2.7/site-packages/psycopg2/_psycopg.so`
It's then obvious looking at the recipe code that the python path is hardcoded and it should not.
@piontk you can try your luck contributing the p4a by editing the recipe to fix it.
See here https://github.com/kivy/python-for-android/blob/9465952/pythonforandroid/recipes/psycopg2/__init__.py#L44 the site-package is hardcoded while it should be more generic to the Python version.
I extracted the APK and inside the lib directory (the "site-packages" directory do not exist) I could note that all the libraries where packaged, except the psycopg2. I'm using python 3 to run my app, it could be a problem? 
You need to extract two things, first unzip the `.apk` then `tar xf assets/private.mp3`
Probably just chaning `--install-lib=lib/python2.7/site-packages` to `--install-lib=.` in the recipe should be enough actually.
When you change it, you may need to `buildozer android p4a -- clean_recipe_build psycopg2` before rebuilding
![image](https://user-images.githubusercontent.com/39860703/53027663-241fe000-3444-11e9-9048-925bcfe71627.png)
After running the `tar xf assets/private.mp3` command line I got it. Seems that psycopg2 was packaged, but my app can't recognize it.
Yes it's because the path is wrong, see it says `_python_bundle/site-packages/lib/python2.7/site-package/psycopg2/`. It's because the `--install-lib=lib/python2.7/site-packages` arg is wrong
Ok, so to fix it I have to edit the __init__.py, inside the psycopg2 recipe directory. I tried to edit it to: `--install-lib=.`  as you said in a older comment. I had the same error on android debugging. After I tried to edit to `--install_lib=_python_bundle/site-packages`, but when I ran the `tar xf assets/private.mp3` command the result is the same. The psycopg2 is located at _python_bundle/site-packages/lib/python2.7/site-packages/psycopg2

P.S: After every change I made I ran the `buildozer android p4a -- clean_recipe_build psycopg2` before the `buildozer android debug` command
After a lot of attemptings I have found the error: I had specified a directory for customs recipes, and I was editting the wrong psycopg2 init.py file. After edit the right one I had this error message:
![image](https://user-images.githubusercontent.com/39860703/53060931-dc787300-349a-11e9-9375-7ed06a7b126c.png)

Great yes that new error is way better!
By the way when you're sharing errors, I think it's better not to share the screenshot, but the error text (using triple backquotes).
So now it seems like the module compiles and is loaded, but we're missing some symbols. I'm not sure yet why, but that's something to dig
Digging into it things feel more clear, it actually is a "feature" of the Android NDK.
When I look into `~/.buildozer/android/platform/android-ndk-r17c/sysroot/usr/include/langinfo.h` I see the following:
```c
#if __ANDROID_API__ >= 26
char* nl_langinfo(nl_item __item) __INTRODUCED_IN(26);
char* nl_langinfo_l(nl_item __item, locale_t __l) __INTRODUCED_IN(26);
#endif /* __ANDROID_API__ >= 26 */
```
So basically the header is available and will work at compile time, but the definition is not available if your `ANDROID_API` is below 26, hence you get undefined symbols runtime errors.
So I see two ways around it. One would be to try with higher API (`ndk-api` I guess), the other way would be to patch `psycopg2` to skip `nl_langinfo` calls

Resolving patch:
diff --git a/pythonforandroid/recipes/psycopg2/__init__.py b/pythonforandroid/recipes/psycopg2/__init__.py
--- a/pythonforandroid/recipes/psycopg2/__init__.py
+++ b/pythonforandroid/recipes/psycopg2/__init__.py
@@ -6,6 +6,9 @@
 class Psycopg2Recipe(PythonRecipe):
     """
     Requires `libpq-dev` system dependency e.g. for `pg_config` binary.
+    If you get `nl_langinfo` symbol runtime error, make sure you're running on
+    `ANDROID_API` (`ndk-api`) >= 26, see:
+    https://github.com/kivy/python-for-android/issues/1711#issuecomment-465747557
     """
     version = 'latest'
     url = 'http://initd.org/psycopg/tarballs/psycopg2-{version}.tar.gz'
@@ -41,7 +44,7 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
                     _env=env)
             shprint(hostpython, 'setup.py', 'install', '-O2',
                     '--root={}'.format(self.ctx.get_python_install_dir()),
-                    '--install-lib=lib/python2.7/site-packages', _env=env)
+                    '--install-lib=.', _env=env)
 
 
 recipe = Psycopg2Recipe()


Test patch: