3

I'm working on an application with large codebase. The problem is that when I launch the app from cold start state, It takes a lot of time to show the first screen of the app.
Based on here I checked the TTID for the app and it was terrifying:

Displayed com.example.myapp/.ui.MainActivity: +6s501ms

So I put some logs to check where is the problem:

class MyApp : Application() {
    override fun onCreate() {
        Log.d("Performance", "MyApp::onCreate::start")
        super.onCreate()
        // App initializations
        Log.d("Performance", "MyApp::onCreate::end")
    }
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d("Performance", "MainActivity::onCreate::start")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // Ui initializations
        Log.d("Performance", "MainActivity::onCreate::end")
    }
}

After this I realized something strange:

---------------------------- PROCESS STARTED (4572) for package com.example.myapp ----------------------------
13:58:06.819 Performance    D  MyApp::onCreate::start
13:58:07.066 Performance    D  MyApp::onCreate::end
13:58:07.132 Performance    D  MainActivity::onCreate::start
13:58:07.566 Performance    D  MainActivity::onCreate::end

I noticed a strange thing. That there is a lot of delay (about 5 seconds) between PROCESS_START and App:onCreate (there isn't any timestamp for PROCESS_START but I calculated manually). Also app creation and activity creation does not take more than 1 second.
So how can I solve this problem? What can be the cause of this issue?

Saman Sattari
  • 3,322
  • 6
  • 30
  • 46
  • Is this a debug build? Are you in debug mode instead of run mode? I think either of these can introduce slowness. – Tenfour04 Jan 23 '23 at 13:49
  • 1
    You may have 1+ libraries registering `ContentObservers` that do work when the process is started. – CommonsWare Jan 23 '23 at 13:58
  • @Tenfour04 Yes this logs belong to the debug build but its not in debug mode. Also I've to mention that I started to check the performance due to reports of usages with release build – Saman Sattari Jan 23 '23 at 14:00
  • If you have a customized Application class that do heavy processing, that could be a reason – Zain Jan 26 '23 at 11:40
  • You added a bounty, but have you looked into the Content Observers yet, as mentioned in the comment above? That's a likely culprit. – Tenfour04 Jan 26 '23 at 16:58
  • @Tenfour04 I looked up and found 5 libs that were using ContentObserver. I removed 3 of them for testing purpose but couldn't remove 2 of them since they are being used everywhere (room and compose). but the results are still the same. – Saman Sattari Jan 27 '23 at 12:20
  • @CommonsWare I tried to remove them but still same issue – Saman Sattari Jan 27 '23 at 12:21
  • @Zain I've commented most of them and used lazyLoading to test if the application is the problem but its not. – Saman Sattari Jan 27 '23 at 12:23
  • Is it possible that you have too many dependencies which all need to be loaded before `onCreate` can be called? This could happen for example if you have static initializer or call functions on `MyApp` field initializers, and maybe if you have third party library types declared as `MyApp` fields, because the classes might be loaded when `MyApp` is loaded. – TWiStErRob Jan 29 '23 at 10:45
  • Also, rather than `Log.d` profile your app as shown in the page you linked: https://developer.android.com/topic/performance/vitals/launch-time#bottlenecks – TWiStErRob Jan 29 '23 at 10:47

0 Answers0