Android Studio: how to attach Android SDK sources?
Asked Answered
F

16

116

How can I attach Android SDK sources to Android Studio?

When I open one of the Android classes, the "Sources not found" window with "Attach Sources" option doesn't appear.

What is the other way to do this?

Finished answered 19/1, 2014 at 19:41 Comment(0)
A
71

Open your sdk manager from toolbar menus and download the "Sources for Android SDK" for the API level defined in your build.gradle file like

compileSdkVersion 19 // compile with API level 19

Nothing else needs to be done.

Note : Sources for SDK is available only for API levels 14 and above.

UPDATE ( Based on stable release 3.2.1):

Google changes the approach of shipping the sources, so lets see what changed.

Go to the following location

Preferences -> Apperance & Behaviour -> System Settings -> Android SDK

Quite lazy to navigate type SDK in search and studio will help you to take to right place.

enter image description here

You can read the description, says mostly what to do. So after clicking on "show package details" you will see whether sources are installed or not (as shown in below picture) if it is not installed do install and you are good.

enter image description here

Aldaaldan answered 19/1, 2014 at 20:1 Comment(5)
@takumar, this is because the source of the SDK is not currently available for Android API 20. I am sure when Android "L" comes out of preview, the source will be available in the SDK manager.Aeneas
After downloading the sources I needed to restart Android Studio to make it to workFoxworth
If you're debugging on an older version than the compileSdkVersion the source code will not match up.Hinze
I'd add Sathesh's comment to the solution. Looks like many had to restart, me too.Loudhailer
You need to tick the checkbox "Show Package Details" to find the entry "Sources for Android..."Sidran
P
249

Android Studio 3.2.1 reporting in: solved the issue by resetting SDK.

Preferences -> Appearance & Behavior -> System Settings -> Android SDK.

Click on Edit to the right of Android SDK location. Next, Next, Finish to complete the wizard and voila!

Ponderous answered 28/4, 2016 at 1:41 Comment(5)
@0xF File / Settings for Windows(Linux?), Preferences for Mac. Added Appearance & Behavior, thanks a lot. Missed that completely.Ponderous
@AlexanderMayatsky File / Settings is on Linux.Penland
on Linux it's: File -> Settings -> Appearance&Behavior -> System Settings -> Android SDKVelutinous
Ran into the same issue where the documentation wasn't displaying. This resolved it for the latest stable version which is 3.1.4.Waring
This isn't working for me. I've downloaded the sources for compileSdkVersion 28 and my build.gradle also has targetSdkVersion 28, and when I run my app in the debugger I can see all sorts of things like art::ArtMethod::Invoke and StartBackgroundThread (this is for a JNI helper thread in my app) but clicking on anything in the Android part of the stack doesn't show me the associated source file. What am I missing?Frisian
A
71

Open your sdk manager from toolbar menus and download the "Sources for Android SDK" for the API level defined in your build.gradle file like

compileSdkVersion 19 // compile with API level 19

Nothing else needs to be done.

Note : Sources for SDK is available only for API levels 14 and above.

UPDATE ( Based on stable release 3.2.1):

Google changes the approach of shipping the sources, so lets see what changed.

Go to the following location

Preferences -> Apperance & Behaviour -> System Settings -> Android SDK

Quite lazy to navigate type SDK in search and studio will help you to take to right place.

enter image description here

You can read the description, says mostly what to do. So after clicking on "show package details" you will see whether sources are installed or not (as shown in below picture) if it is not installed do install and you are good.

enter image description here

Aldaaldan answered 19/1, 2014 at 20:1 Comment(5)
@takumar, this is because the source of the SDK is not currently available for Android API 20. I am sure when Android "L" comes out of preview, the source will be available in the SDK manager.Aeneas
After downloading the sources I needed to restart Android Studio to make it to workFoxworth
If you're debugging on an older version than the compileSdkVersion the source code will not match up.Hinze
I'd add Sathesh's comment to the solution. Looks like many had to restart, me too.Loudhailer
You need to tick the checkbox "Show Package Details" to find the entry "Sources for Android..."Sidran
W
32

