Google’s Android SDK includes android.jar, which contains the Android public API class files. They also provide an Eclipse plugin, making it quite easy to get started. What you will not find, however, is androidSrc.jar, so when you try to look at Android SDK source code in Eclipse, you get something like this:

Open Source to the Rescue!

Earlier this month, Google released the source code — all of it — and by the way, it’s HUGE. Anyway, to browse source in Eclipse, you need to visit http://source.android.com/ and follow their instructions to pull everything down using Git. Set aside an evening to do this, because having the full source makes it infinitely easier to understand how the SDK works.

Attaching to Eclipse…Not So Fast, Buddy

Now that you have the source, you can tell Eclipse where to find it. I tried right-clicking on android.jar and selecting Properties, but am greeted with this screen:

Hmm…”The current class path entry belongs to container ‘Android Library’ which does not allow user modifications to source attachments on its entries.” This really threw me for a loop. I generally use IDEA, so I was really stumped by this. So, I dug into the source code to find out a solution.

Examining the Source

When you download the Android source, you get EVERYTHING. This includes the OS, the Dalvik VM, the Eclipse Plugin, the public SDKs, etc.

In the com.android.ide.eclipse.adt.project.internal package, I found
a class named AndroidClassspathContainerInitializer.java. This has
the following code:

IPath android_src = new Path(AdtPlugin.getOsAbsoluteAndroidSources());

OK, fine. So looking inside AdtPlugin.java:

/** Returns the absolute android sources path in the sdk */public static String getOsAbsoluteAndroidSources() {  return getOsSdkFolder() + getOsRelativeAndroidSources();}/** Returns the android sources path relative to the sdk folder */public static String getOsRelativeAndroidSources() {  return AndroidConstants.FD_ANDROID_SOURCES;}

And finally inside AndroidConstants.java:

public static final String FD_ANDROID_SOURCES = "sources";

I win!

Solution #1

Based on the above analysis, you need to create a directory named “sources” inside your Android SDK installation. So right next to your android.jar, create a sources directory.

Now that you have this directory, you can find the appropriate source directories in the Android sources that you pulled from Git. Google’s checkout directions suggest a directory named ‘mydroid’, so I’ll use that here. The public SDK source is found in mydroid/frameworks/base/core/java. Under that folder you’ll find an android directory, which should be copied (or symlinked) over to the sources directory in your SDK installation.

You’ll have to do some hunting around to find all of the source code, it’s kind of sprinkled all over the place in the mydroid directory tree. In the end, you should have something like:

SDK_PATH  | android.jar  +--docs/...  +--samples/...  +--sources       +--android       |      ...accounts, annotation, app, bluetooth, etc...       +--com/android/etc...       +--dalvik/...       +--java/...       +--javax/...

I grabbed all kinds of directories, and I didn’t take good notes, so I probably missed a few and inadvertently added too many. You probably only really need the public SDK sources, but I think it’s nice to also study implementation classes, as well.

Now when I browse into the Android SDK classes, I see the source code:

Solution #2

If you’d rather not copy (or link) all of those source directories into your SDK directory, you can also create an Eclipse User Library. At first, I tried creating my own library containing android.jar, but Eclipse complained about that being a duplicate .jar file. So instead, I picked an arbitrary JAR file as my user library, and then proceeded to attach all of the Android source directories to that library.

My user library seemed to do the trick, but I like having that sources directory because I don’t have to remember to add the user library to each new project I create.


<!-- entry --><!-- post --><!-- You can start editing here. --> <!-- c_gravatar --> <!-- c_sidebar --> kiran Says: <!-- c_author -->

Good job on writing this.
Google did a really bad job on the api docs.. I am glad the source will alleviate the pain of the missing docs.
btw, have you had any luck with getting the VideoView working on the real phone or the emulator.

<!-- c_content --> <!-- c_main --> November 3rd, 2008 at 11:55 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> wdf Says: <!-- c_author -->

It doesn’t work at all, and just see .class

<!-- c_content --> <!-- c_main --> November 18th, 2008 at 4:58 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> wdf Says: <!-- c_author -->

When I copied (not symlinked) over to the sources directory, it works, thanks a lot!

<!-- c_content --> <!-- c_main --> November 18th, 2008 at 5:05 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> apple Says: <!-- c_author -->

Which files did you copy exactly? I tried both symlink and copy and still doesn’t work. I copied
~/myandroid/frameworks/base/core/java/android/

Tried to view Activity.class from Eclipse and had the same error.

$ cd ~/android-sdk-linux_x86-1.0_r1/sources
$ cp -rf ~/myandroid/frameworks/base/core/java/android/ .
$ ls
android
$ ls android
accounts app content ddm hardware os pim provider server syncml text view widget
annotation bluetooth database debug net package.html preference security speech test util webkit

<!-- c_content --> <!-- c_main --> November 20th, 2008 at 8:51 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Wazabe Says: <!-- c_author -->

Sweet as hell, thank you so much for sharing this killer tip !

<!-- c_content --> <!-- c_main --> November 21st, 2008 at 12:13 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Josh Guilfoyle Says: <!-- c_author -->

Just as a tip for anyone reading, you must hit Refresh on the Android library in Eclipse to make this work. Even if you restart Eclipse.

<!-- c_content --> <!-- c_main --> November 22nd, 2008 at 12:50 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> apple Says: <!-- c_author -->

Refresh did the trick! Thank you Josh.

<!-- c_content --> <!-- c_main --> December 1st, 2008 at 8:55 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> LeConcombreMasque Says: <!-- c_author -->

I also found this documented at http://ralf.alfray.com/.izumi/AndroidTips.blog (scroll down)

<!-- c_content --> <!-- c_main --> December 2nd, 2008 at 1:00 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> davemepp Says: <!-- c_author -->

