Я пытаюсь использовать python скриптов в android проекте (java). Моя конечная цель - запустить скрипты консоли face_recognition, поэтому я начал с демонстрации PythonConsoleActivity от Chaquopy. https://github.com/chaquo/chaquopy-console Я создал PythonActivity. java (который напоминает шаблон MainActivity. java из chaquopy)
package com.projectdevelopment.faces;
import android.app.*;
import com.projectdevelopment.faces.python.PythonConsoleActivity;
public class PythonActivity extends PythonConsoleActivity {
@Override protected Class<? extends Task> getTaskClass() {
return Task.class;
}
public static class Task extends PythonConsoleActivity.Task {
public Task(Application app) {
super(app);
}
@Override public void run() {
py.getModule("main").callAttr("main");
}
}
}
У меня есть файл main.py, хранящийся в src / main / python по совету Chaquopy. Структура проекта
from six.moves import input
def main():
print("Enter your name, or an empty line to exit.")
while True:
try:
name = input()
except EOFError:
break
if not name:
break
print("Hello {}!".format(name))
Я настроил проект gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven { url "https://chaquo.com/maven" }
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath 'com.google.gms:google-services:4.3.3'
classpath "com.chaquo.python:gradle:8.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
и приложение gradle
apply plugin: 'com.android.application'
apply plugin: 'com.chaquo.python'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.projectdevelopment.faces"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
python{
buildPython "C:/Python38/python.exe"
//staticProxy "main.py"
pip{
install "six"
}
}
ndk {
abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
sourceSets{
main{
python{
srcDirs = ["src/main/python"]
}
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//***********************ADDITIONAL DEPENDENCIES************************
implementation 'com.wonderkiln:camerakit:0.13.1'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
//**********************************************************************
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
}
apply plugin: 'com.google.gms.google-services'
Когда я запускаю приложение, я использую кнопку из mainActivity, чтобы запустить PythonActivity следующим образом:
python.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pythonActivity = new Intent(getApplicationContext(), PythonActivity.class);
startActivity(pythonActivity);
}
});
При нажатии кнопки python возникает фатальное исключение: Невозможно создать экземпляр класса (см. ниже)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.projectdevelopment.faces, PID: 4286
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projectdevelopment.faces/com.projectdevelopment.faces.PythonActivity}: java.lang.RuntimeException: Cannot create an instance of class com.projectdevelopment.faces.PythonActivity$Task
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.projectdevelopment.faces.PythonActivity$Task
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:275)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.projectdevelopment.faces.python.ConsoleActivity.onCreate(ConsoleActivity.java:53)
at com.projectdevelopment.faces.python.PythonConsoleActivity.onCreate(PythonConsoleActivity.java:20)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:267)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.projectdevelopment.faces.python.ConsoleActivity.onCreate(ConsoleActivity.java:53)
at com.projectdevelopment.faces.python.PythonConsoleActivity.onCreate(PythonConsoleActivity.java:20)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: com.chaquo.python.PyException: ModuleNotFoundError: No module named 'chaquopy'
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:973)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:961)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:961)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
at <python>.importlib.import_module(__init__.py:127)
at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:154)
at com.chaquo.python.Python.getModule(Native Method)
at com.projectdevelopment.faces.python.PythonConsoleActivity$Task.<init>(PythonConsoleActivity.java:54)
at com.projectdevelopment.faces.python.PythonConsoleActivity$Task.<init>(PythonConsoleActivity.java:49)
at com.projectdevelopment.faces.PythonActivity$Task.<init>(PythonActivity.java:14)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:267)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at com.projectdevelopment.faces.python.ConsoleActivity.onCreate(ConsoleActivity.java:53)
at com.projectdevelopment.faces.python.PythonConsoleActivity.onCreate(PythonConsoleActivity.java:20)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 4286 SIG: 9
Я уверен, что упускаю что-то простое, но я в растерянности. Я использую 64-битную версию Python3 .8.
Есть предложения?