Use Android Studio to import Android source code

1 background

This article mainly explains how to use it to see the relevant configuration of the source code (this example is based on the official source code for configuration instructions). In the android source code, I found that there is a development/tools/idegen directory. I checked and found that it is the project file that generates the IDE (mainly the project file of intellij, which is actually Android Studio), so the welfare tool comes like this. For details, see below.

2 Configuration process

2-1 Basic preparations

First of all, you have to install and configure Android Studio, and then you have to download the source code. As for how to download it, I won’t explain it here. It’s relatively simple, just check it on the official website.

Secondly, you need to ensure that the source code has been compiled to generate out-related directory files.

Well, the above are some basic preparations. For custom source code or native source code, these preparation steps are necessary, so there is no need to emphasize it.

2-2 Configuration import and use

2-2-1 Compile source code idegen module and generate AS configuration file (*.ipr)

Before starting to compile the idegen module, you must know that you need to compile the out directory and related files globally first. I won’t say too much about this. We compile the idegen module with the following command:

mmm development/tools/alien/

The meaning of this line of command is to compile the idegen module project, and then generate the idegen.jar file (if you don’t believe me, go and check the Android.mk target of this module, no more explanation). After running, if you see the following information, it means that the compilation is OK:

......
#### make completed successfully (7 seconds) ####
 
 

Then execute the following script:

development/tools/alien/alien.sh

This command means to generate the corresponding android.ipr, android.iml IEDA project configuration files in the root directory. Wait for a while to get a message similar to the following to indicate OK:

Read excludes: 21ms
Traversed tree: 194799ms
 
 

Through the above operations, we have completed the basic source code configuration work.

2-2-2 Some customization operations before importing into Android Studio

Everyone knows that using Eclipse to import source code is very slow, and Android Studio is also slow when importing source code, so it is recommended to modify the android.iml file (remove the code that you do not use), and then import it into Studio.

Just like the ones starting from line 2237 of the android.iml file extracted below:

<sourceFolder url="file://$MODULE_DIR$/sdk/testapps/testProjectTest/testlib/src" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/sdk/testapps/testSensors/src" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/sdk/testapps/userLibTest/src" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/tools/external/fat32lib/src/main/java" isTestSource="false" />
      <excludeFolder url="file://$MODULE_DIR$/.repo" />
      <excludeFolder url="file://$MODULE_DIR$/external/bluetooth" />
      <excludeFolder url="file://$MODULE_DIR$/external/chromium" />
      <excludeFolder url="file://$MODULE_DIR$/external/emma" />
      <excludeFolder url="file://$MODULE_DIR$/external/icu4c" />
      <excludeFolder url="file://$MODULE_DIR$/external/jdiff" />
      <excludeFolder url="file://$MODULE_DIR$/external/webkit" />
      <excludeFolder url="file://$MODULE_DIR$/frameworks/base/docs" />
      <excludeFolder url="file://$MODULE_DIR$/out" />
      <excludeFolder url="file://$MODULE_DIR$/out/eclipse" />
      <excludeFolder url="file://$MODULE_DIR$/out/host" />
      <excludeFolder url="file://$MODULE_DIR$/out/target/common/docs" />
      <excludeFolder url="file://$MODULE_DIR$/out/target/common/obj/JAVA_LIBRARIES/android_stubs_current_intermediates" />
      <excludeFolder url="file://$MODULE_DIR$/out/target/product" />
      <excludeFolder url="file://$MODULE_DIR$/prebuilt" />

<excludeFolder url="file://$MODULE_DIR$/.repo"/>We can filter out unnecessary content in the same way as the above code , so that it will be much faster when importing.

You can also open the Modules on the left through the Project Stucture of Android Studio, and then exclude some directories in the Sources on the right.

2-2-3 Use Android Studio to import source code project

With the above operations, we open Android Studio, then choose to open an existing Android Studio project, and then choose to open the android.ipr file in the root directory of the source code (File --> Open...), and then appear as The same index as in the picture can wait for the import:


So far we can easily use Android Studio to view the source code (Ctrl+Shift+N or Navigate --> File). As shown below:


Awesome! ! !

2-3 Use skills

As we can see in the picture above, we can search the whole set of source code through Android Studio. But at this time if you open a Launcher.javaClass, and then put the mouse on the String parameter of any method, hold down CTRL + left mouse button to jump to the String class, you will find that the file you jumped to is a String.class file, why? Because he jumps to the class file inside the jar in your default SDK. Since we want to modify and view the entire set of source code, it is so painful to jump, so we need to configure it to jump to the String.java file. The specific method is as follows:

First delete all the dependencies in the dependencies, and only keep the two that are not selected in the figure below (of course you can choose to keep some other jars you use),

Then click the plus sign of JARs or directories... Add the frameworks and external of your source code and other jump directories you use to the dependencies, and then apply, as follows:

