How to View and Debug Android Source Code Using Android Studio

Today’s Tech News

Recently, it was reported that the local life service company formed by the merger of Ele.me and Koubei has raised $4 billion in funding, bringing its valuation to $30 billion. According to insiders, over $3 billion of the new funding comes from Alibaba and SoftBank’s Vision Fund, with Primavera Capital Group and existing investors from Koubei and Ant Financial also participating in this round of financing, which is expected to be completed by the end of November.

Author’s Profile

Tomorrow is Saturday, so I wish everyone a happy weekend in advance!

This article is adapted from cfy137000‘s blog, which shares how to view the source code using Android Studio, hoping to be of help to everyone.

Blog address of cfy137000:

https://blog.csdn.net/cfy137000

Basic Environment

Whether at work or while learning Android, we often need to use the Android source code. Without proper development tools, we cannot debug or write code, and Android Studio can compile and debug the source code.

Operating System

First, we need a Linux operating system; it cannot be compiled directly under Windows. Here, I am using Deepin, and the operations for Ubuntu should be similar. If you only have a Windows computer, I strongly recommend using Docker to complete this; do not use a virtual machine.

Java

Java version 1.8 is sufficient. This article compiles Android-P, and depending on the Android version, the required Java environment may vary slightly.

How to View and Debug Android Source Code Using Android Studio

Android Source Code

Downloading the source code is not the focus of this article and will not be elaborated on. It is recommended to use the Tsinghua source for downloading, and the website provides detailed instructions on how to download.

Tsinghua University Open Source Software Mirror Site

https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/

Compiling the Source Code

Before importing the project into Android Studio, it is best to compile the entire base code first. This ensures that there are no issues with our code and development environment, and it also generates the R files through the compilation.

Initialize the Compilation Environment

Command:

source build/envsetup.sh

It is important to note that you must ensure your terminal is bash or zsh, as the Android compilation scripts only guarantee compatibility with these two. If you are using fish or something else, you need to switch manually. Furthermore, only zsh is supported for Android P; on Android O, it still supports bash. I encountered some issues while compiling Android O because I used zsh.

Terminal Check Source Code for Android P

function validate_current_shell() {
    local current_sh="
$(ps -o command -p $$)"
    case "$current_sh" in
        *bash*)
            function check_type() { type -t "$1"; }
            ;;
        *zsh*)
            function check_type() { type "$1"; }
            enable_zsh_completion ;;
        *)
            echo -e "WARNING: Only bash and zsh are supported.\nUse of other shell would lead to erroneous results."
            ;;
    esac
}

Terminal Check Source Code for Android O

if [ "x$SHELL" != "x/bin/bash" ]; then
    case `ps -o command -p $$` in
        *bash*)
            ;; 
        *)
            echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
            ;;
    esac
fi

Select Compilation Target

Before compiling, we need to select the compilation target. The compilation target refers to the device on which the generated image will run, such as whether it will run on a Pixel phone or a virtual machine. Command:

lunch

The lunch command will list all currently supported compilation types, and then you can enter the corresponding number to select.

How to View and Debug Android Source Code Using Android Studio

Here we select 28 aosp_x86_64-eng to compile the x86 version; you can choose based on your actual situation.

How to View and Debug Android Source Code Using Android Studio

We can also use the choosecombo command for a one-time selection, for example:

choosecombo 2 aosp_x86_64 3

How to View and Debug Android Source Code Using Android Studio

The choosecombo command takes three parameters:

  • The first parameter is Build type: which sets the TARGET_BUILD_TYPE environment variable to release or debug.

  • The second parameter is the product to be compiled, namely aosp_x86_64.

  • The third parameter is the variant to be compiled, which can be user, userdebug, or eng version.

  • user: indicates that the compiled system image is for formal release to the market, with restricted permissions, such as no root access and no debugging.

  • userdebug: opens root and debug permissions based on the user version.

  • eng: represents engineer, which is the so-called developer version, with maximum permissions (root, etc.), and also comes with many debug tools.

Compile

The compilation command is quite simple.

make -j18

The make command compiles the code, and this command uses the -j parameter to set the number of threads participating in the compilation to speed up the process. For example, here we set 18 threads to compile simultaneously. Typically, this number is the number of CPU cores * 2 + 2; it is not better the larger it is. After about an hour, depending on your computer’s performance, the compilation should be completed.

Verification

