This content originally appeared on Level Up Coding - Medium and was authored by Rex Tech
When using Android Studio, I often get strange exceptions thrown by the IDE. Even both Android Studio and Intellij IDEA are open source projects, it’s hard for us to predict what happened at runtime.
So is it possible for us to debug Android Studio? The answer is yes! It’s quite simple to debug Android Studio at runtime. Follow the steps below and you’ll become an IDE expert.
Preparation
To debug Android Studio, you need the tools below:
- Android Studio, of course
- Intellij IDEA(both Ultimate Edition and Community Edition are OK). We’ll import Android Studio’s source code into it and add breakpoints by it.
Get Android Studio’s version
First, we need to make sure which version of Android Studio we are using.
We can find the version by selecting Android Studio — About Android Studio:
In the window, we can see that we are using Android Studio Arctic Fox | 2020.3.1.
Get Android Studio’s source code
We can get Android Studio’s source code by following Google’s guide: Checkout and build the source code.
Note: we need to change the branch name from studio-master-dev to the tag which corresponds to the version we are using.
Releases since Android Studio 2.4 are tagged in git. We can find all the tags here: https://android.googlesource.com/platform/manifest/+refs. Android Studio’s tags have the studio prefix.
So we can check out Android Studio Arctic Fox | 2020.3.1 by:
repo init -u https://android.googlesource.com/platform/manifest -b studio-2020.3.1
repo sync
Import code into Intellij IDEA
Follow this guide to import code into Intellij IDEA: Building Android Studio.
Edit custom VM options in Android Studio
Open Android Studio and select Help — Edit Custom VM Options…:
In the Editor, add the codes below:
-Xdebug
-Xrunjdwp:transport=dt_socket,address=8888,server=y,suspend=y
We’re using JDWP here. If you are not familiar with JDWP, you can take a look at Oracle’s documentation: Java Debug Wire Protocol. In short, we can debug any Java application (no matter it’s running on a local PC or a remote server) using JDWP.
Add Debug Configuration in Intellij IDEA
Launch Intellij IDEA and open the Run/Debug Configurations window:
Click the + button in the top left corner and select Remote JVM Debug:
Input the configuration’s name. As we set the address argument to 8888 in the custom VM options, input 8888 in the Port field and click OK.
Add breakpoints
If you’re familiar with Android Studio’s source code, just add breakpoints at the place that you’re interested in. If not, you can use the UI Inspector to find where to add breakpoints.
Let’s take the Gradle Sync Action as an example.
Select Tools — Internal Actions — UI — UI Inspector:
If you can not find Internal Actions, select Help — Edit Custom Properties:
Add the code below in idea.properties and restart Android Studio:
idea.is.internal=true
After click UI Inspector, you can find the implementation class of any UI element. If we want to know the Gradle Sync process, press Control + Alt and left-click the Gradle Sync button:
As you can see, the action of the Sync button is implemented by Action com.android.tools.idea.gradle.actions.SyncProjectAction. So add breakpoints in this class.
Start debugging
After add breakpoints, restart Android Studio. Android Studio will wait for the debugger. So select the Remote JVM Debug configuration we added before in Intellij IDEA, and click the Debug button.
Now, we can debug Android Studio! Look at any source code of Android Studio that you’re interested in and you’ll become an IDE expert soon!
A Simple Way To Debug Android Studio was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Rex Tech
Rex Tech | Sciencx (2021-08-22T22:20:54+00:00) A Simple Way To Debug Android Studio. Retrieved from https://www.scien.cx/2021/08/22/a-simple-way-to-debug-android-studio/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.