`
xzqttt
  • 浏览: 9074 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

View Android Source Code in Eclipse

阅读更多
Recently, I have been hacking around with Android a lot, and I think it's awesome.

Unfortunately, the Android SDK does not come with source code, and sometimes the docs are not so great, yet. Being an open source project, the Android sources are available online, but they are not very easy to access from Eclipse. For one, the source files are sometimes difficult to find as they are distributed over many individual Git repositories, and then I am not always online.

Integrating source files into Eclipse is usually quite easy, but the Android Eclipse plugin (ADT) does not allow to modify the Java Source Attachment of android.jar in the Android Library. Fortunately, you only have to find out what the default location for source code is. Eric Burke nicely describes how to find that out.

Long story short: Android source code needs to be placed in a "sources" subdirectory of the Android SDK. Here is what you need to do:

   1. Get the Android source code (install repo, repo init, repo sync)
   2. Move all Java sources into a "sources" subdirectory of the Android SDK

Step 2 sounds easier than it is. There are a lot of Java files in the Android sources, and they are sprinkled all over the place. Eclipse, however, needs the source files in a standard Java source directory structure: The path of a source file needs to match its package name. To simplify this task, I have written a short Python script that recursively searches for Java files and packs them into a ZIP file. Unpack that source file into a "sources" subdirectory of the Android SDK. Enjoy:

from __future__ import with_statement  # for Python < 2.6

import os
import re
import zipfile

# open a zip file
DST_FILE = 'sources.zip'
if os.path.exists(DST_FILE):
  print DST_FILE, "already exists"
  exit(1)
zip = zipfile.ZipFile(DST_FILE, 'w', zipfile.ZIP_DEFLATED)

# some files are duplicated, copy them only once
written = {}

# iterate over all Java files
for dir, subdirs, files in os.walk('.'):
  for file in files:
    if file.endswith('.java'):
      # search package name
      path = os.path.join(dir, file)
      with open(path) as f:
        for line in f:
          match = re.match(r'\s*package\s+([a-zA-Z0-9\._]+);', line)
          if match:
            # copy source into the zip file using the package as path
            zippath = match.group(1).replace('.', '/') + '/' + file
            if zippath not in written:
              written[zippath] = 1
              zip.write(path, zippath)
            break;
        
zip.close()

Posted by Dr. Michael Forster at 4:50 PM
分享到:
评论

相关推荐

    Beginning Android 2 Source Code

    《初识Android 2源码》是针对Android开发入门的学习资料,主要面向使用Eclipse 3.5(对应android-sdk-windows-2.0.1版本)的开发者。这本书的源代码提供了深入理解Android 2系统运行机制和开发流程的宝贵资源。 ...

    Source code for Beginning Android Application Development

    - **Eclipse与Android开发**:讲解如何使用Eclipse IDE配置Android项目,并创建第一个Hello World应用。 2. **Chapter 2:Android应用结构** - **项目结构**:解析Android项目的目录结构,如src、res、...

    Android Game Space Blaster APK and Eclipse Project Source Code

    This is an android game from souce code of book Apress Pro Android Games with some update by me to study android view customize.

    android SDK 源码

    本压缩包文件“android_sdk_source_code”提供了Android 4.1(代号Jelly Bean)的SDK源码,这对于开发者来说是一个宝贵的学习资源,特别是对那些希望进行系统级开发或者进行自定义修改的人员。 首先,Android 4.1 ...

    pro android 4 pdf and source code

    1. **Android开发环境搭建**:书中会详细讲解如何设置Java开发环境,安装Android SDK,配置Eclipse或Android Studio IDE,以及使用AVD(Android Virtual Device)进行模拟器测试。 2. **Android架构与组件**:介绍...

    基于Eclipse-JDT生成Java源代码的AST并可视化.zip

    parser.setSource(sourceCode.toCharArray()); // sourceCode为Java源代码字符串 parser.setKind(ASTParser.K_COMPILATION_UNIT); // 表示我们要解析的是整个编译单元 ``` 接下来,调用`parse()`方法生成AST对象: ...

    Eclipse 常用设置&操作

    同时,你可以在`java-&gt;editor-&gt;Save Actions`中启用“Format Source Code”和“Organize Imports”,这样每次保存时,Eclipse会自动进行格式化和导入排序。 在Eclipse中,选择正确的JRE版本也十分重要。在`Java-&gt;...

    Eclipse插件\快捷键

    Code Folding - Code Folding in Eclipse Source Code Editors - **Code Folding**:支持在 Eclipse 源代码编辑器中进行代码折叠,提高代码可读性。 **下载地址**:...

    Eclipse常用及技巧

    8. **源码控制(Source Control)** - Eclipse集成了多种源码控制系统,如Git、SVN,方便进行版本管理和团队协作。 9. **市场(Eclipse Marketplace)** - Eclipse Marketplace提供了大量第三方插件,用户可以...

    Eclipse中文使用教程

    2 .12 快速视图 (Fast View ) 2 .13 比较 2 .14 历史纪录 2 .15 回应 UI 3 .喜好设定 (Preferences ) 3 .1工作台 ( Workbench ) 3.2 Ant 3 .3建置次序 ( Build Order ) 3 .4说明 ( Help ) 3 .5自动更新 ( Install/ ...

    SagarTextEditor-Source-Code:这是使用Eclipse RCP创建的SagarTextEditor应用程序的源代码。-Source code editor

    1. **编辑器视图(Editor View)**:在Eclipse RCP中,编辑器视图是用户与数据交互的主要界面。SagarTextEditor的源代码中,我们能看到如何创建一个定制的编辑器,支持基本的文本操作,如编辑、查找替换、缩进调整等...

    android 源码

    选择"File" &gt; "Import" &gt; "Existing Android Code into Workspace",然后选择你的项目目录。 4. **关联源码**:右键点击项目,选择"Properties" &gt; "Java Build Path" &gt; "Source",然后点击"Link Source..."按钮。在...

    新版Android开发教程.rar

    � Eclipse JDT plugin (included in most Eclipse IDE packages) � WST (optional, but needed for the Android Editors feature; included in most Eclipse IDE packages ) o JDK 5 or JDK 6 (JRE alone is not ...

    eclipse开发简单入门

    3. **程序代码完成功能**:Eclipse提供Code Completion和Code Assist功能,帮助开发者快速编写代码。 4. **执行Java程序**:可以通过右击项目或类,选择“Run As”&gt;“Java Application”来运行Java程序。 #### 总结...

    Eclipse下的可视化界面设计WindowBuilder插件所有版本打包

    Source View – write code and review the generated code Structure View – composed of the Component Tree and the Property Pane. Component Tree – shows the hierarchical relationship between all of the...

    Eclipse – 整合开发工具

    要訣和技巧(Tips and Tricks) 266 7.1編輯程式檔(Editing Source) 266 7.2搜尋(Searching) 271 7.3程式碼導覽和讀取(Code navigation and reading) 273 7.4 Java視圖(Java views) 277 7.5...

    Eclipse使用方法研究

    **视图(View):** - 视图是用来显示相关信息的面板,例如“资源管理器”视图显示项目中的文件结构,“控制台”视图显示程序输出的信息。 - 用户可以根据需要定制显示哪些视图。 **编辑器(Editor):** - 编辑器...

    Google SVN eclipse环境配置

    3. 回到Eclipse,点击顶部菜单栏的“窗口”(Window)选项,选择“显示视图”(Show View),然后在弹出的子菜单中选择“其他”(Other)。 4. 在展开的视图列表中,找到“SVN”类别,然后选择“资源库”( ...

Global site tag (gtag.js) - Google Analytics