由于python-for-android的编译的numpy库并不能在android中运行,所以特别写一篇文章记录下来。
1 工作目录
当前目录:~/apk-ex/numpy-setup
代码目录:~/apk-ex/numpy-src
虚拟环境:~/apk-ex/venv
依赖库:kivy[base]==2.2.1
2 安装软件
2.1 安装numpy
python -m pip install numpy -i https://mirrors.aliyun.com/pypi/simple
2.2 下载并运行补丁
补丁下载地址 https://download.csdn.net/download/RainbowPY/89543491
运行脚本 numpy_patch.py
3 编写代码
3.1 测试代码 ~/apk-ex/numpy-src/main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import numpy as np
class BoxLayoutWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
arr = np.array([1, 2, 3, 4, 5])
btn = Button(text=f'numpy array: {arr}')
self.add_widget(btn)
class TestApp(App):
def build(self):
return BoxLayoutWidget()
if '__main__' == __name__:
TestApp().run()
![](https://i-blog.csdnimg.cn/direct/8c1e625361d74c5ba5ab3277ce246744.png)
3.2 安装脚本 ~/apk-ex/numpy-setup/setup.py
from setuptools import setup
from setuptools import find_packages
import os
os.environ['ANDROIDSDK'] = os.path.abspath('../android-sdk')
os.environ['ANDROIDNDK'] = os.path.abspath('../android-sdk/ndk/25.2.9519653')
os.environ['ANDROIDAPI'] = '26'
os.environ['NDKAPI'] = '21'
options = {'apk': {'requirements': 'python3, sdl2, kivy, numpy',
'debug': None,
'dist-name': 'numpy-setup',
'package': 'org.apk.example',
'bootstrap': 'sdl2',
'arch': 'arm64-v8a'}}
setup(
name='numpy-test',
version='0.0',
description='numpy-for-android setup',
author='C+ detective',
packages=find_packages(),
options=options,
package_data={'../numpy-src': ['*.py']}
)
4 运行安装脚本
python setup.py apk
生成android安装包: numpy-setup-debug-0.0.apk
[INFO]: numpy-setup has compatible recipes, using this one
[INFO]: numpy-setup has compatible recipes, using this one
[INFO]: # Copying android package to current directory
[INFO]: # Copying android package to current directory
[INFO]: # Android package filename not found in build output. Guessing...
[INFO]: # Android package filename not found in build output. Guessing...
[INFO]: # Found android package file: /home/finance/.local/share/python-for-android/dists/numpy-setup/build/outputs/apk/debug/numpy-setup-debug.apk
[INFO]: # Found android package file: /home/finance/.local/share/python-for-android/dists/numpy-setup/build/outputs/apk/debug/numpy-setup-debug.apk
[INFO]: # Add version number to android package
[INFO]: # Add version number to android package
[INFO]: # Android package renamed to numpy-setup-debug-0.0.apk
[INFO]: # Android package renamed to numpy-setup-debug-0.0.apk
[DEBUG]: -> running cp /home/finance/.local/share/python-for-android/dists/numpy-setup/build/outputs/apk/debug/numpy-setup-debug.apk numpy-setup-debug-0.0.apk
[DEBUG]: -> running cp /home/finance/.local/share/python-for-android/dists/numpy-setup/build/outputs/apk/debug/numpy-setup-debug.apk numpy-setup-debug-0.0.apk
<Command '/usr/bin/cp /home/finance/.local/share/python-for-android/dists/numpy-setup/build/outputs/apk/debug/numpy-setup-debug.apk numpy-setup-debug-0.0.apk', pid 9603>: process started
(venv) finance@mx:~/apk-ex/numpy-setup
$