We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
ctypes.util.find_library 64-bit error
I was trying to use `zeroconf` package for my application. It worked perfectly on `armeabi-v7a`, however the program crashed on launch on `arm64-v8a` (both tested on Huawei P30).

I have investigated the issues and discovered that the problem is with `ctypes.util.find_library` or, more precisely with the p4a module `andoroid._ctypes_library_finder` in the function `find_library`.

The actual problem is that this function finds 32bit libraries regardless of the actual architecture. For example

```python
ctypes.util.find_library('c')
```

returns `/system/lib/libc.so` both for 32- and 64-bit architecture. The correct behavior is to return this if Python is compiled for 32-bit and `/system/lib64/libc.so` for 64-bit one.

Below is the code of a simple Kivy app that shows the issue:

```python
# main.py
import sys
import ctypes


from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label


root = Builder.load_string("""\
#:import sys sys

BoxLayout:
    orientation: 'vertical'
    Label:
        id: arch
        size_hint_y: 1
        text_size: self.size
        halign: 'center'
        valign: 'middle'
        text: '64-bit' if sys.maxsize > 2**32 else '32-bit'
    Label:
        id: lib
        size_hint_y: 1
        text_size: self.size
        halign: 'center'
        valign: 'middle'
    Label:
        id: err
        size_hint_y: 4
        text_size: self.size
        halign: 'left'
        valign: 'middle'
""")


class TestCtypesApp(App):

    def build(self):
        lib = ctypes.util.find_library('c')
        root.ids.lib.text = str(lib)
        try:
            cdll = ctypes.CDLL(lib)
        except Exception as err:
            root.ids.err.text = "{}: {}".format(type(err).__name__, err)
        else:
            root.ids.err.text = 'CORRECT'
            root.ids.err.halign = 'center'
        return root


if __name__ == '__main__':
    TestCtypesApp().run()
```

```ini
# buildozer.spec
[app]
title = Test CTypes
package.name = testctypes
package.domain = org.test
source.dir = .
source.include_exts = py
version = 0.1
requirements = python3,kivy
orientation = portrait
osx.python_version = 3
osx.kivy_version = 1.9.1
fullscreen = 0
android.api = 30
android.arch = armeabi-v7a
ios.kivy_ios_url = https://github.com/kivy/kivy-ios
ios.kivy_ios_branch = master
ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
ios.ios_deploy_branch = 1.7.0

[buildozer]
log_level = 2
warn_on_root = 1

[app@arm64]
android.arch = arm64-v8a
```

When compiled for  `armeabi-v7a` it shows:

```
32-bit

/system/lib/libc.so

CORRECT
```

while on `arm64-v8a`:

```
64-bit

/system/lib/libc.so

OSError: dlopen failed: library "/system/lib/libc.so" needed or dlopened by
"/data/data/org.test.testctypes/files/app/_python_bundle/modules/_ctypes.cpython-38.so"
is not accessible for this namespace "classloader-namespace"
```

The expected output is:

```
64-bit

/system/lib64/libc.so

CORRECT
```