Thanks for the helpful post, Eric.

I used http://androidforums.com/showthread.php?t=1045 to create android-src.jar and then extracted the jar to ANDROID_HOME\sources, and it worked like a charm.

<!-- c_content --> <!-- c_main --> December 5th, 2008 at 4:21 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Michael Forster Says: <!-- c_author -->

Collecting all of the *.java files from the Android source tree to the right place is a lot of work. Here is a small script that I wrote to build a source ZIP: http://blog.michael-forster.de/2008/12/view-android-source-code-in-eclipse.html

<!-- c_content --> <!-- c_main --> December 20th, 2008 at 12:09 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Robert Zaleski Says: <!-- c_author -->

I had problems getting this to work until I used the rsync line on the ralf site to copy everything. It may have worked sym linking everything too, but it must either be that I didn’t refresh right, or there was a certain file I needed. But it’s good to know I can browse source now. Thanks for the info.

<!-- c_content --> <!-- c_main --> December 20th, 2008 at 2:25 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> boyb Says: <!-- c_author -->

In Eclips, i want to see the souce code for “Android Liabrary->res.layout-simple_list_item_1.xml” or something like that, when double click the item, eclips could not open it. If right click the item and choose to see property, it prompt “no property pages for”, but i could see the souce code for Activity.classs.
I have already copy folder andoid(including accounts/anotation…)? And i find some folder …apps/common/res/layout contain those xml, so i also copy apps to andoid folder and click to refresh the sdk in Eclips, but still could not see the souce.

What else should i do to view souce code of those xml file?Thanks

<!-- c_content --> <!-- c_main --> December 21st, 2008 at 9:05 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Uhyon Chung Says: <!-- c_author -->

You might also want to make sure your SDK Location contains a final slash (or backslash in Windows). Or else, it won’t find your sources since the append in getOsAbsoluteAndroidSources does add a slash itself.

<!-- c_content --> <!-- c_main --> March 17th, 2009 at 10:55 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> sudheer Says: <!-- c_author -->

I tried all the steps mentioned above. But I am not able to see every thing even now. For e.g. I am not able to see the surface manager class in android.os .but its present in the relevant directory. I am not able to see that in eclipse. Can some one tell me what could be the problem?

What I did
1. created sources directory
2. copied android folder from /framework/core/.. to sources
3. copied all other java files to sources
4. refreshed/restarted eclipse

Did I miss any thing?

<!-- c_content --> <!-- c_main --> March 19th, 2009 at 3:19 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Ty Says: <!-- c_author -->

The slash was the culprit for me (Mac OS-X, Ganymede). The android SDK path defaults to no slash, so the default path formed is incorrect.
Eclipse > Preferences > Android > SDK Location

<!-- c_content --> <!-- c_main --> April 16th, 2009 at 11:40 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Sharad Says: <!-- c_author -->

I observed that instead of “source” folder it was “sources” folder in 1.5 SDK, if you are not sure on what name you should give to the folder then just goto the build path of the project (Right click Android project -> Properties) and expand Android 1.1 and then Android.jar, you should see source attachment path with should exactly match the path where you are keeping the source code. Once this is set just restart eclipse and you are done.

<!-- c_content --> <!-- c_main --> May 15th, 2009 at 4:50 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Rick Bing Says: <!-- c_author -->

Great job. You are Numero Uno.

<!-- c_content --> <!-- c_main --> May 19th, 2009 at 4:28 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Donald B. Says: <!-- c_author -->

Thanks, this is very helpful.

I had to create the symlinks in the appropriate plaforms/android-1.x directory to get this to work for me.

My SDK:
~/apps/android-sdk-linux_x86-1.5_r2/

But my android.jar file is here:
~/apps/android-sdk-linux_x86-1.5_r2/platforms/android-1.1

<!-- c_content --> <!-- c_main --> May 24th, 2009 at 6:05 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> mefiX Says: <!-- c_author -->

With Android 1.5 i had to place the “sources”-folder into
/platforms/android-1.5
Eventually a refresh of the project revealed the source-files.

<!-- c_content --> <!-- c_main --> May 26th, 2009 at 5:01 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Jn Says: <!-- c_author -->

Thanks a lot!

<!-- c_content --> <!-- c_main --> June 9th, 2009 at 4:45 am <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> Manjunath Says: <!-- c_author -->

This is some thing which i was looking form past many days! thanks a lot. bravo.

<!-- c_content --> <!-- c_main --> August 10th, 2009 at 9:32 pm <!-- c_meta --> <!-- comment --> <!-- c_gravatar --> <!-- c_sidebar --> stanlick Says: <!-- c_author -->

Well, I have the source showing up on one machine but not another. The machine that does not show source, is effectively a clone of the machine which works ! What exactly are you refreshing when you mention refreshing the Android library in Eclipse?

Peace,
Scott

<!-- c_content --> <!-- c_main --> August 25th, 2009 at 10:56 am <!-- c_meta --> <!-- comment --> <!-- commentlist --> <!-- content -->

Follow It’s Just a Bunch of Stuff That Happens via the Entries and Comments Atom feeds.

<!-- footer --> <!-- page --> <script type="text/javascript"></script><script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script><script type="text/javascript"></script><!-- Dynamic Page Served (once) in 0.874 seconds -->

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 浅析Android防止内存溢出问题
  2. Android野史系列:4.利用 Android Studio开
  3. Android之——自定义下拉菜单的实现
  4. 还在杀进程?Android你不知道的那些事儿
  5. 杂谈Android线程优先级
  6. 从0系统学Android--5.1 广播机制
  7. [置顶] Android学习路线指南
  8. android优化 清除无效代码 UCDetector
  9. android下使用gdb调试工具笔记
  10. android启动过程详解(一)——解析init.rc