Mobile Development 13 min read

Comprehensive Guide to Android Source Code Exploration, Environment Setup, Debugging Techniques, and Permission Analysis

This article introduces Android source code directory structures, explains how to configure a local development environment with IDEGen and Android Studio, demonstrates advanced debugging methods such as method, conditional, and log breakpoints, and walks through a real‑world permission‑granting case study to illustrate source‑level analysis and modification strategies.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Comprehensive Guide to Android Source Code Exploration, Environment Setup, Debugging Techniques, and Permission Analysis

With the rapid growth of Android, new technologies such as pluginization, hot‑fix, automated testing, reverse‑engineering tools like Art Hook, VirtualApp, and custom ROMs require deep familiarity with the Android source code. This article explains how to set up a source‑code reading environment, offers debugging tips, and presents a case study on permission handling.

1. Android source code directory overview – The source tree contains many directories; key ones include frameworks (core framework), bionic (libc), art (runtime), kernel (Linux kernel), system/core (adb, debugger, etc.), and packages/apps (system apps such as Phone, Music).

2. Configuring the local source‑code reading environment – After downloading the AOSP source, use the built‑in idegen tool to generate Android Studio project files:

Run source build/envsetup.sh in the root directory.

Execute lunch with the appropriate target (e.g., AOSP or LineageOS).

Run make idegen -j4 .

Execute sudo development/tools/idegen/idegen.sh (sudo is required for some steps).

After generation, edit android.iml to exclude non‑essential directories (keep only frameworks and a few others) and remove unnecessary orderEntry nodes, then set the correct SDK version in Project Structure.

3. Debugging the source code – Advanced debugging techniques include:

Method breakpoints : pause execution at a method entry to inspect parameters and return values, useful when line numbers do not match.

Conditional breakpoints : add a Boolean expression involving local variables; the breakpoint triggers only when the condition is true.

Log breakpoints : enable "Evaluate and log" to print variable values without stopping execution, greatly speeding up traceability.

These features are accessed via the Breakpoints panel in Android Studio.

4. Code analysis case study – dynamic permission granting – The article examines how Android 6.0+ handles runtime permissions such as READ_EXTERNAL_STORAGE . The request flows through Activity.requestPermissions or ActivityCompat.requestPermissions , which launches an implicit intent ( android.content.pm.action.REQUEST_PERMISSIONS ) handled by GrantPermissionsActivity in the PackageInstaller app.

Key analysis steps:

Create mAppPermissions by reading the app’s manifest permissions from PackageManagerService .

Group ungranted permissions into mRequestGrantPermissionGroups .

Iterate over the groups, presenting UI for user approval; after all groups are processed, call setResult and finish .

When the user approves, grantRuntimePermissions ultimately invokes PackageManagerService.grantRuntimePermission , updating the permission state and returning the result to the caller.

The article discusses possible modification points, such as moving the granting logic into ActivityManagerService.startActivity to bypass the UI, or directly granting permissions from the app process (which may lack required privileges).

5. Summary – The guide covered Android directory structure, environment setup with IDEGen, several powerful debugging techniques, and a concrete permission‑granting analysis. Readers are encouraged to deepen their knowledge of Java, C/C++, Linux kernel internals, and official documentation to master Android source code.

Debuggingmobile developmentAndroidpermissionandroid-studioSource Codeidegen
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.