我正在尝试在 android 项目(java)中使用 python 脚本。我的最终目标是运行 face_recognition 控制台脚本,所以我从 Chaquopy 的 PythonConsoleActivity 演示开始。https://github.com/chaquo/chaquopy-console 我创建了一个 PythonActivity.java(类似于 chaquopy 的 MainActivity.java 模板)
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");
}
}
}
按照 Chaquopy 的建议,我将 main.py 文件存储在 src/main/python 中。
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。
有什么建议么?