较旧的Android SDK版本上的java.lang.NoClassDefFoundError [英] java.lang.NoClassDefFoundError on older Android SDK versions

查看:65
本文介绍了较旧的Android SDK版本上的java.lang.NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Play上发布了我的应用程序的一个版本,并于今天早晨与许多不满意的客户一起醒来.该应用程序的最新版本集成了对蓝牙低功耗(BTLE)心率监测器的支持.

I released a version of my app to the Google Play and woke up this morning with a number of unhappy customers. The latest version of the app integrates support for a Bluetooth Low Energy (BTLE) heart rate monitor.

该应用在Android 4.3和4.4上运行良好,但在4.0、4.1和4.2上崩溃,并显示以下错误.

The app runs fine on Android 4.3 and 4.4 but crashes on 4.0, 4.1, and 4.2 with the following error.

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.eiref.boatcoach.MainActivity
    at com.eiref.boatcoach.WhatToDo.onClick(WhatToDo.java:274)
    at android.view.View.performClick(View.java:4204)
    at android.view.View$PerformClick.run(View.java:17355)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:152)
    at android.app.ActivityThread.main(ActivityThread.java:5132)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

在类似于以下内容的简单Onclick中创建Intent时发生错误……

The error occurs when creating an Intent in a simple Onclick similar to the following…

public void onClick(View v) {
        Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
}

在急忙购买4.2平板电脑以便我可以重现该问题之后,我得出的结论是,它与支持Bluetooth LE的新版应用有关,该新版本已在SDK 4.3及更高版本中启用.如果我删除MainActivity中所有对Bluetooth的引用,则崩溃将在4.2和更早版本的设备上消失.

After rushing out and buying a 4.2 tablet so I can replicate the issue, I’ve come to the conclusion that it has to do with this new version of the app supporting Bluetooth LE, which is enabled in SDK 4.3 and later. If I delete all references to Bluetooth in MainActivity then the crash goes away on 4.2 and earlier devices.

通过阅读文档,我的理解是,只要编写一个应用程序,它就可以运行具有旧版设备的蓝牙LE功能,只要小心不要执行以下代码即可执行BTLE代码...

My understanding from reading the documentation was that one could write an app that includes Bluetooth LE functionality and it would run on older devices, just so long as one was careful not to execute BTLE code using something like the following...

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) return;
    BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    mBluetoothAdapter = manager.getAdapter();
//etc.

因此,我的manifest.xml不包含以下内容,因为它会阻止下载到较旧的设备,并且我显然想尽可能地维护单个代码库...

Therefore my manifest.xml does not include the following as it would prevent download to older devices and I obviously want to maintain a single code base if possible ...

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />    

第一个问题,我的上述假设是否能够在4.3之前的SDK中包含BTLE代码?如果没有,我是否真的需要创建该应用程序的两个版本...一个供使用4.3及更高版本的用户使用,一个供其他所有人使用?

First question, is my assumption above being able to include BTLE code in SDKs prior to 4.3 correct? If not, do I really need to create two versions of the app... one for folks using 4.3 and later and one for everyone else?

有很多关于java.lang.NoClassDefFoundError的StackOverflow帖子,我想我已经阅读了大多数相关文章.许多建议我检查Java Build Path,以确保检查了Android Private Libraries和Android Dependencies.他们是.有些人建议将gen文件夹移到src文件夹之前,但这似乎没有什么作用.

There are many StackOverflow posts about java.lang.NoClassDefFoundError and I think I've read most of the relevant ones. Many suggest that I examine the Java Build Path to make sure that Android Private Libraries and Android Dependencies are checked. They are. Some suggest moving the gen folder before the src folder, but this doesn't seem to make a difference.

我将发布Eclipse Java构建路径的图像,但是由于这是我的第一篇文章,所以我没有插入图像所需的10个信誉点,所以这是我关注的另一篇文章... Android java.lang.NoClassDefFoundError

I'd post an image of the Eclipse Java Build Path but as this is my first post I don't have the 10 reputation points needed to insert an image, so here's another post that I followed... Android java.lang.NoClassDefFoundError

那么,第二个问题,还有其他关于构建路径可能有什么问题的想法吗?

So, second question, any other thoughts about what could be wrong with the build path?

非常感谢.

更新...通过某种方式提出一个问题,我得到了足够的分数来发布Java构建路径的图像.正如@Ashoke指出的那样,我确实认为这与错误的构建路径或支持库有关.

Update... somehow by asking a question I got enough points to post images of the java build path. As @Ashoke points out I do think it has something to do with the wrong build path or support libraries.

推荐答案

尝试将所有与Ble相关的代码放入一个单独的类中,只有在具有必需API级别的设备上,您才可以实例化.我认为,如果没有此回调,可能会导致您遇到问题.

Try getting all of the Ble related code into a separate class that you only instance at all if on a device with the necessary API levels. I think without this the call-backs could be leading to your issues.

这篇关于较旧的Android SDK版本上的java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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