`

Browsing Android Source in Eclipse

阅读更多

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 --> <!-- 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 --> <!-- 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 --> <!-- 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 --> <!-- 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 --> <!-- c_meta -->
<!-- comment -->
<!-- c_gravatar -->
<!-- c_sidebar -->
<!-- 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 --> <!-- c_meta -->
<!-- comment -->
<!-- c_gravatar -->
<!-- c_sidebar -->
apple Says:
<!-- c_author -->

Refresh did the trick! Thank you Josh.

<!-- c_content -->
<!-- c_main --> <!-- 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 --> <!-- 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 --> <!-- c_meta -->
<!-- comment -->
<!-- c_gravatar -->
<!-- c_sidebar -->
<!-- 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 --> <!-- 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 --> <!-- 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 --> <!-- 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 --> <!-- 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 --> <!-- 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 --> <!-- 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 --> <!-- c_meta -->
<!-- comment -->
<!-- c_gravatar -->
<!-- c_sidebar -->
Rick Bing Says:
<!-- c_author -->

Great job. You are Numero Uno.

<!-- c_content -->
<!-- c_main --> <!-- 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 --> <!-- 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 --> <!-- c_meta -->
<!-- comment -->
<!-- c_gravatar -->
<!-- c_sidebar -->
Jn Says:
<!-- c_author -->

Thanks a lot!

<!-- c_content -->
<!-- c_main --> <!-- 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 --> <!-- 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 --> <!-- 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 -->
分享到:
评论

相关推荐

    基于linux与windows平台下 如何下载android sdk源代码的方法详解

    具体的步骤可以参考外部链接`http://stuffthathappens.com/blog/2008/11/01/browsing-android-source-in-eclipse/`。这里是一个简化的流程: 1. **创建源代码目录** - 在Eclipse的Android SDK目录下(通常包含`...

    基于机器学习的疾病数据集分析

    该代码使用scikit-learn的乳腺癌数据集,完成分类模型训练与评估全流程。主要功能包括:数据标准化、三类模型(逻辑回归、随机森林、SVM)的训练、模型性能评估(分类报告、混淆矩阵、ROC曲线)、随机森林特征重要性分析及学习曲线可视化。通过`train_test_split`划分数据集,`StandardScaler`标准化特征,循环遍历模型进行统一训练和评估。关键实现细节包含:利用`classification_report`输出精确度/召回率等指标,绘制混淆矩阵和ROC曲线量化模型效果,随机森林的特征重要性通过柱状图展示,学习曲线分析模型随训练样本变化的拟合趋势。最终将原始数据和预测结果保存为CSV文件,便于后续分析,并通过matplotlib进行多维度可视化比较。代码结构清晰,实现了数据处理、模型训练、评估与可视化的整合,适用于乳腺癌分类任务的多模型对比分析。

    PyTorch入门指南:从零开始掌握深度学习框架.pdf

    内容概要:本文作为PyTorch的入门指南,首先介绍了PyTorch相较于TensorFlow的优势——动态计算图、自动微分和丰富API。接着讲解了环境搭建、PyTorch核心组件如张量(Tensor)、autograd模块以及神经网络的定义方式(如nn.Module),并且给出了详细的神经网络训练流程,包括前向传播、计算损失值、进行反向传播以计算梯度,最终调整权重参数。此外还简要提及了一些拓展资源以便进一步探索这个深度学习工具。 适用人群:初次接触深度学习技术的新学者和技术爱好者,有一定程序基础并希望通过PyTorch深入理解机器学习算法实现的人。 使用场景及目标:该文档有助于建立使用者对于深度学习及其具体实践有更加直观的理解,在完成本教程之后,读者应当能够在个人设备上正确部署Python环境,并依据指示独立创建自己的简易深度学习项目。 其他说明:文中所提及的所有示例均可被完整重现,同时官方提供的资料链接也可以方便有兴趣的人士对感兴趣之处继续挖掘,这不仅加深了对PyTorch本身的熟悉程度,也为未来的研究或者工程项目打下了良好的理论基础和实践经验。

    基于Springboot框架的高校心理教育辅导管理系统的设计与实现(含完整源码+完整毕设文档+数据库文件).zip

    此高校心理教育辅导系统功能分析主要分为管理员功能模块、教师功能模块和学生功能模块三大模块,下面详细介绍这三大模块的主要功能: (1)管理员:管理员登陆后可对系统进行全面管理,管理员主要功能模块包括个人中心、学生管理、教师管理、辅导预约管理、学生信息管理、测评结果分析管理、心理健康学习管理、试题管理、留言板管理、试卷管理、系统管理以及考试管理,管理员实现了对系统信息的查看、添加、修改和删除的功能。管理员用例图如图3-1所示。(2)学生:学生进入本高校心理教育辅导系统前台可查看系统信息,包括首页、心理健康信息、试卷列表、公告通知以及留言反馈等,注册登录后主要功能模块包括个人中心、辅导预约管理以及考试管理。(3)教师:教师学生登录后主要实现的功能模块包括个人中心、辅导预约管理、学生信息管理、测试结果分析管理、心理健康学习管理、试卷管理、试题管理、留言板管理、考试管理。Spring Boot是一个简化程序设置的拥有开箱即用的框架,它主要的优点是根据程序员不同的设置而生成不同的代码配置文件,这样开发人员就不用每个项目都配置相同的文件,从而减低了开发人员对于传统配置文件的时间,提高了开发效率。它内

    网络文化互动中的虚拟现实技术应用.doc

    网络文化互动中的虚拟现实技术应用

    自驾游中如何预防迷路情况.doc

    自驾游中如何预防迷路情况

    实现多人聊天的客户端小程序

    实现多人聊天的客户端小程序

    空间误差分析:统一的应用导向处理 附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    漫画中的文化元素挖掘.doc

    漫画中的文化元素挖掘

    【Bender】基于Bender进行光线追踪研究 附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    ,,Qt源程序~界面设计例程(XML文件读取+滚动区域放置控件+保存多sheetExcel文件) IDE版本: Qt creator 4.8.0 Qt 5.12.0 代码特点: 1.能读取xml格式文

    ,,Qt源程序~界面设计例程(XML文件读取+滚动区域放置控件+保存多sheetExcel文件) IDE版本: Qt creator 4.8.0 Qt 5.12.0 代码特点: 1.能读取xml格式文件,并通过其配置界面; 2.能在滚动区域内放置多种控件,界面大小不够会出现滚动条来扩展界面; 3.能通过xml配置文件初始化联动的单选框,输入框和表格; 4.通过程序动态新建单选框,输入框和表格; 5.将表格保存为Excel文件,每个表格就是一个sheet。 视频不够清晰,请上B站看: 【Qt例程:界面设计项目(XML文件读取+滚动区域放置控件+保存Excel文件)- ,Qt源程序; XML文件读取; 滚动区域放置控件; 保存多sheet Excel文件; Qt Creator 4.8.0; Qt 5.12.0; 动态创建控件; 界面设计例程。,Qt程序进阶:XML文件读取与处理,滚动区域控件布局,多sheet Excel文件保存功能

    ,,FPGA 以太网 UPD IP 协议实现 fpga 千兆以FPGA 以太网 UPD IP 协议实现 fpga 千兆以FPGA 以太网 UPD IP 协议实现, fpga 千兆以太网接口控制器

    ,,FPGA 以太网 UPD IP 协议实现 fpga 千兆以FPGA 以太网 UPD IP 协议实现 fpga 千兆以FPGA 以太网 UPD IP 协议实现, fpga 千兆以太网接口控制器,FPGA UDP IP协议实现 在FPGA上实现UDP通信,Verilog HDL描述语言实现,数据链路层,网络层,传输层有纯逻辑实现。 接口为GMII接口,与外部phy对接。 实验器件为s6,因此编译环境用的是ISE14.7。 vivado轻松无压力,随意移植。 ,FPGA; 以太网; UPD; IP协议; 千兆以太网接口控制器; Verilog HDL描述语言; 数据链路层; 网络层; 传输层; 接口为GMII接口; 编译环境为ISE14.7。,基于FPGA的千兆以太网UDP IP协议实现与优化

    eclipse-inst-jre-win64.rar

    eclipse-inst-jre-win64.rar

    Matlab实现基于BO贝叶斯优化Transformer结合BiLSTM双向长短期记忆神经网络时间序列预测的详细项目实例(含完整的程序,GUI设计和代码详解)

    内容概要:本文档详细介绍了一个基于Transformer和BiLSTM双向长短期记忆神经网络结合贝叶斯优化(BO)进行时间序列预测的项目。该项目主要解决传统方法在处理复杂非线性关系、多变量依赖和大规模数据时存在的局限性,提升预测精度和计算效率。项目通过MATLAB实现完整的程序、GUI设计和详细的代码说明,涵盖数据预处理、模型设计与训练、超参数调优、评估与应用等各个环节。同时探讨了项目的挑战和未来改进方向,为深度学习技术在时间序列预测中的应用提供了实用价值。 适合人群:对时间序列预测感兴趣的研究人员和技术人员,尤其是具有一定深度学习基础并且希望深入了解和实践Transformer、BiLSTM及相关优化技术的专业人士。 使用场景及目标:①为金融、能源、气象等多个领域的实际问题提供时间序列预测解决方案,包括股市预测、电力负载预估等;②提高预测模型的泛化能力和准确性;③优化模型的超参数选取,从而提高训练速度和效率。 其他说明:文中特别强调了数据处理的重要性,如去除噪声、特征选择等问题,并介绍了贝叶斯优化技术的应用,使得模型能够在较少尝试下找到最优配置。同时展示了如何通过图形化界面展示训练过程和评估结果,确保用户体验友好。此外,文档还包括了防止过拟合、提高模型性能的各种技巧,如正则化、早期停止、Dropout等措施。总体而言,本项目致力于提供一套完善的深度学习解决方案,促进跨学科应用和发展。

    励志图书中的时间管理、目标设定与自我提升.doc

    励志图书中的时间管理、目标设定与自我提升

    HarmonyOS NEXT 闯关习题答案(无解析)

    当前资源包含初中高级闯关习题

    亲子自驾游趣味活动推荐.doc

    亲子自驾游趣味活动推荐

    自然语言处理领域的深度双向变压器预训练模型BERT及其应用

    内容概要:本文介绍了BERT(Bidirectional Encoder Representations from Transformers),它是一种新型的语言表示模型,通过利用掩码语言模型(MLM)和下一句预测任务(NSP),实现了从无标注文本中预训练深层双向表示模型的方法。这种双向注意力机制允许模型在同一层联合调节左右语境,极大地提升了下游自然语言处理任务的性能。与单向语言模型如ELMo、GPT不同,BERT能直接捕捉句子内部复杂的依存关系,在多项NLP基准测试中刷新了记录,显著优于以前的最佳表现。 适合人群:从事自然语言处理研究的技术人员以及对该领域有兴趣的研究学者和开发者。 使用场景及目标:适用于需要高级别自然语言理解和推理能力的任务,特别是涉及问答系统、机器翻译和情感分析等任务的研发团队和技术部门。通过采用BERT可以快速提高相关应用场景中的精度。 其他说明:BERT不仅展示了双向建模相对于传统单向方法的优势,还强调了充分预训练对于改善小型数据集上模型表现的关键作用。此外,文中还详细比较了与其他几种现有先进模型的特点,并提供了具体的实验设置和技术细节供进一步探究。

    漫画作品与网络文化互动.doc

    漫画作品与网络文化互动

Global site tag (gtag.js) - Google Analytics