Android app first start is very slow and systrace shows 30 seconds of bindApplication
Asked Answered
C

1

6

I'm currently developing an Android app and trying to improve the start time. In order to do so, I'm using the Systrace tool.

The first time I run the app (right after installed), it takes ~40 seconds to start, and I get this trace:

First run

As you can see, there is a 30 seconds light purple tag with title bindApplication.

After this, I close the app (swiped away from recent activities) and reopen it. This time the bindApplication tag is just 4 seconds long:

Second run

  • Does anybody know if it's normal for the first run to take so long ?
  • What can I do to improve it ?

My guess here is that bindApplication is related somehow to heavy work in the onCreate App method, but I don't see how that could happen. Just in case it helps: in my onCreate I initialize the following libraries: Parse, Crashlytics, Timber, ParseFacebookUtils and Google Analytics.

EDIT:

Here is the App subclass code:

public class MyApp extends Application {

  private Tracker tracker;

  @Override public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
      Trace.beginSection("MyApp");
    }
    Fabric.with(this, new Crashlytics());

    // Parse setup
    Parse.enableLocalDatastore(this);
    ParseObject.registerSubclass( ... );

    Parse.Configuration.Builder parseConfigBuilder = new Parse.Configuration.Builder(this).applicationId(
        getString(R.string.parse_application_id))
        .server(getString(R.string.parse_server_url));

    if (BuildConfig.DEBUG) {
      // add logs
      Timber.plant(new DebugTree());
      Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
      parseConfigBuilder.addNetworkInterceptor(new ParseLogInterceptor());
    }

    Parse.initialize(parseConfigBuilder.build());

    ParseFacebookUtils.initialize(this);

    ParseInstallation.getCurrentInstallation().saveInBackground();

    AnalyticsManager.getInstance().init(this);
    AnalyticsManager.getInstance().debugMode(BuildConfig.DEBUG);

    if (BuildConfig.DEBUG) {
      StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
          .detectAll()
          .penaltyLog()
          .build());
      StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().
          detectAll()
          .penaltyLog()
          .build());
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
      Trace.endSection();
    }
  }

  /**
   * Gets the default {@link Tracker} for this {@link Application}.
   * @return tracker
   */
  synchronized public Tracker getDefaultTracker() {
    if (tracker == null) {
      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
      tracker = analytics.newTracker(R.xml.global_tracker);
    }
    return tracker;
  }
}
Chappie answered 16/7, 2016 at 5:0 Comment(4)
before onCreate of Launcher Activity of your app, the subclassed-Application class code is executed. Do you have any initialisation or any other long running code inside your Application class? May be that is also 1 area where you can look for performance...Ralleigh
@AADTechnical i just initialize the mentioned libraries, no long running code.Chappie
i guess you need to show the code where you are doing all these initialisations. Its hard to Guess without looking at code where there is performance issue...Ralleigh
@AADTechnical You are right, I just added it.Chappie
T
11

it is problem of instant run. i had once this kind of problem and i solve if by disable instant run. here is same question and you can find your answer in comment of question.

First launch take long time (ClassLoader referenced unknown path)

Topgallant answered 16/7, 2016 at 5:28 Comment(1)
Wow, I hate Instant run so much right now. Thanks !Chappie

© 2022 - 2024 — McMap. All rights reserved.