关于安卓调用python代码(chaquo)(一)

简介: 安卓调用python代码(chaquo)

摘要:

日常安卓开发中,跨语言开发也是常有发生的事情。
本文,将介绍如何在安卓中,通过sdk调用python文件。

!!!源码地址在文末!!!

开发环境

win10
androidstudio 4+
gradle version 3.6+
jdk 1.8
python 3+

发车

chaquo官方指引 官网

(1)androidstudio安装插件 Python Community Edition,本地已经配置好python运行环境

(2)在项目根目录的build.gradle,引入依赖,如下图:


buildscript {
    repositories {
        ...
        //python library
        maven { url "https://chaquo.com/maven" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0'
        ...
        //python library
        classpath "com.chaquo.python:gradle:10.0.1"
    }
}

allprojects {
    repositories {
        ...
        //python library
        maven { url "https://chaquo.com/maven" }
    }
}

(3)在模块的build.gradle下,申明引入chaquo,ndk声明,python申明即可。代码如下图:

plugins {
    id 'com.android.library'
    id 'com.chaquo.python'
}

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        consumerProguardFiles 'consumer-rules.pro'
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }


        python {
//            buildPython "D:\\software\\python\\python.exe"
            pip {
//                install "opencv-python"
                install "numpy"
//                install "wave"
//                install "scipy"
//                install "matplotlib"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            consumerProguardFiles 'proguard-rules-libpython.pro'
        }
        debug {
            minifyEnabled false
            consumerProguardFiles 'proguard-rules-libpython.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

至此,python依赖引入完成。

运行

首先,在main目录下面,新建一个python目录,然后新建一个python文件。例子如下:

def add(a,b):
    return a + b

def sub(count,a=0,b=0,c=0):
    return count - a - b -c

def get_list(a,b,c,d):
    return [a,b,c,d]

最后,在java代码中,进行调用:
(1)sdk初始化:

 if (!Python.isStarted()) {
     Python.start(new AndroidPlatform(this.mContext));
 }

(2)调用文件中的方法

Python py = Python.getInstance();
PyObject backend = py.getModule("fileName");
backend.callAttr("funcName", "arg")

至此,即可实现调用。
代码地址

备注:若因网络环境问题,建议开启vpn。若因电脑python环境问题,请耐心配置。本demo实测可用。

that's all---------------------------------------------------------------------------------

目录
相关文章
|
3月前
|
存储 算法 调度
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
196 26
|
3月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
358 1
|
3月前
|
机器学习/深度学习 算法 调度
基于多动作深度强化学习的柔性车间调度研究(Python代码实现)
基于多动作深度强化学习的柔性车间调度研究(Python代码实现)
191 1
|
2月前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
249 100
|
2月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
407 95
|
3月前
|
Python
Python的简洁之道:5个让代码更优雅的技巧
Python的简洁之道:5个让代码更优雅的技巧
255 104
|
3月前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
453 99
|
2月前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
155 88
|
3月前
|
IDE 开发工具 开发者
Python类型注解:提升代码可读性与健壮性
Python类型注解:提升代码可读性与健壮性
279 102
|
2月前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。

热门文章

最新文章

推荐镜像

更多