美文网首页
关于安卓调用python代码(chaquo)(一)

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

作者: motosheep | 来源:发表于2022-08-31 08:23 被阅读0次

    摘要:

    日常安卓开发中,跨语言开发也是常有发生的事情。
    本文,将介绍如何在安卓中,通过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---------------------------------------------------------------------------------

    相关文章

      网友评论

          本文标题:关于安卓调用python代码(chaquo)(一)

          本文链接:https://www.haomeiwen.com/subject/orzrnrtx.html