Skip to content

Android project withCachedEngine will crash  #238

@flyisme

Description

@flyisme

Environment details

  1. google_navigation_flutter-0.4.0

Steps to reproduce

I Using Flutter as a module dependency in native projects, where the native project utilizes engine cache to improve initial Flutter launch speed

Code example

  • android code
EbFlutterGlobal.globalContext.startActivity(
            FlutterActivity
                .withCachedEngine(EbFlutterGlobal.CACHE_ENGINE_ID)
                .build(EbFlutterGlobal.globalContext)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        )
  • flutter code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_navigation_flutter/google_navigation_flutter.dart';

class NavigationSample extends StatefulWidget {
  const NavigationSample({super.key});

  @override
  State<NavigationSample> createState() => _NavigationSampleState();
}

class _NavigationSampleState extends State<NavigationSample> {
  GoogleNavigationViewController? _navigationViewController;
  bool _navigationSessionInitialized = false;

  @override
  void initState() {
    super.initState();
    _initializeNavigationSession();
  }

  Future<void> _initializeNavigationSession() async {
    print('_initializeNavigationSession:${GoogleMapsNavigator.simulator}');
    if (!await GoogleMapsNavigator.areTermsAccepted()) {
      await GoogleMapsNavigator.showTermsAndConditionsDialog(
        'Example title',
        'Example company',
      );
    }
    // Note: make sure user has also granted location permissions before starting navigation session.
    await GoogleMapsNavigator.initializeNavigationSession();
    setState(() {
      _navigationSessionInitialized = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Google Maps Navigation Sample1')),
      body: _navigationSessionInitialized
          ? GoogleMapsNavigationView(
        onViewCreated: _onViewCreated,
        initialNavigationUIEnabledPreference: NavigationUIEnabledPreference.disabled,
        // Other view initialization settings
      )
          : const Center(child: CircularProgressIndicator()),
    );
  }

  void _onViewCreated(GoogleNavigationViewController controller) {
    _navigationViewController = controller;
    controller.setMyLocationEnabled(true);
    // Additional setup can be added here.
  }

  @override
  void dispose() {
    if (_navigationSessionInitialized) {
      GoogleMapsNavigator.cleanup();
    }
    super.dispose();
  }
}

Stack trace

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.google_navigation_flutter.NavigationSessionApi.areTermsAccepted"., null, null)
                                                                                                    #0      NavigationSessionApi.areTermsAccepted (package:google_navigation_flutter/src/method_channel/messages.g.dart:5364:7)
                                                                                                    <asynchronous suspension>
                                                                                                    #1      _NavigationSampleState._initializeNavigationSession (package:flutter_bridger/NavigationSample.dart:27:10)
                                                                                                    <asynchronous suspension>


...
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:590)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
                                                                                                    Caused by: java.lang.reflect.InvocationTargetException
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886) 
                                                                                                    Caused by: com.google.maps.flutter.navigation.FlutterError: Cannot access navigation functionality before the navigation session has been initialized.
...

I think the reason is that when pre-loading the engine, it also pre-loads the Flutter view (which executes related channel methods). At this point, FlutterActivity hasn't been opened yet (on the native side, injection happens only when FlutterActivity is loaded), therefore it reports 'Unable to establish connection on channel'. How should I handle this? In my project, I want to use engine pre-loading to optimize the launch @speed.

Thanks!

Metadata

Metadata

Labels

priority: p1Important issue which blocks shipping the next release. Will be fixed prior to next release.status: investigatingThe issue is under investigation, which is determined to be non-trivial.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions