background

Recently, I encountered a Dialog problem, which can only be reproduced on 6.0 mobile phones, but our project is targetsdk=28, click Dialog reference, only see the class file of 28, now I am getting lazy, I used to write a demo for this problem, to reproduce the problem. But now I want to be able to directly locate the cause of the current project quickly, can be based on source debug must be better, in order to achieve this lazy goal, I found a lot of ways, the following one is introduced

Method one switches to Compilesdk

Studio will use compilesdkversion configured in Gradle by default. If you set compilesdkversi to 23, you can go to the class file by checking define shortcuts. There are bound to be a lot of apis that don’t compile

Method two: Look for studio plug-ins

Since the default mode provided by Studio doesn’t work, why don’t we find a plugin to implement it? Through various searches, I found a great plugin, AndroidSourceViewer; Very powerful:

  • Support to view Android/Java any version of the source
  • Support comparison of Android/Java any two versions of the source differences
  • You can view Android official website documents and locate methods
  • Support Native method source view

See screenshots for details:

When I started debugging with great joy, I found that Studio could not recognize the code, because the author thought to add the version number in front of the class name… For example. AndroidSourceViewer/cache/android/app / 8.0.0 _r4 – Dialog. Java so I angry about an issue that the road is fail again

Method three: Modify the Studio configuration and link directly to the corresponding source code

My idea, I found the studio jump source configuration file, will originally jump to 28 JAR package configuration, modify 23, so not perfect, so I opened the search method, online said, modify JDK-table.xml, can achieve my purpose, however. Pro test can not; No matter how I changed it, it didn’t work;

Then I thought of the cache of idea, sure enough, I found this 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>
Copy the code

PS: This file may not exist, you can manually add to their cache

By modifying this file, as long as the source path exists, it is easy to debug other versions of the source code; However, changing the source path is risky, so proceed with caution and clear the cache when not in use.

conclusion

Now many articles on the domestic Internet are copied, I do not know why; Jkk-table.xml, as I found, is not working at all, maybe it is just for the new project, maybe it is caused by the studio version upgrade, in short, the original author of this article is also very hard, but others copy the past, and do not maintain, it is really wrong.

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