我正在使用 开发一个应用程序android studio 2.3.2
,我Jython
也使用过,因为我的应用程序中需要一个 Python 类。我已经安装Jython 2.7.0
并添加了path
我试图通过检查其他问题来解决我的问题,例如:
- 错误:任务“:app:transformClassesWithDexForDebug”执行失败
- com.android.build.transform.api.TransformException
- 为 Android 实现 Google 登录时任务“:app:transformClassesWithDexForDebug”执行失败
- Android Studio TransformException:错误:任务':app:transformClassesWithDexForDebug'的执行失败
- Android-错误:任务':app:transformClassesWithDexForRelease'的执行失败
- com.android.build.transform.api.TransformException
- ……
这是我的代码:
接口 SA
package com.aso.mdasa.mdasa;
interface SA {
Set resultCorpus(String DataBase, String polarity, String feature);
int resultSearch(String tweet);
}
情绪分析.java
class SentimentAnalysis {
private final Class interfaceType;
private final PyObject klass;
// Constructor obtains a reference to the importer, module, and the class name
private SentimentAnalysis(PySystemState state, Class interfaceType, String moduleName, String className) {
this.interfaceType = interfaceType;
PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__"));
PyObject module = importer.__call__(Py.newString(moduleName));
klass = module.__getattr__(className);
System.err.println("module=" + module + ",class=" + klass);
}
// This constructor passes through to the other constructor
public SentimentAnalysis(Class interfaceType, String moduleName, String className) {
this(new PySystemState(), interfaceType, moduleName, className);
}
// All of the followng methods return
// a coerced Jython object based upon the pieces of information
// that were passed into the SentimentAnalysis. The differences are
// between them are the number of arguments that can be passed
// in as arguents to the object.
public Object createObject() {
return klass.__call__().__tojava__(interfaceType);
}
public Object createObject(Object arg1) {
return klass.__call__(Py.java2py(arg1)).__tojava__(interfaceType);
}
Object createObject(Object arg1, Object arg2) {
return klass.__call__(Py.java2py(arg1), Py.java2py(arg2)).__tojava__(interfaceType);
}
public Object createObject(Object arg1, Object arg2, Object arg3)
{
return klass.__call__(Py.java2py(arg1), Py.java2py(arg2),
Py.java2py(arg3)).__tojava__(interfaceType);
}
private Object createObject(Object args[], String keywords[]) {
PyObject convertedArgs[] = new PyObject[args.length];
for (int i = 0; i < args.length; i++) {
convertedArgs[i] = Py.java2py(args[i]);
}
return klass.__call__(convertedArgs, keywords).__tojava__(interfaceType);
}
public Object createObject(Object... args) {
return createObject(args, Py.NoKeywords);
}
}
SAPython.py
from com.aso.mdasa.mdasa import SA
# Building object that subclasses a Java interface
from senticnet.senticnet import Senticnet
import pymysql
import pandas as pd
import sys
class SAPython (SA) :
__sn = Senticnet('ar')
def __init__(DBase) :
self.__connect(DBase)
def resultCorpus (self, polarity, feature = None):
result = {}
.....
return result
def resultSearch (self, tweet) :
return self.__calcTweetPolarity(tweet, len(tweet))
....
类结果搜索
public class ResultSearch extends FragmentActivity{
private String search;
private ImageView img ;
public static final int[] photos = {
R.drawable.v_bad,
R.drawable.bad,
R.drawable.good,
R.drawable.v_good } ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_search);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
public void onStart(){
super.onStart();
search = getIntent().getStringExtra("text");
img = (ImageView) findViewById(R.id.img);
sentimentAnalysis();
}
public void sentimentAnalysis() {
SentimentAnalysis sentimentA = new SentimentAnalysis(
SA.class, "SAPython", "SentimentAnalysis");
SA sa = (SA) sentimentA.createObject();
double result = sa.resultSearch(search);
if (result < -0.5)
img.setBackground(getResources().getDrawable(photos[0]));
if (result >= -0.5 && result < 0)
img.setBackground(getResources().getDrawable(photos[1]));
if (result >= 0 && result < 0.5)
img.setBackground(getResources().getDrawable(photos[2]));
if (result >= 0.5)
img.setBackground(getResources().getDrawable(photos[3]));
}
}
我收到此错误:
FAILURE:构建失败并出现异常。
出了什么问题:任务“:app:transformClassesWithDexForDebug”执行失败。com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: dex 进程返回代码 1
尝试:使用 --info 或 --debug 选项运行以获得更多日志输出。
例外是:org.gradle.api.tasks.TaskExecutionException:任务':app:transformClassesWithDexForDebug'的执行失败。在 org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:84) 在 org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:55) 在 org.gradle .api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62) 在 org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58) 在 org.gradle.api.internal .tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88) 在 org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.
这是我的gradle.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
dexOptions {
incremental true
preDexLibraries = false
javaMaxHeapSize "4g"
}
defaultConfig {
applicationId "com.aso.mdasa.mdasa"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Enabling multidex support.
multiDexEnabled true
compileOptions.encoding = 'UTF8'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:multidex:1.0.1'
testCompile 'junit:junit:4.12'
compile files('libs/jython-standalone-2.7.0.jar')
}
这是我的 gradle.properties:
ject-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx4608M -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true