Android Studio导入Android整个系统源码

简介

【本文章转自https://blog.csdn.net/QQxiaoqiang1573/article/details/72903237】

 由于怕博文丢失就拷一份过来,很实用

偶然发现一个神器idegen,通过它我们可以用Android Studio阅读整个系统源码,非常方便。话不多说直接来看怎么使用idegen,导入系统源码到Android Studio。

见证奇迹的时候

Windows环境下,将整个Android系统源码导入到Android Studio中。 
首先,我们要做的就是在源码环境下,即Linux环境下,执行以下命令:

 
  1. $ source build/envsetup.sh

  2. $ lunch aosp_x86-eng #(or pick your favorite lunch target)

  3. $ make

  4. $ mmm development/tools/idegen/

  5. $ development/tools/idegen/idegen.sh

  • 1
  • 2
  • 3
  • 4
  • 5

注:上面的编译命令以实际的系统编译命令为准 
依次执行完上面的命令,就会在系统根目录下生成如下文件

 
  1. .classpath (Eclipse)

  2. android.ipr (IntelliJ / Android Studio)

  3. android.iml (IntelliJ / Android Studio)

  • 1
  • 2
  • 3

因为我这用的是Android Studio导入源码,所以我们只需要选择android.ipr这个文件即可。同时,在development/tools/idegen/目录下有个README文件,我们来看下其中的内容。 
development/tools/idegen/README

 
  1. IDEGen automatically generates Android IDE configurations for IntelliJ IDEA

  2. and Eclipse. Your IDE should be able to compile everything in a reasonable

  3. amount of time with no errors.

  4.  
  5. If you're using IntelliJ...

  6.  
  7. If this is your first time using IDEGen...

  8.  
  9. IDEA needs a lot of memory. Add "-Xms748m -Xmx748m" to your VM options

  10. in "IDEA_HOME/bin/idea.vmoptions" on Linux or

  11. "IntelliJ IDEA.app/Contents/Info.plist" on OS X.

  12.  
  13. Create a JDK configuration named "1.6 (No Libraries)" by adding a new

  14. JDK like you normally would and then removing all of the jar entries

  15. under the "Classpath" tab. This will ensure that you only get access to

  16. Android's core libraries and not those from your desktop VM.

  17.  
  18. From the project's root directory...

  19.  
  20. Repeat these steps after each sync...

  21.  
  22. 1) make (to produce generated .java source)

  23. 2) development/tools/idegen/idegen.sh

  24. 3) Open android.ipr in IntelliJ. If you already have the project open,

  25. hit the sync button in IntelliJ, and it will automatically detect the

  26. updated configuration.

  27.  
  28. If you get unexpected compilation errors from IntelliJ, try running

  29. "Build -> Rebuild Project". Sometimes IntelliJ gets confused after the

  30. project changes significantly.

  31.  
  32. If you're using Eclipse...

  33.  
  34. If this is your first time using IDEGen...

  35.  
  36. Edit eclipse.ini ("Eclipse.app/Contents/MacOS/eclipse.ini" on OS X) and

  37. add "-Xms748m -Xmx748m" to your VM options.

  38.  
  39. Configure a JRE named "1.5 (No Libraries)" under "Preferences -> Java ->

  40. Installed JREs". Remove all of the jar entries underneath "JRE system

  41. libraries". Eclipse will not let you save your configuration unless at

  42. least one jar is present, so include a random jar that won't get in the

  43. way.

  44.  
  45. From the project's root directory...

  46.  
  47. Repeat these steps after each sync...

  48.  
  49. 1) make (to produce generated .java source)

  50. 2) development/tools/idegen/idegen.sh

  51. 3) Import the project root directory into your Eclipse workspace. If you

  52. already have the project open, simply refresh it (F5).

  53.  
  54. Excluding source roots and jars

  55.  
  56. IDEGen keeps an exclusion list in the "excluded-paths" file. This file

  57. has one regular expression per line that matches paths (relative to the

  58. project root) that should be excluded from the IDE configuration. We

  59. use Java's regular expression parser (see java.util.regex.Parser).

  60.  
  61. You can create your own additional exclusion list by creating an

  62. "excluded-paths" file in the project's root directory. For example, you

  63. might exclude all apps except the Browser in your IDE configuration with

  64. this regular expression: "^packages/apps/(?!Browser)".

  65.  
  66. Controlling source root ordering (Eclipse)

  67.  
  68. You may want some source roots to come before others in Eclipse. Simply

  69. create a file named "path-precedence" in your project's root directory.

  70. Each line in the file is a regular expression that matches a source root

  71. path (relative to the project's root directory). If a given source root's

  72. path matches a regular expression that comes earlier in the file, that

  73. source root will come earlier in the generated configuration. If a source

  74. root doesn't match any of the expressions in the file, it will come last,

  75. so you effectively have an implicit ".*" rule at the end of the file.

  76.  
  77. For example, if you want your applications's source root to come first,

  78. you might add an expression like "^packages/apps/MyApp/src$" to the top

  79. of the "path-precedence" file. To make source roots under ./out come last,

  80. add "^(?!out/)" (which matches all paths that don't start with "out/").

  81.  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

上面的文档介绍了IntelliJ和eclipse怎么导入系统源码的方法,这里我们就只使用Android Studio来导入源码。所以就不管eclipse部分的东西了。因为Android Studio的前身就是IntelliJ,所以Android Stduio的导入方法和IntelliJ是一样的。 
在执行完上面Linux系统部分的编译工作后,我们就可以回到Windows环境了,来看看我们怎么用Android Studio导入系统源码。 
第一步: 
找到Android Studio的安装路径中的vmoptions文件并打开:”Android Studio Path”/bin/studio.exe.vmoptions或者studio64.exe.vmoptions 
添加

 
  1. -Xms748m

  2. -Xmx748m

  • 1
  • 2

到其文件中。 
这里写图片描述
第二步: 
打开Android Studio,选择File->Open 
弹出路径选择框,输入相应的源码根路径,然后选择android.ipr文件,就开始导入源码啦。在导入过程中,需要等待索引的建立相关的关联,大概需要花10~20分钟。 
这里写图片描述
这里写图片描述
第三步: 
当我们导入完源码后,我们就可以查看整个系统的源码啦,但是我们在跳转公共的类时,并没有跳转到frameworks目录下对应的源码类,而是jar包中的类,这不是我们想要的,我们需要让其跳转到相应的类中。我们就需要新建一个没有任何jar库的SDK给到系统源码项目的依赖。 
那我们要怎么创建一个没有jar的SDK呢?步骤如下: 
右键项目名->Open Module Settings->Project->New(JDK) 
然后选择SDKs->编辑刚刚添加的jdk->删除Classpath目录下的所有包并命名为1.6(No Libraries) 
再选择Modules->Module SDK选择刚刚编辑的SDK(1.6(No Libraries)),并删除下面所有的包。 
大功告成,完成上面的步骤后,公共的类就直接指向系统的源码啦。 
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

参考文章

AOSP Sources in the IDE

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值