Skip to content

UninitializedPropertyAccessException crash on Android when amplify_datastore is included but not used #6114

Open
@giulitu95

Description

@giulitu95

Description

We're observing a crash on Android devices only (iOS is unaffected) when the amplify_datastore package is included in the project, but no Amplify functionality is used at runtime.

In our case, we temporarily released our app without using any Amplify features, although the library was still present in pubspec.yaml.

This results in a runtime crash due to an uninitialized lateinit property, even though the plugin is never used or initialized in code.

Root cause

The issue originates from the DataStoreHubEventStreamHandler.kt file:

  • The token variable is declared as lateinit:

  • It is accessed in the onCancel() method:

override fun onCancel(p0: Any?) {
eventSink = null
Amplify.Hub.unsubscribe(token)
}

This method gets triggered by the Flutter engine even though the stream wasn't properly initialized — because no Amplify plugin was ever added or configured.

More specifically, the stream gets initialized only when the AmplifyDataStore plugin is added via Amplify.addPlugin(...), which internally sets up the static streamWrapper and assigns a DataStoreStreamController. When this step is skipped (as in our case), the token remains uninitialized, and accessing it during onCancel() causes the app to crash.

Expected behavior

Including the amplify_datastore package without using it should not crash the app.
The plugin should:

  • Either check whether token is initialized using ::token.isInitialized before accessing it.

  • Or register/deregister the EventChannel only when the plugin is fully configured.

Sentry stacktrace

java.lang.RuntimeException: Unable to destroy activity {com.faegroup.app/com.faegroup.app.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property token has not been initialized
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5654)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5687)
    at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:47)
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2374)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:233)
    at android.os.Looper.loop(Looper.java:344)
    at android.app.ActivityThread.main(ActivityThread.java:8249)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071)
kotlin.UninitializedPropertyAccessException: lateinit property token has not been initialized
    at com.amazonaws.amplify.amplify_datastore.DataStoreHubEventStreamHandler.onCancel(DataStoreHubEventStreamHandler.kt:226)
    at com.amazonaws.amplify.amplify_datastore.AmplifyDataStorePlugin.onDetachedFromEngine(AmplifyDataStorePlugin.kt:195)
    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.remove(FlutterEngineConnectionRegistry.java:272)
    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.remove(FlutterEngineConnectionRegistry.java:280)
    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.removeAll(FlutterEngineConnectionRegistry.java:288)
    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.destroy(FlutterEngineConnectionRegistry.java:123)
    at io.flutter.embedding.engine.FlutterEngine.destroy(FlutterEngine.java:473)
    at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDetach(FlutterActivityAndFragmentDelegate.java:772)
    at io.flutter.embedding.android.FlutterActivity.onDestroy(FlutterActivity.java:909)
    at android.app.Activity.performDestroy(Activity.java:8406)
    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1378)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5641)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5687)
    at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:47)
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2374)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:233)
    at android.os.Looper.loop(Looper.java:344)
    at android.app.ActivityThread.main(ActivityThread.java:8249)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:589)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1071)

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

  1. Add the amplify_datastore package to a Flutter project (via pubspec.yaml).
  2. Do not call Amplify.addPlugin() with AmplifyDataStore, and do not interact with Amplify in any way.
  3. Run the app on an Android device.

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.29.2

Amplify Flutter Version

2.6.1

Deployment Method

Amplify CLI (Gen 1)

Schema

Metadata

Metadata

Assignees

No one assigned

    Labels

    AndroidIssues specific to the Android PlatformbugSomething is not working; the issue has reproducible steps and has been reproduceddatastoreIssues related to the DataStore Categorypending-close-response-requiredThe issue will be closed if details necessary to reproduce the issue are not provided within 7 days.to-be-reproducedIssues that have not been reproduced yet, but have reproduction steps provided

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions