【问题标题】:java.lang.RuntimeException: Unable to instantiate application. ClassNotFoundException: Didn't find classjava.lang.RuntimeException:无法实例化应用程序。 ClassNotFoundException:找不到类
【发布时间】:2017-09-16 20:51:13
【问题描述】:

我知道这个问题已经被问过一百万次了,但是我找不到针对我具体情况的答案。我有一个包含所有代码的库,以及几个导入该库的其他模块。

-project
--mylibrary
---sr/main/java
----co/android/mylibrary
----BaseApp (extends MultidexApp)
--Application1
---sr/main/java
----co/android/app2
-----Android Manifest
--Application2
---sr/main/java
----co/android/app2
-----Android Manifest

两个清单都像这样使用基本应用程序。

<application
    android:name="co.android.mylibrary.BaseApp"
    android:allowBackup="false"
    android:fullBackupContent="false"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/AppTheme"
    tools:replace="android:icon,android:theme, android:allowBackup">

构建依赖项如下所示:

dependencies {
  releaseCompile project(path: ':mylibrary', configuration: 'release')
  debugCompile project(path: ':mylibrary', configuration: 'debug')
}

我的基础应用初始化multidex的方法:

protected void attachBaseContext(Context base) {   
    super.attachBaseContext(base);
    try {
      MultiDex.install(this);
    }catch (RuntimeException e){}
}

我在两个位置(库和应用程序)都添加了一些 proguard 规则。这些不包括 3rd 方库和我自己的一些类的一些规则。

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.view.View 
-keep public class co.android.mylibrary.data.greendao.**{ *; }

因此,该应用程序在我的 s8 上运行良好,但在 moto G 等某些手机上却无法正常运行。如果启用 Proguard 并且其资源不断减少(例如发布版本),它们也可以正常运行。我注意到的另一个奇怪行为是,当我在代码的某些部分设置断点并运行发布版本(将 debuggable 设置为 true)时,它会在 s8 上中断,但在 moto 上不会中断。

为什么会有这种奇怪的行为?另一个我发现超级相似的问题是unable to instantiate application - ClassNotFoundException。但仍然没有解决方案。

这是错误的完整日志。忽略包名。

编辑

根据@Mostafa Anter 的建议,更改了我在应用程序上编译库的方式后:

compile project(path: ':mylibrary', configuration: 'debug')

它开始给我这个错误。我关闭了即时跑步功能。

【问题讨论】:

  • 你能显示 ClassNotFoundException 的 logcat 详细信息吗?

标签: android firebase gradle android-multidex


【解决方案1】:

使基类扩展Application 然后在 onCreate 方法中调用这一行MultiDex.install(this);

修改模块级 build.gradle 文件以启用 multidex 并将 multidex 库添加为依赖项,如下所示:

android {
defaultConfig {
    ...
    minSdkVersion 15 
    targetSdkVersion 26
    multiDexEnabled true
}
...
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
}

【讨论】:

  • 你在使用 proguard 吗?如果是,请向我们提供规则
  • 尝试将此行添加到 progaurd -keep public class your_missing_class_full_name
  • 没有。那没有帮助。我添加了类似 -keep public class co.android.mylibrary.BaseApp 的内容。此外,这是在调试版本中,我认为这甚至不重要,因为收缩被禁用。
  • 启用收缩后,它会运行应用程序。但不会在我想要的某些代码处中断,这会在某些设备上导致奇怪的行为。
  • 尝试用 compile project(':mylibrary') 替换 releaseCompile project(path: ':mylibrary', configuration: 'release') debugCompile project(path: ':mylibrary', configuration: 'debug') ')
【解决方案2】:

如果您使用 JavaVersion.VERSION_1_8,请确保在所有模块中都使用它

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-31
    • 2013-04-12
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多