(Special attention needs to be paid here: the Android API 23 item should be moved to the end, otherwise when jumping, it will still jump to String.class in the SDK first)


At this time, we open Launcher.java to jump to String as above, and you will find that it directly jumps to the String.java file under your source code path as shown in the figure below, as follows:

On the android root directory, press the shortcut key F4 (if it has been set as the Eclipse shortcut key, it will be F12) or File --> Project Structure to open the Module Setting,


Select the directory that does not need to be scanned and click Excluded in the upper right corner (the .repo, out and prebuilts directories are relatively large, it is recommended to choose), after completion, the effect is as shown on the left, and the folders are colored differently. The red ones are the excluded folders. AS will not scan the contents. Restart the AS, you will find that the time to scan the source code directory is greatly reduced.


Tips: Because the configuration and cache files of Android Studio exist in the home/.AndroidStudio (Windows system is in C:\Users\Administrator\.AndroidStudio2.1 ) folder, a long time may cause the system disk to be tight. If you want to modify the default storage location (such as changing to another disk), you need to modify the relevant configuration information in the android-studio/bin/idea.properties file, modify the content reference:


Fourth, configure the JDK and SDK of AS

This is enough for those who usually just view the source code.

3 summary

As the saying goes, if you want to do a good job, you must first sharpen your tools. You will find that using Android Studio is much better than Eclipse and Source Insight (of course, SubText is still very powerful), whether it is code prompts or jumps, or Python Shell, etc. Plug-in support and so on are very powerful, experience it slowly.

By the way, Google's ambitions are really great. It seems that Android Studio is about to be built by them as an all-round IDE.

4 Additional README

Attach the original English README document, English masters can just look here:

IDEGen automatically generates Android IDE configurations for IntelliJ IDEA
and Eclipse. Your IDE should be able to compile everything in a reasonable
amount of time with no errors.

If you’re using IntelliJ…

If this is your first time using IDEGen...

    IDEA needs a lot of memory. Add "-Xms748m -Xmx748m" to your VM options
    in "IDEA_HOME/bin/idea.vmoptions" on Linux or
    "IntelliJ IDEA.app/Contents/Info.plist" on OS X.

    Create a JDK configuration named "1.6 (No Libraries)" by adding a new
    JDK like you normally would and then removing all of the jar entries
    under the "Classpath" tab. This will ensure that you only get access to
    Android's core libraries and not those from your desktop VM.

From the project's root directory...

    Repeat these steps after each sync...

    1) make (to produce generated .java source)
    2) development/tools/idegen/idegen.sh
    3) Open android.ipr in IntelliJ. If you already have the project open,
       hit the sync button in IntelliJ, and it will automatically detect the
       updated configuration.

    If you get unexpected compilation errors from IntelliJ, try running
    "Build -> Rebuild Project". Sometimes IntelliJ gets confused after the
    project changes significantly.

If you’re using Eclipse…

If this is your first time using IDEGen...

    Edit eclipse.ini ("Eclipse.app/Contents/MacOS/eclipse.ini" on OS X) and
    add "-Xms748m -Xmx748m" to your VM options.

    Configure a JRE named "1.5 (No Libraries)" under "Preferences -> Java ->
    Installed JREs". Remove all of the jar entries underneath "JRE system
    libraries". Eclipse will not let you save your configuration unless at
    least one jar is present, so include a random jar that won't get in the
    way.

From the project's root directory...

    Repeat these steps after each sync...

    1) make (to produce generated .java source)
    2) development/tools/idegen/idegen.sh
    3) Import the project root directory into your Eclipse workspace. If you
       already have the project open, simply refresh it (F5).

Excluding source roots and jars

IDEGen keeps an exclusion list in the "excluded-paths" file. This file
has one regular expression per line that matches paths (relative to the
project root) that should be excluded from the IDE configuration. We
use Java's regular expression parser (see java.util.regex.Parser).

You can create your own additional exclusion list by creating an
"excluded-paths" file in the project's root directory. For example, you
might exclude all apps except the Browser in your IDE configuration with
this regular expression: "^packages/apps/(?!Browser)".

Controlling source root ordering (Eclipse)

You may want some source roots to come before others in Eclipse. Simply
create a file named "path-precedence" in your project's root directory.
Each line in the file is a regular expression that matches a source root
path (relative to the project's root directory). If a given source root's
path matches a regular expression that comes earlier in the file, that
source root will come earlier in the generated configuration. If a source
root doesn't match any of the expressions in the file, it will come last,
so you effectively have an implicit ".*" rule at the end of the file.

For example, if you want your applications's source root to come first,
you might add an expression like "^packages/apps/MyApp/src$" to the top
of the "path-precedence" file.  To make source roots under ./out come last,
add "^(?!out/)" (which matches all paths that don't start with "out/").

Guess you like

Origin blog.csdn.net/Vincent20111024/article/details/51307066