简体   繁体   English

如何摆脱仅在调试模式下在Android应用程序中遇到的ClassNotFoundException?

[英]How to get rid of this ClassNotFoundException that is encountered in my Android app only in debug mode?

I am using Google Play Location Services in my Android app and while debugging it, the code execution stops at this ClassNotFoundException instead of the breakpoints that I have set. 我在Android应用中使用Google Play定位服务,并且在对其进行调试时,代码执行在此ClassNotFoundException处停止,而不是在我设置的断点处停止。 The class in question (ParcelableGeofence) seems to belong to the Location Services API. 有问题的类(ParcelableGeofence)似乎属于Location Services API。 I had added the Google Play Services library strictly following the instructions in the documentation and the app runs fine on the phone - this problem occurs only in the debug mode. 我已经严格按照文档中的说明添加了Google Play服务库,并且该应用在手机上运行良好-仅在调试模式下会出现此问题。

I have double checked to make sure that Google Play library project is present in Eclipse workspace as a library, is referenced by my project and the play services jar is present in the libs folder of my project. 我已仔细检查以确保Google Play库项目作为库存在于Eclipse工作区中,并已被我的项目引用,并且play services jar位于项目的libs文件夹中。 What else can we try to fix this issue or work around it? 我们还能尝试什么解决此问题或解决该问题? I am not able to debug other code because this error suspends the VM while debugging. 我无法调试其他代码,因为此错误会在调试时挂起VM。

Please see the image of my debug perspective for this error below: 请在下面查看此错误的调​​试透视图:

在此处输入图片说明

EDIT : To clarify, this error occurs when I debug on my phone that has the latest version of Google Play Services installed. 编辑 :澄清一下,当我在安装了最新版本Google Play服务的手机上调试时,会发生此错误。 So this does not have to do with the emulator not having the Play Services SDK. 因此,这与没有Play Services SDK的模拟器无关。

Source not found clearly tells you that the source code of the class is not available to show during debugging. 未找到源代码清楚地告诉您,该类的源代码在调试期间无法显示。 Google Play location services api library jar will have only the class files. Google Play位置服务api库jar仅包含类文件。 Not the exact java source file. 不是确切的Java源文件。 So you cannot step into Google Play location services api method while u debug. 因此,您在调试时无法进入Google Play位置服务api方法。 You need to step over and can debug only your methods. 您需要跨步并且只能调试您的方法。 I would suggest you to check the return from Google Play location services library methods and then search Google with the returned error code or exception. 建议您检查Google Play定位服务库方法的返回值,然后使用返回的错误代码或异常搜索Google。 Hope this will help you. 希望这会帮助你。

While you run the the projects, just the byte code is enough. 在运行项目时,仅字节码就足够了。 No need of source code. 不需要源代码。

Note: If you wanna debug Android source code then install Android source code from SDK manager. 注意:如果要调试Android源代码,请从SDK Manager安装Android源代码。 I would suggest you to install documentation also from SDK manager to understand Android api methods. 我建议您也从SDK管理器安装文档以了解Android api方法。

Eclipse does this, sometimes, when debugging Android projects. 在调试Android项目时,Eclipse有时会这样做。 It used to be far more common a few years ago (eg when debugging Android 2.2 applications it would almost always occur on launch). 它在几年前变得更加普遍(例如,在调试Android 2.2应用程序时,它几乎总是在启动时发生)。

However, it has no effect other than being annoying (ie the app does not crash), just resuming execution should work. 然而,它比烦人(即应用程序不会崩溃),其他没有任何影响,只是恢复执行应该工作。

If you breakpoints are not triggered, it's probably unrelated to this. 如果未触发断点,则可能与此无关。

Use GenyMotion emulator and install gapps 使用GenyMotion模拟器并安装间隙

http://www.genymotion.com/ http://www.genymotion.com/

for gapps 差距

https://goo.im/gapps/ (allows you to work on maps ) https://goo.im/gapps/ (允许您在地图上工作)