The source of this problem is in the line 47 of the file [pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py](../blob/develop/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py#L47). For 64-bit Python (build target arch matters, not the system archiecture), the libraries to search should be `["/system/lib64/libc.so", "/system/lib/libc.so"]`.

I am also submitting a pull request resolving this issue.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py`
Content:
```
1 
2 import os
3 
4 
5 def get_activity_lib_dir(activity_name):
6     from jnius import autoclass
7 
8     # Get the actual activity instance:
9     activity_class = autoclass(activity_name)
10     if activity_class is None:
11         return None
12     activity = None
13     if hasattr(activity_class, "mActivity") and \
14             activity_class.mActivity is not None:
15         activity = activity_class.mActivity
16     elif hasattr(activity_class, "mService") and \
17             activity_class.mService is not None:
18         activity = activity_class.mService
19     if activity is None:
20         return None
21 
22     # Extract the native lib dir from the activity instance:
23     package_name = activity.getApplicationContext().getPackageName()
24     manager = activity.getApplicationContext().getPackageManager()
25     manager_class = autoclass("android.content.pm.PackageManager")
26     native_lib_dir = manager.getApplicationInfo(
27         package_name, manager_class.GET_SHARED_LIBRARY_FILES
28     ).nativeLibraryDir
29     return native_lib_dir
30 
31 
32 def does_libname_match_filename(search_name, file_path):
33     # Filter file names so given search_name="mymodule" we match one of:
34     #      mymodule.so         (direct name + .so)
35     #      libmymodule.so      (added lib prefix)
36     #      mymodule.arm64.so   (added dot-separated middle parts)
37     #      mymodule.so.1.3.4   (added dot-separated version tail)
38     #      and all above       (all possible combinations)
39     import re
40     file_name = os.path.basename(file_path)
41     return (re.match(r"^(lib)?" + re.escape(search_name) +
42                      r"\.(.*\.)?so(\.[0-9]+)*$", file_name) is not None)
43 
44 
45 def find_library(name):
46     # Obtain all places for native libraries:
47     lib_search_dirs = ["/system/lib"]
48     lib_dir_1 = get_activity_lib_dir("org.kivy.android.PythonActivity")
49     if lib_dir_1 is not None:
50         lib_search_dirs.insert(0, lib_dir_1)
51     lib_dir_2 = get_activity_lib_dir("org.kivy.android.PythonService")
52     if lib_dir_2 is not None and lib_dir_2 not in lib_search_dirs:
53         lib_search_dirs.insert(0, lib_dir_2)
54 
55     # Now scan the lib dirs:
56     for lib_dir in [ldir for ldir in lib_search_dirs if os.path.exists(ldir)]:
57         filelist = [
58             f for f in os.listdir(lib_dir)
59             if does_libname_match_filename(name, f)
60         ]
61         if len(filelist) > 0:
62             return os.path.join(lib_dir, filelist[0])
63     return None
64
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py b/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py
--- a/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py
+++ b/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py
@@ -1,4 +1,5 @@
 
+import sys
 import os
 
 
@@ -44,7 +45,10 @@
 
 def find_library(name):
     # Obtain all places for native libraries:
-    lib_search_dirs = ["/system/lib"]
+    if sys.maxsize > 2**32:  # 64bit-build
+        lib_search_dirs = ["/system/lib64", "/system/lib"]
+    else:
+        lib_search_dirs = ["/system/lib"]
     lib_dir_1 = get_activity_lib_dir("org.kivy.android.PythonActivity")
     if lib_dir_1 is not None:
         lib_search_dirs.insert(0, lib_dir_1)

{"golden_diff": "diff --git a/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py b/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py\n--- a/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py\n+++ b/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py\n@@ -1,4 +1,5 @@\n \n+import sys\n import os\n \n \n@@ -44,7 +45,10 @@\n \n def find_library(name):\n     # Obtain all places for native libraries:\n-    lib_search_dirs = [\"/system/lib\"]\n+    if sys.maxsize > 2**32:  # 64bit-build\n+        lib_search_dirs = [\"/system/lib64\", \"/system/lib\"]\n+    else:\n+        lib_search_dirs = [\"/system/lib\"]\n     lib_dir_1 = get_activity_lib_dir(\"org.kivy.android.PythonActivity\")\n     if lib_dir_1 is not None:\n         lib_search_dirs.insert(0, lib_dir_1)\n", "issue": "ctypes.util.find_library 64-bit error\nI was trying to use `zeroconf` package for my application. It worked perfectly on `armeabi-v7a`, however the program crashed on launch on `arm64-v8a` (both tested on Huawei P30).\r\n\r\nI have investigated the issues and discovered that the problem is with `ctypes.util.find_library` or, more precisely with the p4a module `andoroid._ctypes_library_finder` in the function `find_library`.\r\n\r\nThe actual problem is that this function finds 32bit libraries regardless of the actual architecture. For example\r\n\r\n```python\r\nctypes.util.find_library('c')\r\n```\r\n\r\nreturns `/system/lib/libc.so` both for 32- and 64-bit architecture. The correct behavior is to return this if Python is compiled for 32-bit and `/system/lib64/libc.so` for 64-bit one.\r\n\r\nBelow is the code of a simple Kivy app that shows the issue:\r\n\r\n```python\r\n# main.py\r\nimport sys\r\nimport ctypes\r\n\r\n\r\nfrom kivy.app import App\r\nfrom kivy.lang import Builder\r\nfrom kivy.uix.label import Label\r\n\r\n\r\nroot = Builder.load_string(\"\"\"\\\r\n#:import sys sys\r\n\r\nBoxLayout:\r\n    orientation: 'vertical'\r\n    Label:\r\n        id: arch\r\n        size_hint_y: 1\r\n        text_size: self.size\r\n        halign: 'center'\r\n        valign: 'middle'\r\n        text: '64-bit' if sys.maxsize > 2**32 else '32-bit'\r\n    Label:\r\n        id: lib\r\n        size_hint_y: 1\r\n        text_size: self.size\r\n        halign: 'center'\r\n        valign: 'middle'\r\n    Label:\r\n        id: err\r\n        size_hint_y: 4\r\n        text_size: self.size\r\n        halign: 'left'\r\n        valign: 'middle'\r\n\"\"\")\r\n\r\n\r\nclass TestCtypesApp(App):\r\n\r\n    def build(self):\r\n        lib = ctypes.util.find_library('c')\r\n        root.ids.lib.text = str(lib)\r\n        try:\r\n            cdll = ctypes.CDLL(lib)\r\n        except Exception as err:\r\n            root.ids.err.text = \"{}: {}\".format(type(err).__name__, err)\r\n        else:\r\n            root.ids.err.text = 'CORRECT'\r\n            root.ids.err.halign = 'center'\r\n        return root\r\n\r\n\r\nif __name__ == '__main__':\r\n    TestCtypesApp().run()\r\n```\r\n\r\n```ini\r\n# buildozer.spec\r\n[app]\r\ntitle = Test CTypes\r\npackage.name = testctypes\r\npackage.domain = org.test\r\nsource.dir = .\r\nsource.include_exts = py\r\nversion = 0.1\r\nrequirements = python3,kivy\r\norientation = portrait\r\nosx.python_version = 3\r\nosx.kivy_version = 1.9.1\r\nfullscreen = 0\r\nandroid.api = 30\r\nandroid.arch = armeabi-v7a\r\nios.kivy_ios_url = https://github.com/kivy/kivy-ios\r\nios.kivy_ios_branch = master\r\nios.ios_deploy_url = https://github.com/phonegap/ios-deploy\r\nios.ios_deploy_branch = 1.7.0\r\n\r\n[buildozer]\r\nlog_level = 2\r\nwarn_on_root = 1\r\n\r\n[app@arm64]\r\nandroid.arch = arm64-v8a\r\n```\r\n\r\nWhen compiled for  `armeabi-v7a` it shows:\r\n\r\n```\r\n32-bit\r\n\r\n/system/lib/libc.so\r\n\r\nCORRECT\r\n```\r\n\r\nwhile on `arm64-v8a`:\r\n\r\n```\r\n64-bit\r\n\r\n/system/lib/libc.so\r\n\r\nOSError: dlopen failed: library \"/system/lib/libc.so\" needed or dlopened by\r\n\"/data/data/org.test.testctypes/files/app/_python_bundle/modules/_ctypes.cpython-38.so\"\r\nis not accessible for this namespace \"classloader-namespace\"\r\n```\r\n\r\nThe expected output is:\r\n\r\n```\r\n64-bit\r\n\r\n/system/lib64/libc.so\r\n\r\nCORRECT\r\n```\r\n\r\nThe source of this problem is in the line 47 of the file [pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py](../blob/develop/pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py#L47). For 64-bit Python (build target arch matters, not the system archiecture), the libraries to search should be `[\"/system/lib64/libc.so\", \"/system/lib/libc.so\"]`.\r\n\r\nI am also submitting a pull request resolving this issue.\n", "before_files": [{"content": "\nimport os\n\n\ndef get_activity_lib_dir(activity_name):\n    from jnius import autoclass\n\n    # Get the actual activity instance:\n    activity_class = autoclass(activity_name)\n    if activity_class is None:\n        return None\n    activity = None\n    if hasattr(activity_class, \"mActivity\") and \\\n            activity_class.mActivity is not None:\n        activity = activity_class.mActivity\n    elif hasattr(activity_class, \"mService\") and \\\n            activity_class.mService is not None:\n        activity = activity_class.mService\n    if activity is None:\n        return None\n\n    # Extract the native lib dir from the activity instance:\n    package_name = activity.getApplicationContext().getPackageName()\n    manager = activity.getApplicationContext().getPackageManager()\n    manager_class = autoclass(\"android.content.pm.PackageManager\")\n    native_lib_dir = manager.getApplicationInfo(\n        package_name, manager_class.GET_SHARED_LIBRARY_FILES\n    ).nativeLibraryDir\n    return native_lib_dir\n\n\ndef does_libname_match_filename(search_name, file_path):\n    # Filter file names so given search_name=\"mymodule\" we match one of:\n    #      mymodule.so         (direct name + .so)\n    #      libmymodule.so      (added lib prefix)\n    #      mymodule.arm64.so   (added dot-separated middle parts)\n    #      mymodule.so.1.3.4   (added dot-separated version tail)\n    #      and all above       (all possible combinations)\n    import re\n    file_name = os.path.basename(file_path)\n    return (re.match(r\"^(lib)?\" + re.escape(search_name) +\n                     r\"\\.(.*\\.)?so(\\.[0-9]+)*$\", file_name) is not None)\n\n\ndef find_library(name):\n    # Obtain all places for native libraries:\n    lib_search_dirs = [\"/system/lib\"]\n    lib_dir_1 = get_activity_lib_dir(\"org.kivy.android.PythonActivity\")\n    if lib_dir_1 is not None:\n        lib_search_dirs.insert(0, lib_dir_1)\n    lib_dir_2 = get_activity_lib_dir(\"org.kivy.android.PythonService\")\n    if lib_dir_2 is not None and lib_dir_2 not in lib_search_dirs:\n        lib_search_dirs.insert(0, lib_dir_2)\n\n    # Now scan the lib dirs:\n    for lib_dir in [ldir for ldir in lib_search_dirs if os.path.exists(ldir)]:\n        filelist = [\n            f for f in os.listdir(lib_dir)\n            if does_libname_match_filename(name, f)\n        ]\n        if len(filelist) > 0:\n            return os.path.join(lib_dir, filelist[0])\n    return None\n", "path": "pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py"}], "after_files": [{"content": "\nimport sys\nimport os\n\n\ndef get_activity_lib_dir(activity_name):\n    from jnius import autoclass\n\n    # Get the actual activity instance:\n    activity_class = autoclass(activity_name)\n    if activity_class is None:\n        return None\n    activity = None\n    if hasattr(activity_class, \"mActivity\") and \\\n            activity_class.mActivity is not None:\n        activity = activity_class.mActivity\n    elif hasattr(activity_class, \"mService\") and \\\n            activity_class.mService is not None:\n        activity = activity_class.mService\n    if activity is None:\n        return None\n\n    # Extract the native lib dir from the activity instance:\n    package_name = activity.getApplicationContext().getPackageName()\n    manager = activity.getApplicationContext().getPackageManager()\n    manager_class = autoclass(\"android.content.pm.PackageManager\")\n    native_lib_dir = manager.getApplicationInfo(\n        package_name, manager_class.GET_SHARED_LIBRARY_FILES\n    ).nativeLibraryDir\n    return native_lib_dir\n\n\ndef does_libname_match_filename(search_name, file_path):\n    # Filter file names so given search_name=\"mymodule\" we match one of:\n    #      mymodule.so         (direct name + .so)\n    #      libmymodule.so      (added lib prefix)\n    #      mymodule.arm64.so   (added dot-separated middle parts)\n    #      mymodule.so.1.3.4   (added dot-separated version tail)\n    #      and all above       (all possible combinations)\n    import re\n    file_name = os.path.basename(file_path)\n    return (re.match(r\"^(lib)?\" + re.escape(search_name) +\n                     r\"\\.(.*\\.)?so(\\.[0-9]+)*$\", file_name) is not None)\n\n\ndef find_library(name):\n    # Obtain all places for native libraries:\n    if sys.maxsize > 2**32:  # 64bit-build\n        lib_search_dirs = [\"/system/lib64\", \"/system/lib\"]\n    else:\n        lib_search_dirs = [\"/system/lib\"]\n    lib_dir_1 = get_activity_lib_dir(\"org.kivy.android.PythonActivity\")\n    if lib_dir_1 is not None:\n        lib_search_dirs.insert(0, lib_dir_1)\n    lib_dir_2 = get_activity_lib_dir(\"org.kivy.android.PythonService\")\n    if lib_dir_2 is not None and lib_dir_2 not in lib_search_dirs:\n        lib_search_dirs.insert(0, lib_dir_2)\n\n    # Now scan the lib dirs:\n    for lib_dir in [ldir for ldir in lib_search_dirs if os.path.exists(ldir)]:\n        filelist = [\n            f for f in os.listdir(lib_dir)\n            if does_libname_match_filename(name, f)\n        ]\n        if len(filelist) > 0:\n            return os.path.join(lib_dir, filelist[0])\n    return None\n", "path": "pythonforandroid/recipes/android/src/android/_ctypes_library_finder.py"}]}