Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android project withCachedEngine will crash #238

Open
flyisme opened this issue Dec 17, 2024 · 1 comment
Open

Android project withCachedEngine will crash #238

flyisme opened this issue Dec 17, 2024 · 1 comment
Assignees
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. status: investigating The issue is under investigation, which is determined to be non-trivial. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@flyisme
Copy link

flyisme commented Dec 17, 2024

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!

@flyisme flyisme added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Dec 17, 2024
@jokerttu jokerttu added status: investigating The issue is under investigation, which is determined to be non-trivial. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. and removed triage me I really want to be triaged. labels Dec 27, 2024
@jokerttu
Copy link
Contributor

@flyisme than you for reporting this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. status: investigating The issue is under investigation, which is determined to be non-trivial. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants