外部JAR添加到Eclipse中的Andr​​oid项目 [英] Add external jar to android project in eclipse

查看:165
本文介绍了外部JAR添加到Eclipse中的Andr​​oid项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立在Eclipse中简单的Andr​​oid项目,该项目使用谷歌的API-Java的客户端。我试图按照本教程得到它启动和运行。我已经通过搜索所以如何JAR添加到Eclipse中的Andr​​oid项目的答案。他们中的大多数建议添加JAR文件到的lib / 文件夹中的Andr​​oid项目里面,然后将这些JAR文件到项目的构建路径。我做这两件事情。该项目编译好(在任何情况下,Eclipse不抱怨任何错误)。但是,当我在模拟器中运行Android应用程序,我得到 ClassDefNotFoundError 每当我尝试实例中的谷歌API的Java的客户端的任何类 JAR。例如:

 新com.google.api.client.http.apache.ApacheHttpTransport();

加薪 ClassDefNotFoundError

下面是code导致错误:

 包com.mycom.android;进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.EditText;公共类SearchRunner实现OnClickListener {    私有静态最后弦乐API_KEY =AIzaSyA1Mg3xWXfoov4HdPUzYY2NwTxuvCev1-E;
    私有静态最后弦乐PLACES_SEARCH_URL =;    @覆盖
    公共无效的onClick(视图v){
        EDITTEXT的EditText =(EditText上)V;
        。字符串SEARCHTEXT = editText.getText()的toString();
        runSearch(SEARCHTEXT);
    }    保护无效runSearch(字符串SEARCHTEXT){
        新com.google.api.client.http.apache.ApacheHttpTransport();
    }}

下面是Eclipse的LogCat中一个更完整的输出:

  15 12-21:42:11.402:E / dalvikvm(3412):找不到类的com.google.api.client.http.apache.ApacheHttpTransport',从引用方法com.mycom.android.SearchRunner.runSearch
12-21 15:42:11.402:W / dalvikvm(3412):VFY:无法解析新实例70(LCOM /谷歌/ API /客户/ HTTP /阿帕奇/ ApacheHttpTransport;)在LCOM / MYCOM /安卓/ SearchRunner;
12-21 15:42:11.402:D / dalvikvm(3412):VFY:在更换运code 0x22为0x0000
12-21 15:42:11.402:D / dalvikvm(3412):DexOpt:无法在0X02到选择直接调用0x00ca在LCOM / MYCOM /安卓/ SearchRunner; .runSearch
12-21 15:42:11.682:I / MapActivity(3412):处理网络更改通知:已连接
12-21 15:42:11.682:E / MapActivity(3412):无法获取连接工厂客户端
12-21 15:42:11.791:D / gralloc_goldfish(3412):无仿真模拟GPU检测。
12-21 15:42:12.532:D / dalvikvm(3412):GC_CONCURRENT释放102K,免费3%10520K / 10823K,暂停为5ms + 7毫秒
12-21 15:42:18.422:D / AndroidRuntime(3412):关闭VM
12-21 15:42:18.422:W / dalvikvm(3412):主题ID = 1:螺纹未捕获的异常(组= 0x409951f8)退出
12-21 15:42:18.482:E / AndroidRuntime(3412):致命异常:主要
12-21 15:42:18.482:E / AndroidRuntime(3412):java.lang.NoClassDefFoundError的:com.google.api.client.http.apache.ApacheHttpTransport
12-21 15:42:18.482:E / AndroidRuntime(3412):在com.mycom.android.SearchRunner.runSearch(SearchRunner.java:20)


解决方案

我按照解决问题的这些说明一步逐步。当我最初开始设置项目:


  1. 我导入的JAR文件从谷歌API的Java的客户端项目到我的项目的的lib / 文件夹

  2. 我右键单击的lib /谷歌的API客户端... 和选择构建路径>添加到构建路径

在这一点上,在Eclipse中,一束库出现在我的项目的引用的库部分:

  jsr305-1.3.9.jar
GSON-1.7.1.jar
番石榴r09.jar
JUnit的-4.8.2.jar
HttpClient的-4.0.3.jar
的HttpCore-4.0.1.jar
的commons-logging-1.1.1.jar
commons- codeC-1.3.jar
杰克逊核心ASL-1.9.1.jar
xpp3-1.1.4c.jar
protobuf的-Java的2.2.0.jar
谷歌-HTTP客户端-1.6.0-beta.jar
谷歌-OAuth的客户端1.6.0-beta.jar
谷歌的API客户端-1.6.0-beta.jar

