简体   繁体   English

致命异常:在 Android 应用程序中调用静态方法时出现 java.lang.NoClassDefFoundError

[英]Fatal Exception: java.lang.NoClassDefFoundError when calling static method in Android app

One of our beta testers using a T-mobile Galaxy S4 running Android 4.4.4 is experiencing very strange NoClassDefFoundError crashes.我们的一位使用运行 Android 4.4.4 的 T-mobile Galaxy S4 的 Beta 测试人员遇到了非常奇怪的 NoClassDefFoundError 崩溃。 I'm completely at a loss for why these could be happening.我完全不知道为什么会发生这些。

The most recent one happens when a static method is called in onCreateView in a fragment.最近一次发生在片段中的onCreateView中调用静态方法时。

MyCustomClass.getNumberOfItems(getActivity());

fails with失败

Fatal Exception: java.lang.NoClassDefFoundError com.mycompany.myapp.utilities.MyCustomClass$5致命异常:java.lang.NoClassDefFoundError com.mycompany.myapp.utilities.MyCustomClass$5

Here's the method in question:这是有问题的方法:

public static List<Item> getNumberOfItems(final Context context)
{
    List<Item> allFinishedItems = getAllFinishedItems(context);

    CollectionUtils.filter(allFinishedItems, new Predicate<Item>()
    {
        @Override
        public boolean evaluate(Item item)
        {
            // TODO Auto-generated method stub
            return isNonCustomItem(context, item);
        }
    });

    return allFinishedItems;
}

1.) what is the "$5" after the class name? 1.) 类名后面的“$5”是什么? (answer: reference to anonymous class for filtering with CollectionUtils.filter) 2.) this user had another crash of similar nature, but with a completely different static method call that's in a library that's included via gradle. (答案:引用匿名类以使用 CollectionUtils.filter 进行过滤) 2.) 该用户发生了另一次类似性质的崩溃,但具有完全不同的静态方法调用,该调用位于通过 gradle 包含的库中。 The crash I'm referencing in this question is coming from code that is part of my own library project, and the same static method call works in other places in the app.我在这个问题中引用的崩溃来自作为我自己的库项目一部分的代码,并且相同的静态方法调用在应用程序的其他地方有效。 This problem seems to be spreading, or at least not contained to 1 class or 1 library.这个问题似乎正在蔓延,或者至少没有包含在 1 个类或 1 个库中。

We're targeting the following Android versions in build.gradle:我们在 build.gradle 中针对以下 Android 版本:

minSdkVersion 14
targetSdkVersion 22  (android 5.1)

What could possibly be going on here?这里可能会发生什么? One other thing to note is that the signed APKs were generated using Android Studio 2.0 preview 4. However, the app works fine for 20-30 other beta testers, so I'm hesitant to point the finger at using a preview version of Studio.另一件需要注意的事情是,签名的 APK 是使用 Android Studio 2.0 预览版 4 生成的。但是,该应用程序适用于 20-30 名其他 Beta 测试人员,因此我犹豫是否使用 Studio 的预览版。

The NoClassDefFoundError s were happening because multidex was only partially implemented in this app - for some reason, this works fine on Android 5/6 but on Android 4.x it makes your app crash with NoClassDefFoundError in random places. NoClassDefFoundError s 的发生是因为 multidex 仅在此应用程序中部分实现 - 出于某种原因,这在 Android 5/6 上运行良好,但在 Android 4.x 上它会使您的应用程序在随机位置因NoClassDefFoundError崩溃。 It seems to me like this situation should be detected by Android Studio and you should be warned you've improperly implemented multidex support.在我看来,这种情况应该由 Android Studio 检测到,你应该被警告你没有正确实现 multidex 支持。

To fix it, make sure the following is true for your project:要修复它,请确保您的项目符合以下条件:

  1. multiDexEnabled = true in the defaultConfig section of your app-level build.gradle multiDexEnabled = true在应用级 build.gradle 的defaultConfig部分

  2. compile 'com.android.support:multidex:1.0.0' in your project-level build.gradle在您的项目级 build.gradle 中compile 'com.android.support:multidex:1.0.0'

  3. Call MultiDex.install() in attachBaseContext() of your Application class.呼叫MultiDex.install()attachBaseContext()应用程序类的。

More details on multidex can be found here.可以在此处找到有关 multidex 的更多详细信息

One of our beta testers using a T-mobile Galaxy S4 running Android 4.4.4 is experiencing very strange NoClassDefFoundError crashes.我们使用运行Android 4.4.4的T-mobile Galaxy S4的Beta测试人员之一遇到了非常奇怪的NoClassDefFoundError崩溃。 I'm completely at a loss for why these could be happening.我完全不知道为什么会发生这些事情。

The most recent one happens when a static method is called in onCreateView in a fragment.最近的一个事件是在片段的onCreateView中调用静态方法时发生的。

