• 关于安卓调用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" }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    (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'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    至此,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]
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

     if (!Python.isStarted()) {
         Python.start(new AndroidPlatform(this.mContext));
     }
    
    • 1
    • 2
    • 3

    (2)调用文件中的方法

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

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

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

    that’s all---------------------------------------------------------------------------------

  • 相关阅读:
    JDK 17新更新的 14个新特性
    SpringBoot防Xss攻击
    Nginx学习(2)—— 常用命令
    yarn的安装与配置(Windows/macOS)
    【YOLOv7】主要改进点详解
    关于产品MVP的定义与实践
    第3章 决策树
    【广州华锐互动】AR轨道交通综合教学平台的应用
    05JVM_类加载阶段
    【科学文献计量】pybibx论文原文精读与对照翻译
  • 原文地址:https://blog.csdn.net/motosheep/article/details/126616353