introduction
Python is a powerful and ecologically rich language that is widely used in data processing, machine learning and backend services. However, Python is not a native mobile development language (Android mainly uses Java/Kotlin, iOS mainly uses Swift/Objective-C). If you want to call Python code in a mobile app (Android or iOS), the best way isPackage Python code into SDK, and then passFFI (external function interface) or local serviceLet the App access it.
This article will introduce several typesPython code packaged into callable SDKMethods includingPyInstaller、Cython、PyOxidizer、Kivy + Buildozer、Chaquopy、BeeWareetc. and provide detailed steps.
1. Choose the packaging method
Different packaging methods are suitable for different application scenarios:
method | Applicable scenarios | Applicable platform | Key advantages | Main disadvantages |
---|---|---|---|---|
PyInstaller | Desktop apps | Windows, macOS, Linux | Quickly generate executable files | The generated file is large and not suitable for mobile |
Cython | Computation intensive modules | Windows, macOS, Linux, Android, iOS | Accelerate Python code | Requires additional C language development |
PyOxidizer | Cross-platform application | Windows, macOS, Linux | Generate a standalone executable file | Complex configuration |
Kivy + Buildozer | Mobile App | Android, iOS | Develop a mobile app directly | Fewer UI components |
Chaquopy | Run Python code on Android | Android | Adapt to native UI | Supports only Android |
BeeWare | Run Python code on iOS | iOS, Android | Adapt to native UI | Immature ecology |
If your goal isPackage Python code into SDK for Android/iOS App calls, the following methods are recommended:
- Cython + JNI(Android)
- Python backend + HTTP API (general solution)
- Chaquopy (Android only)
- BeeWare (iOS only)
2. Method 1: Use Cython to generate dynamic link library (suitable for Android/iOS)
2.1. Principle
Cython can compile Python code into C code and generate dynamic link libraries (.so or .dll), which can then be called on Android or iOS via JNI (Java Native Interface) or FFI (Foreign Function Interface).
2.2. Steps
(1) Install Cython
pip install cython
(2) Write Python code
Suppose we have a simple Python computing modulemath_utils.py
:
def add(a: int, b: int) -> int: return a + b
(3) Createmath_utils.pyx
Change the code to Cython code (.pyx
document):
# math_utils.pyx def add(int a, int b): return a + b
(4) Writing
from setuptools import setup from import cythonize setup( ext_modules=cythonize("math_utils.pyx", language_level="3"), )
(5) Compile into.so
(Linux/Android) or.dylib
(macOS/iOS)
python build_ext --inplace
This will generatemath_utils.cpython-38-x86_64
(Linux) or.dylib
(macOS)。
(6) Calling in Java/Kotlin (Android)
Loading in Android project.so
document:
("math_utils"); public class MathUtils { public native int add(int a, int b); }
3. Method 2: Use the HTTP API (suitable for Android/iOS)
3.1. Principle
- Deploy Python code to the server
- Let mobile apps access Python services through HTTP API
3.2. Steps
(1) Install FastAPI
pip install fastapi uvicorn
(2) Write FastAPI code
from fastapi import FastAPI app = FastAPI() @("/add") def add(a: int, b: int): return {"result": a + b} if __name__ == "__main__": import uvicorn (app, host="0.0.0.0", port=8000)
(3) Run the API server
uvicorn main:app --host 0.0.0.0 --port 8000
(4) Call on the mobile app
Request API in Android/iOS code:
URL url = new URL("http://server_ip:8000/add?a=3&b=4"); HttpURLConnection conn = (HttpURLConnection) (); ("GET");
4. Method 3: Use Chaquopy (for Android)
4.1. Principle
Chaquopy allows you to run Python code directly in an Android project and interact with Java.
4.2. Steps
(1) Project on AndroidAdd dependencies in
dependencies { implementation ':gradle:12.0' }
(2)Run Python code
import ; import ; if (!()) { (new AndroidPlatform(this)); } Python py = (); PyObject obj = ("script").callAttr("add", 3, 5); (());
5. Method 4: Use BeeWare (for iOS)
5.1. Principle
BeeWare allows running Python code on iOS, usingRubicon-ObjC
Enter iOS interaction.
5.2. Steps
Install BeeWare:
pip install briefcase
Create an iOS project:
briefcase create iOS
Run iOS emulator:
briefcase run iOS
6. Summary
plan | Applicable platform | Key advantages | Main disadvantages |
---|---|---|---|
Cython + JNI | Android, iOS | High performance, locally invoked | Need to compile.so /.dylib , more complex |
HTTP API | General | Easy to expand | Requires a network connection |
Chaquopy | Android | Run Python directly on Android | Supports only Android |
BeeWare | iOS | Run Python directly on iOS | Immature ecology |
If you wantPython runs efficiently inside the App, it is recommended to useCython + JNI; If you wantQuick integration,suggestionUse FastAPI as the backend. Different solutions can be selected for different application scenarios. I hope this article can help you successfully package Python code into SDK!
The above is the detailed content of several methods to package Python code into callable SDKs (applicable to mobile apps). For more information about packaging Python code into SDKs, please pay attention to my other related articles!