📜 ⬆️ ⬇️

We profile Unity project with Android Studio

Good day to all! This article is about how to profile Unity games on Android with Android Studio. I will talk about how to set up Android Studio and get the maximum amount of data. The questions of analysis and conclusions based on the result obtained are beyond the scope of this article.

Requirements


For full profiling of the application, you need a phone with Android 8 or newer (API 27). By experience, profiling with Android of older versions carries more adventures than benefits. For this reason, I recommend to get a full line of Nexus devices, as they have old hardware and the latest Android updates.

Setting up the Unity project


To get the result, you have to set up a Unity project in a certain way.

Settings in Build Settings


image


Settings in Player Settings


image


Gradle project preparation


After you have installed all the settings, build the Unity project. You should get a folder with the Gradle project.
image

Unity by default collects the project at the rate that you plan to build the final APK from it. Therefore, all debug information has been removed from it, but fortunately it can be returned. To do this, you need to replace libil2cpp.so and libunity.so with debug information.

libil2cpp.so


This is the file that contains all the C ++ code that IL2CPP generated from your C # code. Unity generates all the necessary information for profiling, but cuts it out for the optimization of the APK size. To return it:

libunity.so


This is the file that contains the low-level part of the Unity Player. Since we are doing a release build, Unity has put a file without debugging information into your project. You need to replace libunity.so with a file with symbols.


Profiling


Now you can start profiling in Android Studio, just press the profiler launch button.

image

Android Studio launches the application and starts the profiling session.

image

By default, Android Studio displays graphics, but does not sample data. To start the process, you need to click on the CPU track so that the profiler switches to the CPU view. At the same time, a dropdown and a 'Record' button will appear on top of the window.

image

Select Sampled 'Native' (In Android Studio 3.3 - C / C ++ Native), and tap the 'Record' button.
Since the recording is made on the device's disk, it is better not to record more than 5-8 seconds from experience, many devices will fade on a smaller amount of data (see the list of tested devices at the end of the article).

To get the result, click 'Stop' and then a red square to interrupt the session. It is difficult to understand the idea of ​​the authors, but if you do not stop the recording completely, then the profiler does not always start analyzing the obtained data and your segment with this data will go far away.

image

After that, it remains only to wait a bit, in 30-50 seconds the profiler will give you the result. If everything is set up correctly, you will get capture with all the function names.

image

Known features



Unity specifics



image

Recommended devices


We used the following devices for experiments:

All devices have been installed, for Huawei Nexus 6P, we have assembled the core and AOSP . As a result, Google and Sony turned out to be the most hassle-free and easy-to-work. Sony has an excellent site for developers - Open Devices . Huawei no longer allows you to unlock devices in an easy way, Samsung causes constant difficulties with additional levels of protection. Moto G5P often led to a drop in the profiler data collection process (simpleperf).

Source: https://habr.com/ru/post/437602/