MyCustomClass.getNumberOfItems(getActivity());

fails with失败于

Fatal Exception: java.lang.NoClassDefFoundError com.mycompany.myapp.utilities.MyCustomClass$5致命异常:java.lang.NoClassDefFoundError com.mycompany.myapp.utilities.MyCustomClass $ 5

Here's the method in question:这是有问题的方法:

public static List<Item> getNumberOfItems(final Context context)
{
    List<Item> allFinishedItems = getAllFinishedItems(context);

    CollectionUtils.filter(allFinishedItems, new Predicate<Item>()
    {
        @Override
        public boolean evaluate(Item item)
        {
            // TODO Auto-generated method stub
            return isNonCustomItem(context, item);
        }
    });

    return allFinishedItems;
}

1.) what is the "$5" after the class name? 1.)类名后面的“ $ 5”是什么? (answer: reference to anonymous class for filtering with CollectionUtils.filter) 2.) this user had another crash of similar nature, but with a completely different static method call that's in a library that's included via gradle. (答案:引用匿名类以使用CollectionUtils.filter进行过滤)2.)此用户发生了另一次类似的崩溃,但是通过gradle包含在库中的静态方法调用完全不同。 The crash I'm referencing in this question is coming from code that is part of my own library project, and the same static method call works in other places in the app.我在此问题中引用的崩溃来自我自己的库项目中的代码,并且相同的静态方法调用在应用程序的其他位置也有效。 This problem seems to be spreading, or at least not contained to 1 class or 1 library.这个问题似乎正在蔓延,或者至少没有包含在1个类或1个库中。

We're targeting the following Android versions in build.gradle:我们在build.gradle中定位以下Android版本:

minSdkVersion 14
targetSdkVersion 22  (android 5.1)

What could possibly be going on here?这可能是怎么回事? One other thing to note is that the signed APKs were generated using Android Studio 2.0 preview 4. However, the app works fine for 20-30 other beta testers, so I'm hesitant to point the finger at using a preview version of Studio.还要注意的另一件事是,已签名的APK是使用Android Studio 2.0预览版4生成的。但是,该应用程序可用于其他20-30个Beta测试人员,因此我犹豫要使用Studio的预览版。

Make sure to setup the pre-requisites as others answer pointed out here, if the crash still occurred you might want to try this approach as well https://developer.android.com/studio/build/multidex#keep确保按照其他人的回答在此处设置先决条件,如果仍然发生崩溃,您可能也想尝试这种方法https://developer.android.com/studio/build/multidex#keep

When building each DEX file for a multidex app, the build tools perform complex decision-making to determine which classes are needed in the primary DEX file so that your app can start successfully.为 multidex 应用程序构建每个 DEX 文件时,构建工具会执行复杂的决策,以确定主 DEX 文件中需要哪些类,以便您的应用程序可以成功启动。 If any class that's required during startup is not provided in the primary DEX file, then your app crashes with the error java.lang.NoClassDefFoundError如果主 DEX 文件中未提供启动期间所需的任何类,则您的应用程序将崩溃并显示错误 java.lang.NoClassDefFoundError

使用混淆器

EDITED:编辑:

Or you can try this approach for ease of use registering your classes in the primary DEX或者您可以尝试这种方法,以便在主要 DEX 中注册您的课程

https://medium.com/androidiots/androidiots-podcast-6-the-black-magic-behind-android-runtime-part-2-9390f751eef0 https://medium.com/androidiots/androidiots-podcast-6-the-black-magic-behind-android-runtime-part-2-9390f751eef0

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

相关问题 异常java.lang.NoClassDefFoundError android / app / Activity - Exception java.lang.NoClassDefFoundError android/app/Activity 具有静态final方法的java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError with static final method 在Android上运行libgdx应用时出现java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError when running libgdx app on Android 使用Firebase时Android应用错误java.lang.NoClassDefFoundError - Android app error java.lang.NoClassDefFoundError when using firebase java.lang.noclassdeffounderror android应用 - java.lang.noclassdeffounderror android app Android应用错误:java.lang.NoClassDefFoundError - Android App Error: java.lang.NoClassDefFoundError Android应用程序崩溃与java.lang.NoClassDefFoundError - Android app crashes with java.lang.NoClassDefFoundError 致命异常:main java.lang.NoClassDefFoundError:org.apache.commons.net.ftp.FTPClient Android Studio - FATAL EXCEPTION: main java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient Android Studio 致命异常:主要java.lang.NoClassDefFoundError:com.google.analytics.tracking.android.EasyTracker - FATAL EXCEPTION: main java.lang.NoClassDefFoundError: com.google.analytics.tracking.android.EasyTracker 致命异常:main java.lang.NoClassDefFoundError:外部library.jar.class Android Studio - FATAL EXCEPTION: main java.lang.NoClassDefFoundError: external library.jar.class Android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM