microsoft / appcenter-sdk-android

Development repository for the App Center SDK for Android
Other
276 stars 134 forks source link

NPE if `AppCenter.start` is called on `Application.attachBaseContext` #1638

Closed yujincheng08 closed 2 years ago

yujincheng08 commented 2 years ago

Description

NPE if AppCenter.start is called on Application.attachBaseContext. This is because getApplicationContext is still not avaliable then.

Repro Steps

Please list the steps used to reproduce your issue.

  1. Create an android App
  2. Create your own Application that extends Application
  3. Override attachBaseContext and call AppCenter.start on it
  4. NPE

Details

  1. Which SDK version are you using?
    • 4.4.4
  2. Which OS version did you experience the issue on?
    • Android N+
  3. What device version did you see this error on? Were you using an emulator or a physical device?
    • All kinds of devices
  4. What third party libraries are you using?
    • None
  5. Please enable verbose logging for your app using AppCenter.setLogLevel(Log.VERBOSE) before your call to AppCenter.start(...) and include the logs here:
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.isDeviceProtectedStorage()' on a null object reference
        at com.microsoft.appcenter.ApplicationContextUtils.isDeviceProtectedStorage(ApplicationContextUtils.java:49)
        at com.microsoft.appcenter.AppCenter.configureInstance(AppCenter.java:726)
        at com.microsoft.appcenter.AppCenter.configureAndStartServices(AppCenter.java:1087)
        at com.microsoft.appcenter.AppCenter.configureAndStartServices(AppCenter.java:1077)
        at com.microsoft.appcenter.AppCenter.start(AppCenter.java:388)
        at org.lsposed.manager.util.Telemetry.start(Telemetry.java:55)
        at org.lsposed.manager.App.attachBaseContext(App.java:151)
        at android.app.Application.attach(Application.java:340)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1193)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1393)
        ... 14 more
vvb2060 commented 2 years ago

We have passed in the application object, why are you still calling getApplicationContext()? Application is a subclass of Context. getApplicationContext() may return null.

https://github.com/microsoft/appcenter-sdk-android/blob/90baebbd0cdbcadc4dda3af729cf86de249c0e10/sdk/appcenter/src/main/java/com/microsoft/appcenter/ApplicationContextUtils.java#L26-L39

MatkovIvan commented 2 years ago

Override attachBaseContext and call AppCenter.start on it

It was out of our test cases, thanks for the report. Should be fixed in #1639

yujincheng08 commented 2 years ago

Fixed in #1639