然而,当我去项目>属性> Java构建路径>库,我看到只有谷歌API的客户端... 上市。当我试图运行该项目,我得到了 NoClassDefFounError ApacheHttpTransport ,因为在原来的文章中提到。这是因为类是在定义谷歌的HTTP客户端!通过手动添加谷歌HTTP客户端来构建路径,问题就解决了​​。

经验教训:仅仅因为一个JAR下Eclipse项目的引用的库上市并不意味着这些JAR文件都包含在项目的构建路径

I am building a simple Android project in eclipse that uses the google-api-java-client. I'm trying to follow this tutorial to get it up and running. I've searched through SO for answers on how to add JARs to an Android project in Eclipse. Most of them recommend adding the JARs into the lib/ folder inside the Android project, and then adding those JARs to the project's build path. I've done these two things. The project compiles fine (in any case, Eclipse doesn't complain about any errors). But when I run the Android app in my emulator, I get ClassDefNotFoundError whenever I try to instantiate any class in the google-api-java-client JAR. For example

new com.google.api.client.http.apache.ApacheHttpTransport();

Raises ClassDefNotFoundError.

Here is the code that causes the error:

package com.mycom.android;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class SearchRunner implements OnClickListener {

    private static final String API_KEY = "AIzaSyA1Mg3xWXfoov4HdPUzYY2NwTxuvCev1-E";
    private static final String PLACES_SEARCH_URL = "";

    @Override
    public void onClick(View v) {
        EditText editText = (EditText) v;
        String searchText = editText.getText().toString();
        runSearch(searchText);
    }

    protected void runSearch(String searchText) {
        new com.google.api.client.http.apache.ApacheHttpTransport();
    }

}

Here is a more complete output from Eclipse LogCat:

12-21 15:42:11.402: E/dalvikvm(3412): Could not find class 'com.google.api.client.http.apache.ApacheHttpTransport', referenced from method com.mycom.android.SearchRunner.runSearch
12-21 15:42:11.402: W/dalvikvm(3412): VFY: unable to resolve new-instance 70 (Lcom/google/api/client/http/apache/ApacheHttpTransport;) in Lcom/mycom/android/SearchRunner;
12-21 15:42:11.402: D/dalvikvm(3412): VFY: replacing opcode 0x22 at 0x0000
12-21 15:42:11.402: D/dalvikvm(3412): DexOpt: unable to opt direct call 0x00ca at 0x02 in Lcom/mycom/android/SearchRunner;.runSearch
12-21 15:42:11.682: I/MapActivity(3412): Handling network change notification:CONNECTED
12-21 15:42:11.682: E/MapActivity(3412): Couldn't get connection factory client
12-21 15:42:11.791: D/gralloc_goldfish(3412): Emulator without GPU emulation detected.
12-21 15:42:12.532: D/dalvikvm(3412): GC_CONCURRENT freed 102K, 3% free 10520K/10823K, paused 5ms+7ms
12-21 15:42:18.422: D/AndroidRuntime(3412): Shutting down VM
12-21 15:42:18.422: W/dalvikvm(3412): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
12-21 15:42:18.482: E/AndroidRuntime(3412): FATAL EXCEPTION: main
12-21 15:42:18.482: E/AndroidRuntime(3412): java.lang.NoClassDefFoundError: com.google.api.client.http.apache.ApacheHttpTransport
12-21 15:42:18.482: E/AndroidRuntime(3412):     at com.mycom.android.SearchRunner.runSearch(SearchRunner.java:20)

解决方案

I solved the issue by following these instructions step-by-step. When I initially began setting up the project:

  1. I imported the JAR files from the google-api-java-client project into my project's lib/ folder
  2. I right-clicked lib/google-api-client... and selected Build Path > Add To Build Path

At this point, in Eclipse, a bunch of libraries appear under my project's "Referenced Libraries" section:

jsr305-1.3.9.jar
gson-1.7.1.jar
guava-r09.jar
junit-4.8.2.jar
httpclient-4.0.3.jar
httpcore-4.0.1.jar
commons-logging-1.1.1.jar
commons-codec-1.3.jar
jackson-core-asl-1.9.1.jar
xpp3-1.1.4c.jar
protobuf-java-2.2.0.jar
google-http-client-1.6.0-beta.jar
google-oauth-client-1.6.0-beta.jar
google-api-client-1.6.0-beta.jar

However, when I went to Project > Properties > Java Build Path > Libraries, I saw that only google-api-client... was listed. When I tried to run the project, I got the NoClassDefFounError for ApacheHttpTransport, as mentioned in the original post. This is because that class is defined in google-http-client!. By manually adding google-http-client to the build path, the problem is solved.

Lesson learned: just because a JAR is listed under an Eclipse project's "Referenced Libraries" does not mean those JARs are included in the project's build path.

这篇关于外部JAR添加到Eclipse中的Andr​​oid项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