NoClassDefFoundError 没有意义

NoClassDefFoundError no sense

我发现 Android 应用程序存在问题。

我的问题是当我打开应用程序时它崩溃并给出 NoClassDefFoundError。 这是堆栈跟踪:

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[=10=]
    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)
    [...]

build.gradle 文件包含以下行:

// 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'

FormatDetectorclass是这样的

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);
}

JsonFormat class 有此声明

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);
}

我用谷歌搜索了这个错误,我发现这可能是由于 class 路径中缺少一些 classes。 出于这个原因,我用 Android Studio 分析器分析了 apk,我发现在 classes.dex 中有包 com.electronwill.nightconfig.corecom.electronwill.nightconfig.json,这是我唯一的两个包正在使用。

我已经在许多 phone 上调试了该应用程序,唯一导致问题的是 Android 5.1.1。我不知道 Android 其他版本是否会导致问题,但我认为 Android 版本不是核心问题。

有人可以帮我解决这个问题吗?任何人都知道为什么即使使用 gradle 并且只有一个 phone 我也会收到此错误?

我不太了解gradle,但是如果我查看maven 存储库,也许依赖关系应该被标注为:

compile group: 'com.electronwill.night-config', name: 'core', version: '3.3.1'

参见:

https://mvnrepository.com/artifact/com.electronwill.night-config/core/3.3.1

您正在使用的库使用了您当前设置不支持的 Java 8 功能 (lambda)。问题出在这一行:

registry.put(fileExtension, () -> format);

您将在 Android in the official docs 中找到有关如何使用 Java 8 项功能的分步信息。这通常意味着将 Android 插件升级到 3.0.0 或更高版本并更改 build.gradle:

中的源和目标兼容性
android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Android 不支持所有 Java 8 个功能;还有一些功能在 API 24 (Android 7).

以下不可用

我已经用不同的方法解决了这个问题。

在 db 库中,我有一个 ConfigAdapter class 用于包装 Night-Config 库中的一些功能。

我已经将这个 class 转换成这样的界面:

/**
 * Find and return value at searched key, or default.
 *
 * @param k   The key to search
 * @param def The default value if object not found
 * @param <T> The value's type
 * @return The value at {@code k} or {@code def}
 */
<T> T get(String k, T def);

/**
 * Replaces old value with new one, return old.
 *
 * @param k     The value's key
 * @param v     The value to set
 * @param <T>   The type of old value
 * @return      The old value if any, or null
 */
<T> T put(String k, T v);

/**
 * Closes the config resource.
 */
void close();

比我在我的应用程序中创建的 ConfigAdapterImpl class 更像这样

public class ConfigAdapterImpl implements ConfigAdapter {

    private SharedPreferences preferences;

    public ConfigAdapterImpl(SharedPreferences _preferences) {
        preferences = _preferences;
    }

    @Override
    public <T> T get(String s, T t) {
        Map<String, ?> all = preferences.getAll();

        // Watch out unchecked cast!

        if (all.get(s) != null)
            return (T) all.get(s);
        else return t;
    }


    @Override
    public <T> T put(String s, T t) {
        SharedPreferences.Editor editor = preferences.edit();

        T oldVal = get(s, null);

        if (t instanceof String)
            editor.putString(s, (String) t);
        else if (t instanceof Integer)
            editor.putInt(s, (Integer) t);
        else if (t instanceof Boolean)
            editor.putBoolean(s, (Boolean) t);

        editor.apply();
        return oldVal;
    }

    @Override
    public void close() {

    }
}

通过此实现,配置由 Android 首选项管理器处理,我的库像 Night-Config 对象一样使用它们。

这里是NightConfig作者。

Android studio 工具可以使用 lambda,但旧版本的 Android 没有我的 FormatDetector 使用的 Supplier class。这可能是导致错误的原因。

您应该使用 the new NightConfig *_android modules,它适用于旧的 Android API。