Android Studio How to View Source Code and Debug Method Steps

  • 2021-12-04 11:10:32
  • OfStack

Background

I recently encountered an Dialog problem, It can only be reproduced on 6.0 mobile phones, But our project is already targetsdk=28. Click on the reference of Dialog, can only see 28 class file, now I also more and more lazy, before this kind of problem, I will write an demo, to reproduce the problem, but now hope to be directly in the current project quickly locate the cause, can be based on the source code debug definitely better, in order to achieve this lazy goal, I found a lot of ways, the following 11 introduced

Method 1 Switch compilesdk

By default, studio uses compilesdkversion configured in gradle. As long as you set compilesdkversi to 23, you can naturally enter the corresponding class file by viewing the shortcut keys of define, but my project is already 28, and it drops back to 6.0. There must be many API that cannot be compiled. This way is different

Method 2 Find studio plug-in

Since the default method provided by studio doesn't work, why don't we find a plug-in to implement it? Through various search methods, I found a good plug-in, AndroidSourceViewer;; Function 10 points powerful:

Support to view any version of Android/Java source code Support comparison of source code differences between any two versions of Android/Java Support Android official website document viewing and method positioning Support Native method source code view

See the screenshot for details:

Just when I opened debug with joy, I found that studio could not recognize the code, because the author's idea was to add the version number before the class name. . . For example. AndroidSourceViewer/cache/android/app/8.0. 0_r4-Dialog. java So I put forward an issue angrily and this road failed again

Method 3 Modify the studio configuration and link directly to the corresponding source code

When I thought about it, I found the configuration file of studio jump source code, and modified the configuration of jar package that originally jumped to 28 to 23, which is not perfect, so I started the search method again. It is said on the Internet that modifying jdk-table. xml can achieve my purpose, however. . Pro-test is not allowed; No matter how I modified it, there was no response;
So I thought of the cache of idea again. Indeed, everything comes to him who waits. I found such a file:

.idea/libraries/Gradle__android_android_28.xml


<component name="libraryTable">
 <library name="Gradle: android-android-28">
  <CLASSES>
   <root url="jar://$USER_HOME$/Library/Android/sdk/platforms/android-27/android.jar!/" />
  </CLASSES>
  <JAVADOC />
  <SOURCES>
   <root url="file://$USER_HOME$/Library/Android/sdk/sources/android-28" />
  </SOURCES>
 </library>
</component>

PS: This file may not be available, so you can add it to your cache manually

By modifying this file, as long as the source code path exists, it can be very convenient for other versions of the source code and debugging; However, modifying the source code path is risky, so we should be careful and remember to empty the cache when not in use. .

Summarize

Now many articles on the domestic Internet are copied, and I don't know why; jdk-table. xml, which I searched for, can't be used at all. Maybe it is only effective for new projects, and it may be caused by the upgrade of studio version. In a word, the original author of this article is also very hard, but others copy the past without maintaining it, which is really misleading. .

Another point is that you can see the source code is still very cool ~


Related articles: