Ariel Vardi
Ariel Vardi

Reputation: 797

Best way to profile Proguard-optimized build in Android Studio?

We want to profile our code as close as possible to what it's like in our release builds, i.e. with the same Proguard optimizations. The obvious issue is that all class names/methods are obfuscated making profiling/debug difficult. What is the best practice to solve this?

Is there a way to upload a proguard mapping to Android Studio? Or is a better approach to have a different Proguard configuration that keeps all class/method names but still run all other optimizations?

Upvotes: 3

Views: 698

Answers (2)

mhansen
mhansen

Reputation: 1141

You can profile deobfuscate proguarded builds, but not with Android Studio, you have to use the simpleperf toolkit that is underneath the Android Studio CPU Profiler, and another UI: Firefox Profiler.

Prerequisites:

  • Check you're on python >= 3.8

    python3 -V
    
  • Install the Android NDK

  • Install the latest simpleperf:

    $ git clone https://android.googlesource.com/platform/system/extras ~/extras
    $ cd ~/extras/simpleperf/scripts
    
  • Profile using app_profiler.py, e.g. for Google Maps @4000Hz sampling stacks and grabbing stacks when the thread blocks (off-cpu analysis) for 10 seconds:

    $ ./app_profiler.py \
        --app com.google.android.apps.maps \
        --record_options "-g -e cpu-clock --trace-offcpu -f 4000 --duration 10" \
        --activity com.google.android.maps.MapsActivity
    
  • Convert to Firefox Profile format, deobfuscating with proguard map:

    $ ./gecko_profile_generator.py \
        --proguard-mapping-file proguard.map \
        | gzip > gecko_profile.json.gz
    
  • Drag and drop the gecko_profile.json.gz into https://profiler.firefox.com/

Upvotes: 1

Darth Beleg
Darth Beleg

Reputation: 2667

There is no way to supply mapping to the profiler as of Android Studio 4.0. A feature request exists so anyone interested can star it to bring it to attention of the Android tools team. This looks like a reasonable feature, other Java profilers can do it.

For now the best option is to add -dontobfuscate to the Proguard/R8 config of the build under profiling as described in the Android docs. There are some potential downsides though:

  1. Theoretical one: keeping long names may skew performance profile a bit because classes are slightly larger and this may affect class loading times. However this is unlikely to change the answers you are looking for during profiling: hot methods will still be hot.
  2. Anecdotal one: some tools in your optimization pipeline may not like the resulting configuration - this depends on the project and tools. I've seen crashes in Facebook's Redex because of this.

Upvotes: 1

Related Questions