I often notice ClassNotFoundExceptions when debugging Android Applications. 在调试Android应用程序时,我经常会注意到ClassNotFoundExceptions。
These exceptions mainly do not cause Apps to break as it is the Class - definition 这些异常主要不会导致应用程序中断,因为它是类-定义
which is requested and the related class may not get instantiated nessecarily. 它被请求,并且相关类可能无法正确实例化。
So though it is a good idea in general to catch and avoid any exceptions, 因此,尽管一般来说捕获并避免任何异常是一个好主意,
in this case it is not of major concern. 在这种情况下,它不是主要关注的问题。 The error may result from an 该错误可能是由于
unused import left in code related to a different android version, 与不同的android版本相关的代码中未使用的导入,
or the class maybe instantiated only when run on a lower / higher target version. 或仅在较低/较高目标版本上运行时,类才能实例化。
Simple answer: Ignore it! 简单的答案:无视它!

VM don't support all of the features. VM不支持所有功能。 You must debug in real device. 您必须在真实设备中调试。

  1. Open AVD Manager (Window > Android Virtual Device Manager) 打开AVD管理器(“窗口”>“ Android虚拟设备管理器”)
  2. Create (or copy) virtual device 创建(或复制)虚拟设备
  3. Select target: Google APIs... 选择目标:Google APIs ...

  4. Start your virtual device and check "com.google.android.gms" process is there. 启动您的虚拟设备,然后检查“ com.google.android.gms”进程是否在那里。

Default virtual devices have no Google Play Services features. 默认虚拟设备没有Google Play服务功能。


If you cannot select Google APIs as target, try this. 如果您不能选择Google API作为目标,请尝试此操作。

  1. Open Android SDK Manager (Window > Android SDK Manager) 打开Android SDK Manager(窗口> Android SDK Manager)
  2. Install Google APIs (ex: Andriod 4.4.2(API19) > Google APIs(ARM..)) 安装Google API(例如:Andriod 4.4.2(API19)> Google API(ARM ..))

I've used the google API in a previous project and I remember that by default the emulator don't ship with the google service (gmail, maps, google play, etc) it's just plain Android. 我在先前的项目中使用过google API,并且我记得默认情况下,模拟器不附带google服务(gmail,地图,google play等),它只是普通的Android。 I think you must install the google service first in your emulator or try to debug on a device that has the google service installed (it's probably there by default on physical devices).. I hope this helps! 我认为您必须先在模拟器中安装google服务,或尝试在已安装google服务的设备上进行调试(默认情况下,物理设备上可能已经安装了google服务)。希望如此对您有所帮助!

Also it would help to see the actual error code. 此外,这将有助于查看实际的错误代码。 If I remember correctly I think emulators cannot use the google's maps API because they require to be OpenGL capable which they are not.. 如果我没记错的话,我认为仿真器不能使用google的maps API,因为它们需要具备OpenGL的功能,而它们却没有。

First as Amav M said: 首先,如Amav M所说:

VM don't support all of the features. VM不支持所有功能。 You must debug in real device. 您必须在真实设备中调试。

You can also use the blustacks emulator, it has google services in it. 您还可以使用blustacks模拟器,它具有google服务。

Second, you need to understand where this error is coming from. 其次,您需要了解此错误的来源。 When your code reach a class that contains reference to undefined class(in your example it's emulator that does not have google services installed), the application will crush, it does not crush when you first use the class, but when it been accessed threw the imports. 当您的代码到达包含对未定义类的引用的类时(在您的示例中为未安装google服务的模拟器),应用程序将崩溃,当您初次使用该类时,它不会崩溃,但是当访问该类时,它将进口。 This is why you will not get a row number in your debugger. 这就是为什么您不会在调试器中获得行号的原因。

To work around, either use a real device or blustacks , or test if google services is enabled and if not do not use any classes that import anything from google services apis. 要变通解决,请使用真实的设备或blustacks ,或者测试是否启用了Google服务,如果没有启用,请不要使用任何从Google Services API导入任何内容的类。

To test if class exist: 要测试类是否存在:

public boolean isClassExist(String className) {
    try {
        return Class.forName(className) != null;
    } catch (ClassNotFoundException e) {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM