Android运行scipy | 运行python

chaquopy之前收费现在免费,收到资助
which meets the following requirements:
环境需求:
Android Gradle plugin version should be between 4.1 and 7.2. 
minSdkVersion must be at least 21

In your top-level build.gradle file

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '4.2.2' apply false
    id 'com.android.library' version '4.2.2' apply false
    id 'com.chaquo.python' version '14.0.2' apply false

}

task clean(type: Delete) {
    delete rootProject.buildDir
}

In the module-level build.gradle file 

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

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.demo_chaquopy"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        python {
            buildPython "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python39\\python.exe"
            pip {
                install "scipy"
                install "pandas"
//                install "opencv-python"
                install "matplotlib"
            }
        }
        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation files('libs\\hellocharts-library-1.5.8.jar')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

pip里面添加你要安装的库,不是所有的库都可以顺利安装,但是这是目前最好的方案了

这样就把库导入到你的项目了怎么用,构建一下,会自动下载你要的库,下不下来你就要那个你懂的操作

在这个目录添加你的py代码

def sayHello():
    print("Hello World")

在java里调用

if (!Python.isStarted()){
    Python.start(new AndroidPlatform(this));
}
Python python=Python.getInstance();
PyObject pyObject=python.getModule("hello");
pyObject.callAttr("sayHello");

 

posted @ 2023-03-29 16:41  Z_Chan  阅读(109)  评论(0编辑  收藏  举报