I'm unable to find the aforementioned gui options in v8.11 on osx however grep-fu tells me:

config is stored in ~/Library/Preferences/AndroidStudioBeta/options/jdk.table.xml

After editing this file (with Studio stopped) and restarting source mapping works fine for me.

diff --git a/options/jdk.table.xml b/options/jdk.table.xml
index 0112b91..33828b8 100644
--- a/options/jdk.table.xml
+++ b/options/jdk.table.xml
@@ -76,7 +76,7 @@
         </javadocPath>
         <sourcePath>
           <root type="composite">
-            <root type="simple" url="file:///Applications/Android Studio.app/sdk/sources/android-19" />
+            <root type="simple" url="file:///Users/tehdawgz/dev/android-sdk/sources/android-19" />
           </root>
         </sourcePath>
       </roots>
Washout answered 14/10, 2014 at 4:41 Comment(2)
AndroidStudio 2.1.2 here on Mac OS. The following worked for me: <root type="simple" url="file://$USER_HOME$/Library/Android/sdk/sources/android-23" />Cork
Worked for me. Ubuntu 14.04/Android Studio 2.2. I used 'locate jdk.table.xml' which in my case was at ~/.AndroidStudio2.2/config/options/jdk.table.xml. Interestingly it seems at some point in this process something replaced my absolute paths with "file://$USER_HOME$..."?? In any case, things started working after I fixed the file.Lustreware
A
18

For me on 2016/08/29 running android studio 2.1.3:

I had downloaded and was compiling against api 24 before the sources were released. I want to point at the source now that they've been released. Restarting Android Studio after downloading the sources in the SDK manager wasnt working.

I had to act like I was going to Edit the SDK location in the SDK Manager, when I did that, I could just click 'next' through the dialog and re-indexed the sdk stuff automatically.

button for edit SDK location where to just click next through

Abecedarium answered 29/8, 2016 at 14:19 Comment(0)
P
7

gdawg's solution worked for me as well in Windows 10 and Android Studio 2.0. With the small modification that the jdk.table.xml file is located at

C:\Users\{USER_NAME}\.AndroidStudio2.0\config\options\jdk.table.xml

Padauk answered 24/4, 2016 at 7:2 Comment(0)
E
5

Not sure why the accepted answer only seems to work with API level 19. I was able to attach SDK sources for API level 15 by explicitly providing the source path for the SDK. Here's what I did:

  1. Make sure I've downloaded the desired SDK sources and setup my build.gradle with the desired API level.
  2. In the Project window on the left, select the External Libraries drop-down.
  3. Right click the menu item and select Open Library Settings.
  4. Click the Sourcepath tab and use the + icon at the bottom left to link to the desired SDK sources (should be in something/sdk/sources)
Earwax answered 20/2, 2014 at 4:7 Comment(2)
>Right click the menu item and select Open Library Settings. - where do you find that ?Praxiteles
It appears that this menu item is no longer available. There are ways to do this in normal InteliJ.Hinze
H
3

In android studio 0.8.6(Beta) (OSX 10.9.4), it seems that you can view the android source code directly without attaching the Sources manually.

The prerequisite is you should download "Sources for Android SDK" in "SDK Manager" and restart "Android Studio"

Hypermetropia answered 27/8, 2014 at 3:13 Comment(0)
D
1

