Java 在设备上调试时“源代码与字节码不匹配”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39990752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:36:47  来源:igfitidea点击:

'Source code does not match the bytecode' when debugging on a device

javaandroidandroid-studioandroid-debugandroid-6.0.1-marshmallow

提问by Crocodile

I have an app which I am compiling against API level 21: enter image description here

我有一个正在针对 API 级别 21 编译的应用程序: 在此处输入图片说明

and then debug it on a real device with API level 23:

然后在具有 API 级别 23 的真实设备上调试它:

enter image description here

在此处输入图片说明

The problem is when I try debugging through the Android OS's own classes, I get 'Source code does not match the bytecode'. Why is this happening? The test device the app is running on is API level 23, and the source file being debugged is level 23 as well. *enter image description here*

问题是当我尝试通过 Android 操作系统自己的类进行调试时,我得到“源代码与字节码不匹配”。为什么会这样?运行应用程序的测试设备是 API 级别 23,被调试的源文件也是级别 23。 *在此处输入图片描述*

I am really confused. Can anyone explain why I am seeing this message and how I can fix it?

我真的很困惑。任何人都可以解释为什么我会看到此消息以及如何修复它?

采纳答案by Michael Hoffmann

There's an open issue for this in AOSP, where some users have given possible solutions: https://code.google.com/p/android/issues/detail?id=225199&sort=-id&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

在 AOSP 中有一个未解决的问题,一些用户已经给出了可能的解决方案:https: //code.google.com/p/android/issues/detail?id=225199&sort=-id&colspec=ID%20Type%20Status%20Owner% 20总结%20Stars

The potential solutions given there (as of the date of this post) are:

那里给出的潜在解决方案(截至本文发布之日)是:

  • Click Build->Clean
  • Disable Instant Run, in Settings->Build, Execution, Deployment
  • 点击构建->清理
  • 在设置->构建、执行、部署中禁用即时运行

回答by Swathi

Go to Project Settings > Artifacts. Select the artifact which has the problem. There is an option "Include in project build". This needs to be checked(enabled). For older versions of IntelliJ this option is "Make on build".

转到项目设置 > 工件。选择有问题的工件。有一个选项“包含在项目构建中”。这需要检查(启用)。对于旧版本的 IntelliJ,此选项是“Make on build”。

回答by Gangnus

If you use Gradle, it is probably a problem with Gradle caches. (Reference). Alas, even if you run

如果您使用 Gradle,则可能是 Gradle 缓存的问题。(参考)。唉,就算你跑

gradle --refresh-dependencies

, it is not refreshing really all dependencies. Some rubbish remains. (Reference).

,它并没有真正刷新所有依赖项。一些垃圾仍然存在。(参考)。

So, the most sure (but drastic and long) variant is to clear all inside from the [user]/.gradle/caches. Or to find your problem project there and clear only its caches.

因此,最确定的(但剧烈而漫长的)变体是清除 [user]/.gradle/caches 中的所有内容。或者在那里找到您的问题项目并仅清除其缓存。

回答by Federico Alvarez

Probably this error message can have more than one cause, my case was not like the one from the OP, in my case this was due to a 3rd party library that required additional libraries.

可能此错误消息可能有多个原因,我的情况与 OP 中的情况不同,在我的情况下,这是由于需要其他库的第 3 方库造成的。

For example: you manually add X.jar to your LIB, but this X.jar requires Z.jar to work.

例如:您手动将 X.jar 添加到 LIB,但此 X.jar 需要 Z.jar 才能工作。

It took me sometime to figure out, the message was not helping at all. I had to debug the app until I reached the crashing class, and in that class make sure that all imports were satisfied.

我花了一些时间才弄明白,这条消息根本没有帮助。我不得不调试应用程序,直到我到达崩溃的类,并在该类中确保所有导入都得到满足。

(Particualry: I added MercadoLibre-0.3.4.jar, which required commons-httpclient.jar)

(部分:我添加了 MercadoLibre-0.3.4.jar,需要 commons-httpclient.jar)

Hope this helps!

希望这可以帮助!

回答by Uriel Frankel

You should use an Android emulator with the same api level as the compileSdkVersion. In your case you should use Android emulator with api level 21.

您应该使用与 compileSdkVersion 具有相同 api 级别的 Android 模拟器。在您的情况下,您应该使用 API 级别为 21 的 Android 模拟器。

回答by Jeffery Ma

here is my solution

这是我的解决方案

if you got more than one version libraries, it may help.

如果您有多个版本库,它可能会有所帮助。

  1. set a breakpoint on the lib source code
  2. let the code run to the breakpoint
  3. you will got these tips

    enter image description here

  4. click the arrow icon

  5. you will got this

    enter image description here

  6. double click to select the correct lib (normally the highest version of the lib is correct)

    if have clicked the "disable" button by mistake, you can enable it in the debugger setting

  1. 在 lib 源代码上设置断点
  2. 让代码运行到断点
  3. 你会得到这些提示

    在此处输入图片说明

  4. 单击箭头图标

  5. 你会得到这个

    在此处输入图片说明

  6. 双击选择正确的lib(一般最高版本的lib是正确的)

    如果误按了“禁用”按钮,您可以在调试器设置中启用它

enter image description here

在此处输入图片说明

if you do not have the tips in the step 3, maybe you can check whether you have checked the setting options.

如果您没有步骤3中的提示,也许您可​​以检查是否检查了设置选项。

回答by medynets

Android Studio takes source version equal to Target Version in your application. Compilation performed with source version equal to above mentioned Compile Version. So, take care that in your project Compile Version == Target Version (adjust module's build.gradle file).

Android Studio 在您的应用程序中采用与目标版本相同的源版本。使用与上述编译版本相同的源版本执行编译。因此,请注意在您的项目中编译版本 == 目标版本(调整模块的 build.gradle 文件)。

回答by Joe Giusti

I had the same issue and found a solution. If you have a line flagged in red, it will give you this error, but if you un-flag all of the lines it will work normally.

我遇到了同样的问题并找到了解决方案。如果您有一行标记为红色,则会出现此错误,但如果您取消标记所有行,它将正常工作。

by flagged I mean when you click on the left side where the line numbers are and it highlights the line. If that is not clear here are pictures.
go from: flagged lineto: not flagged line

标记我的意思是当您单击行号所在的左侧并突出显示该行时。如果不清楚,这里有图片。
从: 标记线转到: 未标记线

回答by ashishdhiman2007

This can also happen in case you have enabled ProGuard. In buildTypes set minifyEnabled false, shrinkResources false, useProguard false

如果您启用了 ProGuard,也会发生这种情况。在 buildTypes 中设置 minifyEnabled false、shrinkResources false、useProguard false

回答by Asjad Sohail

I tried the solutions given here while working on an application that used Bluetooth Low Energy(BLE). I tried,

我在处理使用蓝牙低功耗 (BLE) 的应用程序时尝试了此处给出的解决方案。我试过,

  1. Clean Build
  2. Disabled Instant Run
  3. Invalidate Caches / Restart
  1. 清洁构建
  2. 禁用即时运行
  3. 使缓存无效/重新启动

all of these failed.

所有这些都失败了。

What I did was debug the points where I thought I was getting the warning, I still got the warning but the application was working fine. You can disregard the warning.

我所做的是调试我认为收到警告的点,我仍然收到警告,但应用程序运行良好。您可以忽略警告。