使用Android Studio查看Android源码

Android Studio

作为Google的亲儿子,Nexus手机系列所收到的待遇大家有目共睹.Android5.0出来之后,Nexus5第一时间就升级到了最新的系统.那么作为Google亲儿子的Android Studio同样备受Google的重视,我也是第一时间从Eclipse转投到了Android Studio的怀抱中,从最初的测试版本一路升级到现在的1.0正式版本(今天发布了1.0.2,果断已经升级了),关于Android Studio的好处我就不用说了,下面两点就足矣让你转投Android Studio了:

  • Android Studio是Google官方指定的,目前官网已经去掉了ADT, 大家可以在Android开发者官网 中进行查看,目前只有Android Studio提供下载了.
  • Google也表示ADT不再进行维护了.

转投Android Studio时大势所趋,网上关于如何使用Android Studio的帖子也是满天飞,所以我就不再啰嗦夸奖Android Studio了.看一下下面的图,估计你就会很有欲望了.想查看详细的内容,可以查看Android Studio官方文档

Android Studio Preview

Android Studio Preview

Android 5.0 : Lollipop

Android Lollipop是Google在今年推出的,关于Lollipop的详细介绍可以查看Lollipop官方介绍 ,我也就不多说了.作为一个开发者,我们不应该停留在表面(即Lollipop的绚丽的界面,和新奇的设计语言Material design),我们更要了解其中的原理.

准备工作

在使用Android Studio查看源码之前,你需要做下面几件事:

  • 下载Android Lollipop源码(当然不一定非要是Lollipop源码,其他的版本也是可以的)
  • 进行过一次完整编译(不编译是无法进行源码导入工作的)

以上操作可以参考Google 官方教程 以确保万无一失(呵呵).上面的几个链接需要翻墙才可以访问,不过有兴趣看这篇文章的人,大部分应该都是知道VPN的.或者没有VPN不会翻墙的,速度去卖一个VPN,能花钱解决的事情,就不要花时间.

