【问题标题】:NoClassDefFoundError no senseNoClassDefFoundError 没有意义
【发布时间】:2018-09-24 18:03:58
【问题描述】:

我发现 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$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)
    [...]

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'

FormatDetector 类是这样的

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 类有这个声明

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

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

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

谁能帮我解决这个问题?任何人都知道为什么即使使用 gradle 并且仅在一部手机中也会出现此错误?

【问题讨论】:

    标签: java android runtime-error noclassdeffounderror


    【解决方案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) 以下不可用。

    【讨论】:

    • 对不起,我没有在上一篇文章中附上我的 gradle 配置,但是我已经包含了 1.8 java 版本的编译选项。我在应用程序的许多其他部分都使用了 lamba 表达式,但它们都没有给我错误。
    • 现在我将发布一个临时解决方案,它不能正确解决问题,但可能对其他人有用。
    【解决方案2】:

    NightConfig 作者在这里。

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

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

    【讨论】:

      【解决方案3】:

      我真的不知道 graddle,但是如果我检查 maven 存储库,也许应该将依赖关系记为:

      编译组:'com.electronwill.night-config',名称:'core',版本: '3.3.1'

      见:

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

      【讨论】:

      • 这没什么区别。
      【解决方案4】:

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

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

      我已经把这个类变成了这样的接口:

      /**
       * 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 类像这样

      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 对象一样使用它们。

      【讨论】:

        猜你喜欢
        • 2017-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-10
        • 1970-01-01
        • 2021-06-20
        • 2013-08-12
        相关资源
        最近更新 更多