1# Examples of using simpleperf to profile Android applications 2 3## Table of Contents 4 5- [Introduction](#introduction) 6- [Profiling Java application](#profiling-java-application) 7- [Profiling Java/C++ application](#profiling-javac-application) 8- [Profiling Kotlin application](#profiling-kotlin-application) 9 10## Introduction 11 12Simpleperf is a native profiler used on Android platform. It can be used to profile Android 13applications. Its documentation is [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/README.md). 14Instructions of preparing your Android application for profiling are [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/README.md#Android-application-profiling). 15This directory is to show examples of using simpleperf to profile Android applications. The 16meaning of each directory is as below: 17 18 ../scripts/ -- contain simpleperf binaries and scripts. 19 SimpleperfExamplePureJava/ -- contains an Android Studio project using only Java code. 20 SimpleperfExampleWithNative/ -- contains an Android Studio project using both Java and C++ code. 21 SimpleperfExampleOfKotlin/ -- contains an Android Studio project using Kotlin code. 22 23It can be downloaded as below: 24 25```sh 26$ git clone https://android.googlesource.com/platform/system/extras 27$ cd extras/simpleperf/demo 28``` 29 30The testing environment: 31 32``` 33Android Studio 3.0 34test device: Android O (Google Pixel 2) 35test device: Android N (Google Nexus 6P) 36Please make sure your device having Android version >= N. 37``` 38 39## Profile a Java application 40 41Android Studio project: SimpleExamplePureJava 42 43steps: 441. Build and install the application: 45 46```sh 47# Open SimpleperfExamplesPureJava project with Android Studio, 48# and build this project successfully, otherwise the `./gradlew` command below will fail. 49$ cd SimpleperfExamplePureJava 50 51# On windows, use "gradlew" instead. 52$ ./gradlew clean assemble 53$ adb install -r app/build/outputs/apk/app-profiling.apk 54``` 55 562. Record profiling data: 57 58```sh 59$ cd ../../scripts/ 60# app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/. 61$ python app_profiler.py -p com.example.simpleperf.simpleperfexamplepurejava 62``` 63 643. Show profiling data: 65 66```sh 67# report_html.py generates profiling result in report.html. 68$ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly 69``` 70 71## Profile a Java/C++ application 72 73Android Studio project: SimpleExampleWithNative 74 75steps: 761. Build and install the application: 77 78```sh 79# Open SimpleperfExamplesWithNative project with Android Studio, 80# and build this project sucessfully, otherwise the `./gradlew` command below will fail. 81$ cd SimpleperfExampleWithNative 82 83# On windows, use "gradlew" instead. 84$ ./gradlew clean assemble 85$ adb install -r app/build/outputs/apk/profiling/app-profiling.apk 86``` 87 882. Record profiling data: 89 90```sh 91$ cd ../../scripts/ 92# app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/. 93$ python app_profiler.py -p com.example.simpleperf.simpleperfexamplewithnative 94``` 95 963. Show profiling data: 97 98```sh 99# report_html.py generates profiling result in report.html. 100$ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly 101``` 102 103## Profile a Kotlin application 104 105Android Studio project: SimpleExampleOfKotlin 106 107steps: 1081. Build and install the application: 109 110```sh 111# Open SimpleperfExamplesOfKotlin project with Android Studio, 112# and build this project sucessfully, otherwise the `./gradlew` command below will fail. 113$ cd SimpleperfExampleOfKotlin 114 115# On windows, use "gradlew" instead. 116$ ./gradlew clean assemble 117$ adb install -r app/build/outputs/apk/profiling/app-profiling.apk 118``` 119 1202. Record profiling data: 121 122```sh 123$ cd ../../scripts/ 124# app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/. 125$ python app_profiler.py -p com.example.simpleperf.simpleperfexampleofkotlin 126``` 127 1283. Show profiling data: 129 130```sh 131# report_html.py generates profiling result in report.html. 132$ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly 133``` 134