Lukas Fryc
Lukas Fryc

Reputation: 1740

Android Studio: how to attach Android SDK sources?

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?

Upvotes: 116

Views: 71939

Answers (16)

Tom
Tom

Reputation: 1052

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

Upvotes: 1

MadHatter
MadHatter

Reputation: 395

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

Upvotes: 0

Vlad
Vlad

Reputation: 8572

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

Upvotes: 0

Piyush Agarwal
Piyush Agarwal

Reputation: 25858

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

Upvotes: 71

Alexander M.
Alexander M.

Reputation: 3478

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!

Upvotes: 249

user4057066
user4057066

Reputation: 295

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.

Upvotes: 0

user1364368
user1364368

Reputation: 1564

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.

Upvotes: 0

joshkendrick
joshkendrick

Reputation: 3547

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

Upvotes: 18

DysaniazzZ
DysaniazzZ

Reputation: 845

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.

Upvotes: 1

ulcica
ulcica

Reputation: 503

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.

Upvotes: 0

Double.fang
Double.fang

Reputation: 1

First, check you have already load source.

then reset Android SDK Location at settings.

I solved my problem by above method.

Upvotes: 0

urps
urps

Reputation: 366

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

Upvotes: 7

gdawg
gdawg

Reputation: 721

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>

Upvotes: 32

clevertension
clevertension

Reputation: 7077

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"

Upvotes: 3

Lee Adams
Lee Adams

Reputation: 331

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.

Upvotes: 0

sfridman
sfridman

Reputation: 441

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)

Upvotes: 5

Related Questions