本文介绍了Android studio 很多错误:找不到类'android.XXX'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Android Studio 2.1.2, debug device android 4.4.2 API19, build env:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
}

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

I have tried reopen the project, invalidate caches, disable instantRun, but I still keep getting those error as below:

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 来解决,感谢 来自巴拉特·库马尔的这个答案.他还发布了一些我推荐阅读的有用链接.至少它对我有用(准确地说:我现在只剩下其中一个错误,而之前有数百个)!

While reading through many similar problems, I found out it might be fixed by enabling Multidex, credits to this answer from Bharath Kumar. He also posted some useful links I reccomend to read. At least it worked for me (to be precise: I'm left with only 1 of those errors now, while it were hundreds before)!

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

In short: enabling multidex can be done by simply adding multiDexEnabled true in your gradle defaultConfig and adding this dependency compile 'com.android.support:multidex:1.0.1'.Finally, install Multidex by adding this piece of code to your Application class:

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

当然另一种选择是防止 64K 方法限制,因此您不再需要 MultiDex.您可以通过减少 Gradle 文件中(未使用的)依赖项的数量,或使用更具体的依赖项(wittyurchin 在 这个答案).

Of course another option is to prevent the 64K method limit, so you don't need MultiDex anymore. You can do this by decreasing the number of (unused) dependencies in your Gradle file, or use more specific dependencies (a nice example for google play-services is provided by wittyurchin in this answer).

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

1) 在构建到目标 API 设备时禁用即时运行(从 Android Studio 运行您的应用时,您会看到弹出的错误消息).

1) Instant run is disabled while building to target API devices (you'll see the error message popping up when running your app from Android Studio).

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

2) If you're using Robolectric for unittesting, you'll probably won't be able to run the tests anymore. You can fix this problem by extending the MultiDex.install(this); code from earlier. Instead of explaining everything myself, it'll be easier to check the issue, and answer of sschuberth over here.

...

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

ps. it seems I don't necessarily need compile 'com.android.support:multidex:1.0.1' to have MultiDex working, however, I've seen many reccomendations that say it IS required. If anyone got more advice on this, be my guest!

这篇关于Android studio 很多错误:找不到类'android.XXX'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:12