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

18

106

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

and then debug it on a real device with API level 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*

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

Elburr answered 12/10, 2016 at 4:52 Comment(4)
looks like the apk in your device is older or different than the current code. I know its obvious but did you try uninstalling the app once and install again?Tana
It seems that error is related with your IDE's Intellij IDEA plugin. I could find a bug reported here confluence.jetbrains.com/display/IDEADEV/…. Could you please go through that?Fragile
@Akash I've tried that. But the file I am debugging through is part of the Android OS (ContentResolver) not my app.Elburr
@FebiMathew Thanks. It could be the same as IDEA-159697. This is a fix in the vanilla IntelliJ IDE. How do I know which Android Studio IDE build this fix is appearing in?Elburr
S
53

There's an open issue for this in Google's IssueTracker.

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

  • Click Build -> Clean
  • Disable Instant Run, in Settings -> Build, Execution, Deployment
Superphosphate answered 12/11, 2016 at 18:53 Comment(4)
Neither disabling Instant Run or any of the other solutions in this link has worked for me.Knockknee
@Knockknee Did you find another solution? I haven't ever encountered this problem again, so it's hard for me to debug it and expand my answer here.Superphosphate
I upped the minimum sdk and did a clean rebuild which made the problem "go away". However I'm not satisfied with this solution as I wanted an older min sdk.Knockknee
None of the above solutions have worked for me. I did not even find the Instant run option. It is so irritating to debug using INteelij. I am having to clean install after every single code change.Bowerbird
O
34

Here is my solution:

If you got more than one library version, this 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

  1. click the arrow icon

  2. you will get this

    enter image description here

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

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

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

Orchid answered 29/4, 2018 at 5:27 Comment(2)
Alternative sources are enabled, but don't show up. No drop down (and yes, I have 4 different API levels installed).Abomb
In a gradle project, this did not help, I just went ahead and removed the wrong library from Project Modules temporarily and that helped, besides a break point in the right class using a drop to frame!Bloodfin
B
20

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.

Bezel answered 9/1, 2018 at 19:55 Comment(2)
This worked for me. My device was API 22, so setting compileSdkVersion to 22 solved the problem.Orose
Also worked for me. I was on a physical device that was using Android 8.1.0 (API 27) and my computer only had sources for API 30 installed. Switching to an emulator with API 30 solved it. (compileSDKVersion now matches the device version)Impressible
S
5

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

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.

Skiff answered 28/8, 2017 at 8:28 Comment(0)
B
2

My app is compiled on API LEVEL 29, but debugging on real device on API LEVEL 28.I got the warning source code does not match the bytecode in AndroidStudio.I fixed it thought these steps:

  1. Go to Preferences>Instant Run: uncheck the instant run

  2. Go to Build>Clean Build

  3. Re-RUN the app

Now, the debug runs normal.

Bloodstained answered 27/9, 2019 at 10:57 Comment(0)
B
2

These are the steps that worked for me (For both Mac and Windows):

  1. Click on "File"
  2. Click on "Invalidate Caches / Restart ..."
  3. Choose: "Invalidate and Restart"

Note: It will take less than a minute for small projects, but since my project was big (approximately one million lines of code), it took 20 minutes.

Backer answered 2/2, 2020 at 2:25 Comment(0)
F
1

I tried all the solution given here and none of them worked for me. In version 2019.1.3 I just clean & rebuild artifact and it worked; first do Build -> Build Artifacts... -> <select your artifact> -> Clean then click Build or Rebuild from same place.

Floyd answered 25/6, 2019 at 5:47 Comment(0)
C
1

So I created an account just so I could help fix this problem that is plaguing a lot of people and where the fixes above aren't working.

If you get this error and nothing here helps. Try clicking the "Resume program play button" until the program finishes past the error. Then click in the console tab next to debug and read the red text.

I was getting that source code error even though my issue was trying to insert a value into a null Array. Step 1 Click the resume button

Step 2 Click the console tab and read the red text

Crimea answered 17/11, 2020 at 13:51 Comment(2)
This looks like it may be a great answer! Others on this post have added screen captures to help clarify which GUI actions need to be taken. A screen capture with a red box/arrow around Resume program play button could be useful here.Ellaelladine
I added some images to help. Not sure how to get them to display but they are in the links I don't have enough rep to post images yet so the links will have to do.Crimea
O
0

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".

Oblate answered 29/6, 2017 at 14:55 Comment(0)
L
0

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.

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

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)

Hope this helps!

Lona answered 27/9, 2017 at 13:51 Comment(0)
S
0

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

Swashbuckler answered 21/12, 2018 at 9:22 Comment(0)
A
0

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

  1. Clean Build
  2. Disabled Instant Run
  3. Invalidate Caches / Restart

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.

Awlwort answered 6/2, 2019 at 13:1 Comment(0)
M
0

You can created AVD, select API Level equal your tagetApi andr compileApi, it works for me.

Magen answered 20/3, 2020 at 1:58 Comment(0)
R
0

here is cause of why I got this error "source code does not match bytecode". My cause doesn't have anything to do with any API, compiler version..... It is caused by when inflate a layout view into a root view while I mistakenly initiate the inflator in else where (a chuck of "result handler" code that put on top of onCreate function of android app code). Somehow the debugger doesn't give me right hint of this (ex, inflator is not initialized or has instance) when breakpoint is set and stop here.

Resurrect answered 20/7, 2021 at 3:10 Comment(0)
D
0

This happened to me when accidentally I have added the same library multiple times.

implementation 'androidx.appcompat:appcompat:1.1.0'

The above library was added multiple times.

Dominga answered 12/8, 2021 at 7:29 Comment(0)
A
0

If clean, rebuild, invalidate cache and restart etc. techniques are not working, then try deleting the previous APK and reinstalling the new APK.

Abracadabra answered 20/10, 2021 at 13:39 Comment(0)
S
-1

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).

Sloth answered 20/7, 2018 at 12:3 Comment(0)
N
-2

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 line to: not flagged line

Novikoff answered 4/10, 2018 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.