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?

Glenn
  • 12,741
  • 6
  • 47
  • 48
Lukas Fryc
  • 1,730
  • 3
  • 16
  • 18

16 Answers16

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!

Pavel
  • 5,374
  • 4
  • 30
  • 55
Alexander M.
  • 3,308
  • 2
  • 20
  • 31
  • 3
    @0xF File / Settings for Windows(Linux?), Preferences for Mac. Added Appearance & Behavior, thanks a lot. Missed that completely. – Alexander M. May 17 '16 at 05:24
  • 1
    @AlexanderMayatsky File / Settings is on Linux. – 0xF May 17 '16 at 12:14
  • 2
    on Linux it's: `File -> Settings -> Appearance&Behavior -> System Settings -> Android SDK` – lenooh Jan 09 '18 at 18:43
  • 2
    Ran into the same issue where the documentation wasn't displaying. This resolved it for the latest stable version which is 3.1.4. – ChallengeAccepted Aug 12 '18 at 03:12
  • 1
    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? – kbro Jun 17 '20 at 11:36
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

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • 1
    @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. – Dick Lucas Sep 22 '14 at 02:52
  • 38
    After downloading the sources I needed to restart Android Studio to make it to work – Sathesh Oct 17 '14 at 05:02
  • 3
    If you're debugging on an older version than the compileSdkVersion the source code will not match up. – David Feb 13 '16 at 00:25
  • I'd add Sathesh's comment to the solution. Looks like many had to restart, me too. – Ferran Maylinch Feb 02 '18 at 12:59
  • You need to tick the checkbox "Show Package Details" to find the entry "Sources for Android..." – Flimm Nov 23 '18 at 10:41
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>
Synesso
  • 37,610
  • 35
  • 136
  • 207
gdawg
  • 721
  • 6
  • 5
  • AndroidStudio 2.1.2 here on Mac OS. The following worked for me: `` – G. Steigert Aug 05 '16 at 18:54
  • 1
    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. – steven smith Jan 10 '17 at 05:53
17

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

joshkendrick
  • 3,497
  • 8
  • 38
  • 52
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

urps
  • 366
  • 1
  • 4
  • 13
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)
sfridman
  • 441
  • 5
  • 12
  • 6
    >Right click the menu item and select Open Library Settings. - where do you find that ? – kiruwka Apr 17 '15 at 09:10
  • 1
    It appears that this menu item is no longer available. There are ways to do this in normal InteliJ. – David Feb 13 '16 at 00:36
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"

clevertension
  • 6,929
  • 3
  • 28
  • 33
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.

DysaniazzZ
  • 825
  • 15
  • 29
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

Tom
  • 964
  • 9
  • 25
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.

Lee Adams
  • 311
  • 1
  • 7
0

First, check you have already load source.

then reset Android SDK Location at settings.

I solved my problem by above method.

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.

ulcica
  • 478
  • 5
  • 15
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.

user1364368
  • 1,474
  • 1
  • 16
  • 22
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.

user4057066
  • 295
  • 3
  • 14
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

Vlad
  • 7,997
  • 3
  • 56
  • 43
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".

MadHatter
  • 301
  • 3
  • 12