After the compilation is complete, you can execute the emulator command to start the compiled virtual machine. Additionally, if the terminal is closed, you need to execute the source build/envsetup.sh command and the lunch command again.

How to View and Debug Android Source Code Using Android Studio

You can see that this is actually the most common Android virtual machine.

Importing Source Code into Android Studio

In fact, there are already documents in the Android source code that tell us how to use the IDE to edit the Android source code, located at source path/development/tools/idegen/README.

How to View and Debug Android Source Code Using Android Studio

Since we are now using Android Studio, we only need to focus on the IntelliJ part; there is no need to look at Eclipse.

Initial Configuration of Android Studio

The document states that because Android is too large, we need to give the IDE more memory. Add the following in Help > Edit Custom VM:

-Xms1g
-Xmx5g

How to View and Debug Android Source Code Using Android Studio

These two parameters mean the initial heap memory is 1G, and the maximum heap memory is 5G. In fact, it is okay not to set them, but you often encounter out-of-memory error messages when viewing code, so having more memory is quite necessary.

Then configure the class size for Android Studio in Help -> Edit custom properties:

idea.max.intellisense.filesize=100000

How to View and Debug Android Source Code Using Android Studio

This parameter defines the default class size for AS; the default value is 2500, which causes too large Java files to be unrecognized. After increasing this number, larger Java files can be imported, but a good computer is still needed.

After configuration, restart the IDE.

How to View and Debug Android Source Code Using Android Studio

Importing Source Code

First, we still need to execute the source build/envsetup.sh command and the lunch command again. Of course, if the terminal is not closed, this step can be skipped, and then execute:

mmm development/tools/idegen

This compiles and generates idegen.jar. It is important to note that although Google’s script says it supports zsh, if you use zsh, you will encounter the error Couldn’t locate the directory development/tools/idegen. Just use bash again to resolve the issue.

How to View and Debug Android Source Code Using Android Studio

After generating idegen.jar, you can use the command to scan and generate the ipr file:

sudo ./development/tools/idegen/idegen.sh

How to View and Debug Android Source Code Using Android Studio

This ipr file is the entire project, and Android Studio can directly recognize and open it just like opening a normal Android project.

How to View and Debug Android Source Code Using Android Studio

After this, Android Studio will start opening the project, which may take some time. Sometimes, AS may display the following message:

How to View and Debug Android Source Code Using Android Studio

The general meaning is that due to the project’s size, AS cannot monitor all changes in the project well. You can solve this as follows:

1. Add the following code at the end of the /etc/sysctl.conf file:

fs.inotify.max_user_watches = 524288

2. Then execute the following command in the terminal:

sudo sysctl -p --system

Finally, restart AS.

Other Configurations for Android Studio

Set Project SDK to Android API 28 and Java version to Java 8.

How to View and Debug Android Source Code Using Android Studio

Then, in the SDK options, only keep Java 1.8 and Android API 28, deleting the rest.

How to View and Debug Android Source Code Using Android Studio

How to View and Debug Android Source Code Using Android Studio

How to View and Debug Android Source Code Using Android Studio

Next, for Modules, delete all Jar files, as we basically do not need them; just viewing the source code is sufficient. If you really need one, keep it accordingly.

How to View and Debug Android Source Code Using Android Studio

If Android Studio keeps scanning files to index, you can go to module settings –> Modules –> find the gen folder –> right-click and select Resources.

How to View and Debug Android Source Code Using Android Studio

Now we can happily read the source code!

Removing Unnecessary Projects

We can exclude projects or paths that we are not concerned about in Android Studio, making it faster to open the source code. For example, we can remove the hardware path.

How to View and Debug Android Source Code Using Android Studio

Debug

During development, debugging is essential. We can debug our code in the source code environment, taking Browser2 as an example:

1. Add Breakpoints

Add a few breakpoints:

How to View and Debug Android Source Code Using Android Studio

2. Click the Attach Debugger to Android Process Button on the Toolbar

How to View and Debug Android Source Code Using Android Studio

3. Select the Program to Debug -> OK

Make sure to check Show all processes.

How to View and Debug Android Source Code Using Android Studio

4. Run the Project Normally to Debug

How to View and Debug Android Source Code Using Android Studio

As you can see, we can debug our project normally.

Welcome to long press the image below -> recognize the QR code in the image

or scan the code to follow my public account

How to View and Debug Android Source Code Using Android Studio

How to View and Debug Android Source Code Using Android Studio

Leave a Comment

Your email address will not be published. Required fields are marked *