源代码与Android的View.java的字节码不匹配。

huangapple go评论82阅读模式
英文:

Source code does not match the bytecode for Android's View.java

问题

我正在尝试调试我的Android应用程序。当调试器到达View.java文件时,我收到消息:“源代码与字节码不匹配”。我可以看到调试器位于文件的错误部分。有人知道如何解决这个问题吗?

我正在一个Android 10(API 29)设备上进行调试。在Android Studio的“首选项” -> “外观和行为” -> “系统设置” -> “Android SDK”中,我已确保一切都是最新的。我也进行了多次清理和重建,清除了缓存,重启了Android Studio和设备,卸载/重新安装了应用,将Android Studio更新到了4.0.1版本。在模块设置中,“编译SDK版本”是29,目标SDK版本和最小SDK版本都是29。没有任何帮助或效果。许多这些步骤都是在类似的帖子中提出的。

它尝试打开的View.java文件位于:~/Library/Android/sdk/sources/android-29/android/view/View.java。这应该是正确的文件,但调试器却位于文件的错误部分,同时我收到错误消息。

换上黑帽子,让我们假设我的笔记本电脑上的View.java文件对于那个API级别是正确的:是否可能我的手机正在运行非标准的代码,是否有任何方法可以检测到或排除这种情况?

英文:

I am attempting to debug my Android app. When the debugger gets to the View.java file, I receive the message, "Source code does not match the bytecode". I can see that the debugger is in the wrong part of the file. Does anyone know how to fix this?

I am debugging on an Android 10 (API 29) device. In the Android Studio Preferences -> Appearance & Behavior -> System Settings -> Android SDK, I've made sure everything is up to date. I've also cleaned and rebuilt multiple times, removed caches, restarted Android Studio and the device, uninstalled/re-installed, updated Android Studio to 4.0.1. In the Module Settings, the "Compile Sdk Version" is 29, and the Target SDK Version and Min SDK Version to 29. Nothing helps or has any effect. Many of these steps were suggested in similar posts.

The View.java file it tries to open is at: ~/Library/Android/sdk/sources/android-29/android/view/View.java. This should be the correct file, yet the debugger is in the wrong part of the file while I get the error message.

Putting on my black hat, let's assume that my laptop's View.java is correct for that API level: could my phone be running non-standard code, and is there any way to detect that or rule it out?

答案1

得分: 2

我想我已经弄清楚了发生了什么,对于发生在Android SDK文件中的这个问题,我有一个解决方案。令人恐惧的是,Android SDK平台升级到了版本5,但源代码包未更新。它仍然是版本1!为什么会发生这种情况,我无法解释:一个理性的人会期望两者保持同步。这个截图显示源代码与Android Studio下载的二进制文件不匹配:

源代码与Android的View.java的字节码不匹配。