Just follow these steps: Settings --> Android SDK --> Launch Standalone SDK Manager --> download your needed SDK sources(in every Anroid Version's Sources for Android SDK) --> restart your Android Studio. If above don't help, make sure you have downloaded the required SDK sources, and reset the Android SDK Location to refresh. I hope it helps.

Decrescendo answered 24/8, 2016 at 2:45 Comment(0)
M
1

Sharing, since it took me a while to realize: at the moment (August 2020) API level 30 does not have available sources (no such option in the SDK manager). I changed my application to target API level 29 (instead of 30), and made sure to download the sources for version 29 and it worked.

Make sure that "Show Package Details" is turned on to see if sources are available for the SDK version that you are targeting.

Also, it's now possible to use "Tools -> SDK Manager" instead of "File -> Settings -> etc...".

SDK Manager

Mindoro answered 18/8, 2020 at 8:42 Comment(0)
T
0

I didn't know what menu step 3 of sfridman's answer was referring to. Another way I managed to get to the source I was interested in was after Android Studio had brought up the .class I wanted to dive into:

Breadcrumbs to .class file

I could then right click on that breadcrumb and choose "Jump to source":

enter image description here

It seemed to find it automatically from doing that, probably because I had installed the source via the Android SDK manager as described elsewhere.

Thyrse answered 9/7, 2014 at 22:0 Comment(0)
L
0

First, check you have already load source.

then reset Android SDK Location at settings.

I solved my problem by above method.

Larock answered 27/4, 2016 at 10:29 Comment(0)
A
0

The only thing that worked for me was:

  • Open the Android SDK Manager (Tools -> Android -> SDK Manager -> Launch Standalone SDK Manager)
  • uninstall all the SDK Platform and Sources for Android SDK (from all API levels)
  • restart Android Studio

Upon restart Android Studio will pop a wizard because no SDK is installed: install the SDK Platform and sources for the currently used API level.

Aloin answered 6/5, 2016 at 14:38 Comment(0)
S
0

In Android Studio 2.3.3 on Windows 7, I had the problem that the already downloaded source files für API-Level 26 in folder SDK\sources\android-26 were not recognized. Using "File | Invalidate Caches / Restart" solved the issue for me.

Sufficient answered 3/10, 2017 at 11:42 Comment(0)
I
0

For me downloading and refresh was not helping. I tried to choose source but still same issue. So I did Invalidate Cache/Restart. Now its working.

I tried to edit the jdk.table.xml file but there was no entry for android-26, it was only till android-25, so i decided not to edit that file.

Indefensible answered 11/1, 2018 at 6:51 Comment(0)
M
0

As for 09/08/2019 there are available such sources

sources;android-15
    Description:        Sources for Android 15
    Version:            2

sources;android-16
    Description:        Sources for Android 16
    Version:            2

sources;android-17
    Description:        Sources for Android 17
    Version:            1

sources;android-18
    Description:        Sources for Android 18
    Version:            1

sources;android-19
    Description:        Sources for Android 19
    Version:            2

sources;android-20
    Description:        Sources for Android 20
    Version:            1

sources;android-21
    Description:        Sources for Android 21
    Version:            1

sources;android-22
    Description:        Sources for Android 22
    Version:            1

sources;android-23
    Description:        Sources for Android 23
    Version:            1

sources;android-24
    Description:        Sources for Android 24
    Version:            1

sources;android-25
    Description:        Sources for Android 25
    Version:            1

sources;android-26
    Description:        Sources for Android 26
    Version:            1

sources;android-27
    Description:        Sources for Android 27
    Version:            1

sources;android-28
    Description:        Sources for Android 28
    Version:            1

You can list them by ./pathtoandroidsdk/tools/bin/sdkmanager --list --verbose --no_https

For example, install by ./pathtoandroidsdk/tools/bin/sdkmanager "sources;android-28"

There is no need in Android Studio for downloading

Setup instruction in Android Studio

In app build.gradle set correct version of sdk which will include the desired sources

android {
    compileSdkVersion 28
    ...

To apply changes - restart IDE

Mucus answered 9/8, 2019 at 8:29 Comment(0)
M
0

I just met a similar issue when I need to download the platform android 28 via command line, I saw the answer to run command ./pathtoandroidsdk/tools/bin/sdkmanager "sources;android-28".

But in fact, from developer.android.com, it shows an example as sdkmanager "platform-tools" "platforms;android-28", so I guess it should be ./pathToAndroidSdk/tools/bin/sdkmanager "platforms;android-28".

Messeigneurs answered 7/5, 2020 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.