Using Android Studio to Profile the Unity App on the Android Platform

0x00 Preface

People often complain that the Android platform does not have a unified and easy-to-use profiler tool. Not as convenient as the iOS instrument tool.

ref:https://help.apple.com/instruments/mac/current/#/
For example, the Unity Blog taught you how to use instrument for profiling a few years ago.

Profiling with Instruments

In fact, the current Android Studio already provides a good profiler tool for the Android platform.

Profile your app performance

This post mainly introduces the CPU Profiler to check the performance of Unity native functions. Just like instrument on the iOS platform.

Graphics related performance profiling, you can use: google/gapid

0x01 export settings

In the following, I will use the Android Studio version: 3.5 preview, Unity version: 2018.3.0b11. The test project is SurvivalShooter (Asset Store).

First, according to the Android Studio documentation:

Inspect CPU activity with CPU Profiler

Sampled (Native): Captures sampled traces of your app’s native threads. To use this configuration, you must deploy your app to a device running Android 8.0 (API level 26) or higher.

The device system version required is: Android 8.0 (API level 26)

In order to detect the overhead of the script code, it is recommended to use IL2CPP for the script backend.

We can write a function for testing.

After that, we exported the project as a Gradle project so that it can be opened in Android Studio.

0x02 Symbols

Use Android Studio to open the exported project. And under the src/main/jniLibs/armeabi-v7a/ directory, you can find related .so files. We mainly focus on libunity.so and libil2cpp.so. The former is the engine part and the latter is the developer’s script part. But the symbol information is incomplete. So all we have to do is replace these .so files with more complete symbol information.

First, replace the libunity.so file, which can be found in the Unity directory

Later, if we want to see the cost of the script call (il2cpp), we need to replace the libil2cpp.so in the Gradle project with the libil2cpp.so with more information.

Its path is at: Project/Temp/StagingArea/symbols/armeabi-v7a/libil2cpp.so.debug

It can be seen that the libil2cpp.so in the Gradle project is only 7.7mb, while the libil2cpp with symbol information is 88.3mb.

0x03 Profiling!

Click on the Profile button in the upper right corner to start profiling. Select the CPU column, and there are Java Method, Sample C/C++, etc. Select C/C++ and you will see a timeline. Click record to start recording call trace.

You can see the chart of the function calls of each thread.

It can also be organized into a stack calls according to time overhead. Here you can see the script function call using il2cpp.

We can look for our UpdateTest1 function.

Of course, this is similar to using Instrument on the iOS platform described in the Unity Blog introduced at the beginning of this post. It is mainly used to profile the performance of some native functions. You can use it as a supplement to the Unity Profiler on the Android platform.


Subscribe To Jiadong Chen's Blog

Avatar
Jiadong Chen
Senior Software Developer

HusbandㅣProgrammerㅣAuthorㅣBloggerㅣSpeakerㅣMicrosoft @MVPAward In Developer TechnologiesㅣPreviously @Unity3d ㅣOpinions = my own.

comments powered by Disqus

Related