开始

  • 进入Android 源码根目录
  • 修改development/tools/idegen/excluded-paths,防止加载过多目录
    # Default Idegen excluded paths file. Contains regular expressions, one per
    # line, which match paths of directories and .jar files that should be excluded
    # from the IDE configuration.
    #
    # We want to exclude as litte as possible by default, so it's important to
    # document the reason for each exclusion.
    #
    # Developers can also create an 'excluded-paths' file in the project's root
    # directory and add their own excludes to slim down their build.
    #
    # Currently, we lump all the .java files together into one big module, so you
    # can't have two classes with the same name at once. In the future, we'll
    # generate individual modules, each with their own dependencies, much like the
    # build, so we won't have to worry about duplicate names any more than the
    # build does.
    #
    # Note: Google-specific excludes go in vendor/google/excluded-paths.
    
    # Generated API stubs. We only want the originals in our IDE.
    android_stubs
    
    # Extra copies of JUnit.
    ^dalvik/dx/src/junit$
    ^dalvik/libcore/luni/src/test/java/junit$
    
    # Duplicate R.java files. We'll keep the versions from the "intermediates"
    # directories.
    ^out/target/common/R$
    
    # Not actually built. Also disabled in make file.
    ^development/samples/MySampleRss$
    ^development/apps/OBJViewer$
    ^packages/apps/IM/samples/PluginDemo$
    
    # We don't currently support development tool source code. Development tools
    # typically have their own IDE configurations anyway.
    #
    # The main problem is that the development tools are meant to run against a
    # fully featured desktop VM while almost everything in our IDE configuration
    # is meant to run against Android's more limited APIs. Mixing the two
    # environments in one IDE configuration doesn't work well.
    ^development/tools$
    ^external/jdiff$
    ^external/emma$
    ^external/clearsilver$
    layoutlib
    ^prebuilt/.*\.jar$
    ^dalvik/.*\.jar$
    ^build/tools/droiddoc$
    
    # Each test has a Main.java in the default package, so they conflict with
    # each other.
    ^dalvik/tests$
    
    # We can only support one policy impl at a time.
    ^frameworks/policies/base/mid$
    #^frameworks/policies/base/phone$
    
    # We don't want compiled jars.
    ^out/.*\.jar$
    
    # This directory contains only an R.java file which is the same as the one in
    # Camera_intermediates.
    ^out/target/common/obj/APPS/CameraTests_intermediates$
    
    # Exclude all prebuilts jars.
    ^prebuilts/.*\.jar$
    
    #add by zhangwenchao for frameworks
    ^art$
    ^bionic$
    ^bootable$
    ^build$
    ^compatibility$
    ^cts$
    ^dalvik$
    ^developers$
    ^development$
    ^device$
    ^docs$
    ^external$
    ^frameworks/av$
    ^frameworks/compile$
    ^frameworks/data-binding$
    ^frameworks/ex$
    ^frameworks/hardware$
    ^frameworks/layoutlib$
    ^frameworks/minikin$
    ^frameworks/ml$
    ^frameworks/multidex$
    ^frameworks/native$
    ^frameworks/opt$
    ^frameworks/rs$
    ^frameworks/support$
    ^frameworks/webview$
    ^frameworks/wilhelm$
    ^frameworks/base/apct-tests$
    ^frameworks/base/api$
    ^frameworks/base/cmds$
    ^frameworks/base/config$
    ^frameworks/base/data$
    ^frameworks/base/docs$
    ^frameworks/base/drm$
    ^frameworks/base/legacy-test$
    ^frameworks/base/libs$
    ^frameworks/base/samples$
    ^frameworks/base/test-runner$
    ^frameworks/base/tests$
    ^frameworks/base/tools$
    ^frameworks/base/core/tests$
    ^frameworks/base/packages$
    ^frameworks/base/services/tests$
    ^hardware$
    ^kernel$
    ^libcore$
    ^libnativehelper$
    ^out$
    ^packages$
    ^pdk$
    ^platform_testing$
    ^prebuilts$
    ^sdk$
    ^shortcut-fe$
    ^system$
    ^test$
    ^toolchain$
    ^tools$
    ^vendor$
    # need modify
    ^out-PD1806MA$
  • 执行
    mmm development/tools/idegen/
    这行命令的意思是编译idegen这个项目,生成idegen.jar文件.生成成功后,会显示这个jar包的位置,并显示 #### make completed successfully (43 seconds) #### ,如果编译失败了,后面会讲到这种情况(比较少见)
  • 执行
    sh ./development/tools/idegen/idegen.sh
    这行命令的意思是生成对应的文件:android.iws, android.ipr, android.iml .如果生成失败,后面也会讲到这种情况.
  • 生成对应的文件后,打开Android Studio,选择打开一个现有的Android Studio项目,选择Android源码的根目录,导入即可(起作用的是android.irp文件).在配置sdk版本之后就可以查看Android 源码了.
  • 已经完成了,没有下一步了.

问题

  • 执行第一个命令的时候编译不过,出现这种情况的原因有好几种:

    • Android 源码没有下载完全
    • 没有进行全部编译
    • 环境变量配置有问题

      解决方法依问题而定,网上对应的帖子很多,可以自己去找. 推荐这种问题还是去Google 和StackOverFlow 上去问.

  • 执行第二个命令的时候,碰到下面的问题:

    Exception in thread "main" java.io.FileNotFoundException: ./out/target/product/hammerhead/obj/GYP/shared_intermediates/res.java (Is a directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileReader.<init>(FileReader.java:72)
    at Configuration.parsePackageName(Configuration.java:204)
    at Configuration.rootOf(Configuration.java:180)
    at Configuration.traverse(Configuration.java:140)
    at Configuration.traverse(Configuration.java:167)
    at Configuration.traverse(Configuration.java:167)
    at Configuration.traverse(Configuration.java:167)
    at Configuration.traverse(Configuration.java:167)
    at Configuration.traverse(Configuration.java:167)
    at Configuration.traverse(Configuration.java:167)
    at Configuration.traverse(Configuration.java:167)
    at Configuration.<init>(Configuration.java:72)
    at Main.main(Main.java:37)

    解决办法是将
    ./out/target/product/hammerhead/obj/GYP/shared_intermediates/res.java
    修改为:
    ./out/target/product/hammerhead/obj/GYP/shared_intermediates/res.j
    即可.

后记

使用Android Studio看Android源码很爽,下面是他们的优缺点:

优点
  • 和Android 完美结合
  • 代码提示很全
  • 代码跳转很强
  • IDE比Eclipse好看(我说的是黑色主题)
  • 插件很强大(底层是IDEA你懂得)
  • 免费(SourceInsight负分,啥?你用的破解版? 当我没说)
缺点
  • 对C++代码的支持不是很好
  • 有一定的上手难度

转载于:https://my.oschina.net/chaoge/blog/803615

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值