You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:在Android Studio中构建可运行命令与脚本的Python安卓可执行程序

构建安卓可用的Python可执行程序方案

1. 准备交叉编译环境

  • 安装Android NDK:在Android Studio的SDK Manager中下载LTS版本NDK(如25.x),配置环境变量NDK_ROOT指向NDK安装目录。
  • 下载Python源码:选择3.10+版本的Python源码包,解压到本地工作目录。

2. 交叉编译Python解释器到安卓架构

以arm64-v8a架构为例,其他架构(armeabi-v7a/x86/x86_64)操作逻辑一致,仅需替换对应架构参数:

  • 生成独立交叉编译工具链:
    $NDK_ROOT/build/tools/make_standalone_toolchain.py --arch arm64 --api 24 --install-dir ./android-arm64-toolchain
    
    api 24对应安卓7.0,可根据项目最低兼容版本调整。
  • 配置Python编译参数:
    进入Python源码目录,执行以下配置脚本:
    export TOOLCHAIN_DIR=./android-arm64-toolchain
    export SYSROOT=$TOOLCHAIN_DIR/sysroot
    export CC=$TOOLCHAIN_DIR/bin/aarch64-linux-android-clang
    export CXX=$TOOLCHAIN_DIR/bin/aarch64-linux-android-clang++
    export AR=$TOOLCHAIN_DIR/bin/aarch64-linux-android-ar
    export LD=$TOOLCHAIN_DIR/bin/aarch64-linux-android-ld
    export RANLIB=$TOOLCHAIN_DIR/bin/aarch64-linux-android-ranlib
    export STRIP=$TOOLCHAIN_DIR/bin/aarch64-linux-android-strip
    
    ./configure \
      --host=aarch64-linux-android \
      --build=x86_64-linux-gnu \
      --prefix=./android-python-arm64 \
      --enable-shared \
      --disable-ipv6 \
      --without-ensurepip \
      ac_cv_file__dev_ptmx=no \
      ac_cv_file__dev_ptc=no
    
  • 编译并安装:
    make -j$(nproc)
    make install
    
    编译完成后,./android-python-arm64目录下会生成安卓平台可用的Python解释器与依赖库。

3. 集成到Android Studio项目

  • 创建或打开Native C++项目,将编译好的python3可执行文件和libpython3.so等库文件,复制到项目的src/main/jniLibs/arm64-v8a目录(对应架构的子目录)。
  • 将自定义Python脚本复制到项目的assets目录。

4. 在安卓中调用Python程序

通过进程调用方式执行Python解释器与脚本,示例Kotlin代码:

// 复制解释器到可执行目录(安卓不允许直接执行jniLibs/assets中的文件)
val pythonExec = applicationContext.jniLibsDir.listFiles()?.find { it.name == "python3" } ?: return
val destPython = File(applicationContext.filesDir, "python3")
if (!destPython.exists()) {
    pythonExec.copyTo(destPython)
    destPython.setExecutable(true)
}

// 复制assets中的脚本到可读写目录
val scriptFile = File(applicationContext.filesDir, "custom_script.py")
if (!scriptFile.exists()) {
    applicationContext.assets.open("custom_script.py").use { input ->
        scriptFile.outputStream().use { input.copyTo(it) }
    }
}

// 执行脚本并获取输出
val process = ProcessBuilder(destPython.absolutePath, scriptFile.absolutePath)
    .redirectErrorStream(true)
    .start()
val output = process.inputStream.bufferedReader().readText()
process.waitFor()
// 处理output结果
  • 权限配置:在AndroidManifest.xml中添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />(如需读写外部存储),并处理动态权限申请。

5. 优化与多架构适配

  • 裁剪体积:编译Python时可通过--disable-modules参数禁用不需要的内置模块,减小解释器体积。
  • 多架构支持:重复交叉编译步骤,生成其他架构的Python解释器,复制到对应jniLibs子目录。
  • 依赖打包:若脚本依赖第三方Python库,可在交叉编译后用pip install --target=./android-python-arm64/lib/python3.x/site-packages安装依赖,再将整个site-packages目录复制到项目中,运行时添加到Python路径。

内容的提问来源于stack exchange,提问作者Shubham Chakraborty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:02:47