【问题标题】:NoSuchFieldError in Android Studio projectAndroid Studio 项目中的 NoSuchFieldError
【发布时间】:2015-03-27 21:44:58
【问题描述】:

我正在尝试将我的 Android Studio - 应用程序连接到交换网络服务,但我不断收到以下错误:

java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicLineFormatter; in class Lorg/apache/http/message/BasicLineFormatter; or its superclasses (declaration of 'org.apache.http.message.BasicLineFormatter' appears in /system/framework/ext.jar)

这是我在我的 android 项目中添加到 /libs/ 的 .jars:

这是发生错误时的 onCreate 方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ExchangeService service = new ExchangeService(); // this is where the error is hitting
ExchangeCredentials credentials = new WebCredentials("user", "pw");
service.setCredentials(credentials);

try {
    service.setUrl(new URI("uri"));
} catch (URISyntaxException e) {
    e.printStackTrace();
}

EmailMessage msg= null;
try {
    msg = new EmailMessage(service);
    msg.setSubject("Hello world!");
    msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Java API."));
    msg.getToRecipients().add("email");
    msg.send();
} catch (Exception e) {
    e.printStackTrace();
}

}

还有我的依赖:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.0.0'
//compile 'org.apache.httpcomponents:httpclient:4.3'
//compile 'org.apache.httpcomponents:httpcore:4.3.3'
//compile 'commons-logging:commons-logging:1.2'
// compile 'joda-time:joda-time:2.7'
     compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}

我目前正在使用 mac(如果屏幕截图没有泄露的话),并且如 Android 应用程序上的 Android Studio 中所述。

【问题讨论】:

  • @dhke 尝试在评论部分导入一个,现在得到:Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
  • 带有有趣信息的错误消息很可能略高于此。
  • @dhke com.android.dex.DexException: Multiple dex files define Lorg/apache/http/Consts; 越来越近了!

标签: java android android-studio apache-httpclient-4.x


【解决方案1】:

问题源于尝试在 android 上使用与操作系统附带的不同(通常是较新)版本的 apache httpcomponents。

这些版本通常与 API 不兼容,但使用相同的类名导致可怕

Multiple dex files define Lorg/apache/http/Consts;

或类似的编译时错误消息。

一般来说,尝试在 android 上使用与现有 android 提供的不同版本的 httpcomponents 时会遇到很多问题。

库存的 android 版本似乎没有指定,但大多数消息来源似乎都同意它基于 httpcomponents-4.0.1 的修改版本。然而,android SDK 包含 4.1.1 的版本 httpclient(在 tools/lib/httpclient-4.1.1.jar 中)。

我认为最常见的解决方案是避免使用较新版本的 httpcomponents。

Apache 现在还提供了httpclient 的自定义版本。但是,正如您自己指出的那样,这仍然会导致问题。

当您需要 httpcomponents 的其他子部分时,问题会变得更糟,例如 httpmime 会拉入各自版本的 httpcomponent jars。

【讨论】:

  • 啊,软糖,所以你看不到我的问题的解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 2016-10-11
  • 2023-01-27
  • 1970-01-01
相关资源
最近更新 更多