ClassNotFoundException的时候,包括库JAR [英] ClassNotFoundException when including a library jar

查看:219
本文介绍了ClassNotFoundException的时候,包括库JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid项目中,我添加了一个库JAR到文件夹。 这个jar包含三个不同的软件包:

In my android project I added a library jar into the libs folder.
This jar contains three different packages:

  1. 在谷歌GSON(com.google.gson)
  2. 在JBoss的网状(org.jboss.netty)
  3. 在我自己开发的,它使用这两个库的客户端应用程序。 (com.example.core)

将广口编译/存档使用蚂蚁。

The jar is compiled/archived using ant.

有在Eclipse中没有错误,但是当我尝试到设备,我得到这个上运行我的Andr​​oid应用程序:

There are no errors in eclipse, but when I try to run my android app on a device I get this:

E/AndroidRuntime(23807): FATAL EXCEPTION: main
E/AndroidRuntime(23807): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.project.MainActivity" on path: /data/app/com.example.project-1.apk
E/AndroidRuntime(23807):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
E/AndroidRuntime(23807):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(23807):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(23807):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(23807):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(23807):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(23807):    at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(23807):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23807):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(23807):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(23807):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(23807):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(23807): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.project.MainActivity" on path: /data/app/com.example.project-1.apk
E/AndroidRuntime(23807):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
E/AndroidRuntime(23807):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(23807):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(23807):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
E/AndroidRuntime(23807):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
E/AndroidRuntime(23807):    ... 11 more

和它绝对是我的瓶子,是造成这一点,因为如果我请从MainActivity内引用它的类,然后解决问题和应用程序在设备上运行。

And it's definitely my jar that is causing this, since if I remove references to it's classes from within the MainActivity then the problem is solved and the app runs on the device.

使用 apktool 我检查,看看有什么进入APK,我发现一切被添加到 smali 目录,即是一切,除了我的包(com.example.core)。
因此,所有从我的罐子Android应用程序类(com.example.project),该GSON(com.google.gson)和网状(org.jboss.netty)是有,但不是com.example.core。

Using apktool I checked to see what goes into the apk, and I found that everything is being added to the smali directory, that is everything except for my package (com.example.core).
So all of the android app classes (com.example.project), the gson (com.google.gson) and netty (org.jboss.netty) from my jar are there but not com.example.core.

我的环境:
Eclipse的4.2.1
ADT 21.0.1
Java 7中(Oracle)的

My environment:
Eclipse 4.2.1
ADT 21.0.1
Java 7 (oracle)

我的设备上测试:
三星Galaxy S2
LG承上启下4

Devices I tested on:
Samsung galaxy S2
LG nexus 4

任何想法? 谢谢你。

推荐答案

好了,我终于找到了问题,我花了一段时间。

Ok, I finally managed to find the problem, it took me a while.

我使用的Java 7我的机器上的蚂蚁建造者使用此默认版本为好,但不幸的是,Android不喜欢Java 7的,所以这个固定我的问题:

I am using java 7 on my machine and the ant builder uses this default version as well, unfortunately though, android does not like java 7, and so this fixed the problem for me:

<javac srcdir="." destdir="bin/classes" source="1.6" target="1.6">
    ...
</java>

或者你可以在Eclipse中使用以下步骤:

Or you can use these steps in Eclipse:

Eclipse menu: Window/Preferences/Java/Compiler
Compiler Compliance level: 1.6 
tick "use default" 

希望,这将节省一些时间,其他人谁正面临着这个问题。

Hope that this will save some time for others who are facing this problem.

这篇关于ClassNotFoundException的时候,包括库JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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