唯一的解决方法是手动下载所需的文件,并在SDK源代码目录中手动打补丁。这很不美观,也是不必要的,但是如果你真的想逐步查看特定的SDK文件,你就必须这样做。

  1. 找出你的Android版本。在设备上,转到“设置” -> “关于手机” -> “Android版本”,并记下“构建编号”的值。我的是QQ3A.200805.001。

  2. 使用此页面找出与你的构建编号对应的标签:https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds。对于我的构建编号,标签是:android-10.0.0_r41

  3. 下载此标签的AOSP源代码。如果你是第一次这样做,这需要很长时间。参见https://stackoverflow.com/a/14353644/259718,了解如何进行操作的示例。

  4. 复制文件以覆盖部分源代码目录:cp -pr ~/android_src/frameworks/base/core/java/android/* ~/Library/Android/sdk/sources/android-29/android/(根据你的环境使用正确的位置,在这里我是使用android-29进行编译)

虽然并非所有android源代码文件夹中的目录都被覆盖,但核心android文件已被覆盖。这些文件占据了大部分源材料。如果你需要其他目录(例如“filterpacks”),你将不得不在其他AOSP目录中查找(例如“filterpacks”的目录在frameworks/base/media中)。

以下是我获取单个文件的步骤:

  1. 在https://android.googlesource.com上找到你需要的文件。通过google.com,我找到了它的位置:https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/View.java
  2. 确定你的文件所在的Android仓库。从前面的URL可以看出,它位于:https://android.googlesource.com/platform/frameworks/base/
  3. 使用你的标签获取该仓库。或者在线获取该标签最新修订的个别文件,例如:https://android.googlesource.com/platform/frameworks/base/+log/refs/tags/android-10.0.0_r41/core/java/android/view/View.java,修订版本为18c0fbf。
    • 要下载个别文件,在URL末尾添加“?format=text”,以获取Base64编码的版本。然后解码它:cat file | base64 --decode。
  4. 现在你有了该文件。进入包含有问题文件的SDK目录:cd ~/Library/Android/sdk/sources/android-29/android/view/
  5. 备份旧文件(cp -pr View.java View.java.orig),如果需要的话(如果出错,你总是可以重新安装SDK源文件)。将新文件复制到相同位置。
  6. 现在,你的调试器将使用正确的文件!你需要为调试器抱怨的每个文件都这样做。

希望这能对未来的某人有所帮助。我花了很多时间来弄清楚这个问题,我真诚地希望其他人不必浪费那么多时间。谷歌需要确保源代码包与发布包保持一致。查看过去的所有SDK发布版本,似乎没有一个源代码包与最新的发布版本保持同步。也许另一种调试SDK文件的不美观方法是使用与Revision#1源代码包相匹配的Revision#1 SDK发布版本。

英文:

I think I figured out what was happening and I have a solution for this problem when it happens with Android SDK files. Frighteningly, the Android SDK Platform gets updated to Revision 5, but the source package is not updated. It is still on Revision 1! Why this happened, I cannot explain: a reasonable person would expect both to be in sync. This screenshot shows the source code does not match the binary that Android Studio downloaded:

源代码与Android的View.java的字节码不匹配。

The only way around this is to manually download the files you need and patch them manually in your SDK source directory. It's ugly, it's unnecessary, but if you really want to step through a specific SDK file you have to do it.

  1. Find out what Android build you have. On your device, go to "Settings" -> "About phone" -> "Android version", and note down your "Build number" value. Mine is QQ3A.200805.001.

  2. Find out the Tag corresponding to your Build number using this page: https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds. For my build number, the tag is: android-10.0.0_r41

  3. Download the AOSP source for this tag. This takes a long time if you are doing this for the first time. See https://stackoverflow.com/a/14353644/259718 for an example on how to do that.

  4. Copy the files to overwrite some of the source directories: cp -pr ~/android_src/frameworks/base/core/java/android/* ~/Library/Android/sdk/sources/android-29/android/ (use the correct location for your environment, here I am compiling with android-29)

While not all of the directories in the android sources folder are overwritten, the core android files are overwritten. These comprise the majority of the source material. If you need other directories (e.g. "filterpacks"), you'll have to find that in the other AOSP directories (e.g. frameworks/base/media for "filterpacks").

Here are the steps I followed to get just one individual file:

  1. Find the file you need on https://android.googlesource.com. Using google.com, I found it at: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/View.java
  2. Determine the Android repo your file lives in. Looking at the previous URL, I can deduce it's at: https://android.googlesource.com/platform/frameworks/base/
  3. Grab that repo using your tag. Or grab that tag's most recent revision of that individual file on-line, e.g.: https://android.googlesource.com/platform/frameworks/base/+log/refs/tags/android-10.0.0_r41/core/java/android/view/View.java, revision 18c0fbf.
    • To download the individual file, add "?format=text" to the URL to get the base64 encoded version. Then decode it: cat file | base64 --decode.
  4. Now you have the file. Go to your SDK directory that contains the errant file: cd ~/Library/Android/sdk/sources/android-29/android/view/
  5. Backup the old file (cp -pr View.java View.java.orig) if you want (you can always re-install the SDK source files if you mess up). Copy in your new file into the same location.
  6. Now, your debugger will use the correct file! You will have to do this for each file that your debugger complains about.

I hope this helps someone else in the future. I spent a lot of time figuring this out and I sincerely hope someone else won't have to waste that time. Google needs to keep the source packages consistent with the release packages. Looking at all the past SDK releases, none of the source packages appear to be in sync with the latest release version. Maybe another ugly way to debug SDK files is to use a Revision #1 SDK release version that matches the Revision #1 source package.

答案2

得分: 0

抱歉,curious_george的解决方案只适用于设备上安装了直接从Google发布的源代码构建的Android版本。设备制造商并没有义务这样做,而Google使用的Apache许可证与GPL不同,不要求衍生作品的发布者提供其源代码访问权限。

我的设备是三星Galaxy S21,当前安装的Android版本的构建编号是
SP1A.210812.016.G998BXXU4BULF,显然没有列在https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds中。

那里列出的标记为210812.016的源代码版本有:

210812.016.A1 android-12.0.0_r3 2021年8月24日 星期二 18:43:39
210812.016.A2 android-12.0.0_r25 2021年8月24日 星期二 18:43:39
210812.016.B1 android-12.0.0_r30 2021年12月24日 星期五 23:08:44
210812.016.C1 android-12.0.0_r31 2021年12月25日 星期六 00:36:35

它们似乎都具有相同版本的View.Java,而且Android Studio指出这不是我设备上的版本。即使是注释更改也可能会混淆调试器,因为它会在特定行号寻找代码。

目前,我也无法使用模拟器进行调试,模拟器可能会拥有真正的Google发布的Android版本,因为最新版本的Android Studio(Android Studio BumbleBee | 2021.1.1 Patch 2,版本号:AI-211.7628.21.2111.8193401,构建日期:2022年2月17日)似乎出现了问题。模拟器在启动时要么锁定其用户界面,要么崩溃,并且调试器无法连接到它。

因此,目前似乎没有办法解决最初发布的问题。

帮帮我!!

英文:

Unfortunately curious_george's solution only works if the device has a version of Android built directly from the published Google sources. Device manufacturers are not obliged to do that, and the Apache Licence used by Google, unlike the GPL, does not oblige publishers of derivative works to give access to their sources.

My device is a Samsung Galaxy S21, and the build number of the currently installed Android is
SP1A.210812.016.G998BXXU4BULF, which is of course not listed in https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds.

The versions of the sources tagged 210812.016 listed there are

210812.016.A1 android-12.0.0_r3 Tue Aug 24 18:43:39 2021
210812.016.A2 android-12.0.0_r25 Tue Aug 24 18:43:39 2021
210812.016.B1 android-12.0.0_r30 Fri Dec 24 23:08:44 2021
210812.016.C1 android-12.0.0_r31 Sat Dec 25 00:36:35 2021

They all appear to have the same version of View.Java, and Android Studio says that it isn't the version in my device. Even a comment change can confuse the debugger since it looks for the code at a particular line number.

At the moment I also can't debug using the emulator, which would presumably have a genuine Google release of Android, since the latest release of Android Studio (Android Studio BumbleBee | 2021.1.1 Patch 2, Build #AI-211.7628.21.2111.8193401, built on February 17,2022) seems to have broken it. The emulator either locks up its UI or crashes when started, and the debugger can't connect to it.

So there seems currently to be no way of solving the originally posted problem.

HELP!!

huangapple
  • 本文由 发表于 2020年8月24日 15:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63556563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定