Android studio many error: Could not find class 'android.XXX'

Android studio many error: Could not find class 'android.XXX'

我正在使用 Android Studio 2.1.2,调试设备 android 4.4.2 API19,构建环境:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
}

我已尝试重新打开项目、使缓存无效、禁用 instantRun,但我仍然不断收到如下错误:

06-24 01:15:08.302 27320-27320/org.linphone E/InstantRun: Could not find slices in APK; aborting.
06-24 01:15:08.322 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.322 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.322 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.322 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.332 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.332 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.332 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.342 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.342 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.352 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method org.linphone.LinphoneLauncherActivity.access$super
06-24 01:15:08.352 27320-27320/org.linphone E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method org.linphone.LinphoneLauncherActivity.access$super

有人能帮帮我吗?

在阅读许多类似的问题时,我发现启用 Multidex 可能会解决此问题,归功于 。他还发布了一些我推荐阅读的有用链接。至少它对我有用(准确地说:我现在只剩下这些错误中的 1 个,而以前有数百个)!

简而言之:只需在 gradle defaultConfig 中添加 multiDexEnabled true 并添加此依赖项 compile 'com.android.support:multidex:1.0.1' 即可启用 multidex。 最后,通过将这段代码添加到您的应用程序来安装 Multidex class:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

当然另一种选择是防止 64K 方法限制,因此您不再需要 MultiDex。您可以通过减少 Gradle 文件中(未使用的)依赖项的数量,或使用更具体的依赖项(wittyurchin 在 中提供了 google 播放服务的一个很好的示例来做到这一点).

但是,如果您确实需要 Multidex,那么您可能会遇到一些问题,例如我发现的问题:

1) Instant 运行 在构建目标 API 设备时被禁用(当 运行 从 Android 运行您的应用时,您会看到弹出的错误消息工作室)。

2) 如果您使用 Robolectric 进行单元测试,您可能无法再进行 运行 测试。您可以通过扩展之前的 MultiDex.install(this); 代码来解决此问题。与其自己解释一切,不如检查问题,并通过 here.

回答 sschuberth 会更容易

...

ps。似乎我不一定需要 compile 'com.android.support:multidex:1.0.1' 才能让 MultiDex 正常工作,但是,我看到很多建议说它是必需的。如果有人对此有更多建议,请成为我的客人!

我遇到了同样的问题,几乎相同的配置(和 Android API19-4.4 调试设备)。

概述在我的案例中修复错误的步骤。 (基于建议 here、缩小和 Proguard 规则):

  1. 更新了gradle版本(只需在File->settings->Build,Execution, Deployment->Build Tools->Gradle ::(RightPanel) 中设置默认选项 ::(RightPanel) ->使用默认的 gradle 包装器)

    if you have to use the particular version of gradle, you can try skipping this step

  2. 清理项目 + 重建项目(如果你也想重新启动 Android Studio 可能是这样)
  3. 从模块(应用程序)中删除依赖项build.gradle -- 确保复制依赖项值以在下一步恢复
  4. Sync/Gradle 构建,获取错误(只是一个安全保护步骤,有趣但有时出于某种原因 Android 即使配置已更改,studio 也是独立的)
  5. 在同一个模块 (app)build.gradle `

    中添加步骤 3 中记录的依赖项

    well if you are wondering why steps 3,4,5-> someone pointed out here that gradle update after dependency inclusion might cause issues resulting into these errors`

  6. 最后也是最重要的:压缩代码并使用progaurd规则官方解释here

    As I am novice to Android Studio, felt that my project, though a very small app, might be inflated by multiple experimental library inclusions + so many comments(and inactive code inside) causing 64K?(nopeIguess),illegitimate references(possibly!); just a intuitive guess after reading answer by @PKuijpers(thanks!). And honestly I didn't wanted to include multiDex because after also reading official document, I was sure that my app is not PokemonGo or NFS or big enough to qualify for multidex.

帮我解决了,呸!作为副产品,我在 Android 资源监视器中也看到了更好的内存利用率!