Android应用程序的首次启动非常缓慢,并且systrace显示30秒的bindApplication [英] Android app first start is very slow and systrace shows 30 seconds of bindApplication

查看:93
本文介绍了Android应用程序的首次启动非常缓慢,并且systrace显示30秒的bindApplication的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个Android应用,并试图缩短启动时间.为此,我使用了

如您所见,有一个30秒的浅紫色标签,标题为 bindApplication .

此后,我关闭了该应用程序(从最近的活动中撤消了)并重新打开它.这次的 bindApplication 标签只有4秒长:

  • 有人知道第一次跑步这么长时间是否正常吗?
  • 我可以做些什么来改善它?

我的猜测是 bindApplication onCreate App方法中的繁重工作有关,但我不知道这怎么可能发生.以防万一:在我的 onCreate 中,我初始化了以下库:Parse,Crashlytics,T​​imber,ParseFacebookUtils和Google Analytics(分析).

这是App子类代码:

 公共类MyApp扩展了Application {私人跟踪器跟踪器;@Override public void onCreate(){super.onCreate();如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.JELLY_BEAN_MR2){Trace.beginSection("MyApp");}Fabric.with(this,new Crashlytics());//解析设置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));如果(BuildConfig.DEBUG){//添加日志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);如果(BuildConfig.DEBUG){StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().建造());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().建造());}如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.JELLY_BEAN_MR2){Trace.endSection();}}/***获取此{@link应用程序}的默认{@link Tracker}.* @返回跟踪器*/同步的公共跟踪器getDefaultTracker(){if(tracker == null){GoogleAnalytics Analytics = GoogleAnalytics.getInstance(this);//要启用调试日志记录,请使用:adb shell setprop log.tag.GAv4 DEBUGtracker = analytics.newTracker(R.xml.global_tracker);}返回跟踪器;}} 

解决方案

这是即时运行的问题.我曾经遇到过这种问题,我通过禁用即时运行来解决.这是同一个问题,您可以在问题的评论中找到答案.

首次启动时间长(ClassLoader引用的未知路径)

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:

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:

  • 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;
  }
}

解决方案

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)

这篇关于Android应用程序的首次启动非常缓慢,并且systrace显示30秒的bindApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