Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio shows sources from API of compileSdkVersion when debugging on device with older API

I am debugging with Android Studio (A.S) 1.0.2 with compileSdkVersion 21 set up in build.gradle. When using an emulator with API 21 everything works fine. The problem occurs when debugging on a device having API 19. Each line in the call stack (tagged as 'Frames' in A.S) correctly shows the function name, source file and line number that matches API 19. However, when I click on one of the lines that corresponds with one of the framework sources (e.g. Activity.java), A.S incorrectly opens up and displays the API 21 version of the file rather than the API 19 version.

In my android sdk folder I have both ./sources/android-19 and ./sources/android-21

Any idea why A.S displays the wrong version of the file?

Things I tried (in order):

  • Resetting Android Studio (by removing ~/.AndroidStudio*)
  • Update Android Studio to latest version
  • Running on emulator with API 21 - A.S shows the correct version (21) of the files.
  • Changing compileSdkVersion to 19 and running on API 19 device - A.S shows the correct line numbers in call stack and opens the correct version (API 19) of the files in the correct line.
  • Changing compileSdkVersion to 19 and running on API 21 emulator - A.S shows, in the call stack, the line numbers corresponding to API 21 (which seems to me as the correct behavior), however, when clicking on it, A.S mistakenly opens up the API 19 version of the file rather than the API 21 version.

To summarize, when clicking on a line in the call stack A.S opens up the version of the file represented by compileSdkVersion and not the one used by the device/emulator during the debug session.

like image 790
HaimS Avatar asked Dec 27 '14 22:12

HaimS


People also ask

What is the difference between compileSdkVersion and targetSdkVersion?

Even if the compileSdkVersion and targetSdkVersion have completely different meanings they are obviously not independent. targetSdkVersion cannot be higher than the compileSdkVersion simply because we cannot target things that we know nothing about during compilation.

How do I change the API level in Android project?

Step 1: Open your project in Android mode then go to Gradle Scripts > build. gradle(Module: app) as shown in the following image. Step 2: Refer to the below image and here you have to change the minSdkVersion and targetSdkVersion as per the requirement.

How can you debug your app when it's already released?

Open the app on your phone. In the Devices view, expand the entry for your phone if it isn't already expanded, and look for your app's package name. Click on the package name, and in the top right of the Devices view you should see a green bug along with a number of other small buttons. Click the green bug.


2 Answers

Answered with working solution here:

https://stackoverflow.com/a/32236703/1267536

Full content:

I use appcompat support library quite a lot. Changing the compileSdkVersion only results in massive compile errors. If you are using appcompat-21, you must compile against api 21 or you will get compile errors.

This works fine if you are debugging against version 21. But when you are debugging on Jelly Bean (API 17), your debugger keeps dropping you to the API 21 source. Very annoying and hard to debug.

The solution I've ended up using is very hacky but works! Let's say you are trying to debug against API 17. Do the following:

  1. mv $ANDROID_HOME/sources/android-21 $ANDROID_HOME/sources/android-21-orig
  2. cp $ANDROID_HOME/sources/android-17 $ANDROID_HOME/sources/android-21
  3. restart android studio so it will pick up the correct paths.
  4. Debug

Just don't forget to put all the directories back after you're done.

Here's an android studio bug report about this issue: https://code.google.com/p/android/issues/detail?id=183976

like image 152
vangorra Avatar answered Oct 09 '22 01:10

vangorra


Open your project in Intellij Idea (Android Studio based on it) and choose File -> Project Structure.... In project setting select "project SDK" to version which you need debug and select for all project modules "project SDK" as module SDK. After that you can attach debug connection to your device/emulator and see proper Android source.

You don't need to build project in Intellij Idea.

like image 31
Sergei Vasilenko Avatar answered Oct 09 '22 01:10

Sergei Vasilenko