Search code examples
javaandroidruntime-errornoclassdeffounderror

NoClassDefFoundError no sense


I'm discovering a problem with an Android application.

My problem is that when I open the application it crashes giving NoClassDefFoundError. This is the stack trace:

09-24 19:37:14.542 32545-32545/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: it.bcv.invadelite, PID: 32545
java.lang.NoClassDefFoundError: com.electronwill.nightconfig.core.file.FormatDetector$$Lambda$0
    at com.electronwill.nightconfig.core.file.FormatDetector.registerExtension(FormatDetector.java:27)
    at com.electronwill.nightconfig.json.JsonFormat.<clinit>(JsonFormat.java:66)
    at it.bcv.invade.appdb.ConfigAdapter.<init>(ConfigAdapter.java:39)
    at it.bcv.invade.appdb.Db.<init>(Db.java:51)
    at it.bcv.invade.appdb.Db.init(Db.java:75)
    at it.bcv.invadelite.activities.StartActivity.initDatabase(StartActivity.java:165)
    at it.bcv.invadelite.activities.StartActivity.onCreate(StartActivity.java:125)
    [...]

and build.gradle file has following lines:

// https://github.com/TheElectronWill/Night-Config
implementation 'com.github.TheElectronWill.Night-Config:core:3.1.0'
implementation 'com.github.TheElectronWill.Night-Config:json:3.1.0'

The FormatDetector class is like this

private static final Map<String, Supplier<ConfigFormat<?>>> registry = new ConcurrentHashMap<>();

/**
 * Registers a ConfigFormat for a specific fileExtension.
 *
 * @param fileExtension the file extension
 * @param format        the config format
 */
public static void registerExtension(String fileExtension, ConfigFormat<?> format) {
    registry.put(fileExtension, () -> format);
}

while the JsonFormat class has this declaration

private static final JsonFormat<FancyJsonWriter> FANCY = new JsonFormat<FancyJsonWriter>() {
    @Override
    public FancyJsonWriter createWriter() {
        return new FancyJsonWriter();
    }

    @Override
    public ConfigParser<Config> createParser() {
        return new JsonParser(this);
    }
};

static {
    FormatDetector.registerExtension("json", FANCY);
}

I've googled about this error and I found that could be due to the missing of some classes in the classpath. For this reason I've analyzed the apk with Android Studio analyzer and i found that in classes.dex there are both packages com.electronwill.nightconfig.core and com.electronwill.nightconfig.json that are the only two package that I'm using.

I've debugged the application on many phones, and the only that is causing problems has Android 5.1.1. I don't know if other versions of Android can cause problems but I think that the Android version is not the core problem.

Anyone can help me to solve this please? Anyone knows why I'm getting this error even with gradle and only in one phone?


Solution

  • NightConfig author here.

    Android studio tools do work with lambdas, but older versions of Android don't have the Supplier class, which is used by my FormatDetector. This is probably what causes the error.

    You should use the new NightConfig *_android modules, which are adapted for older Android APIs.