plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[Bug]: Does not launch dart function in release build

Open patzjo opened this issue 1 year ago • 8 comments

Platform

android 10

Plugin

android_alarm_manager_plus

Version

2.0.6+1

Flutter SDK

3.3.0

Steps to reproduce

build and launch in release mode using flutter build apk --release

Application starting to print to logcat when building with debug and profile arguments but does not work when building release version.

Building and running with: flutter build apk --debug && flutter install // Working flutter build apk --profile && flutter install // Working flutter build apk --release && flutter install // Gives dart error: Dart_LookupLibrary 'android_alarm_manager_plus.dart not found

Seems that release mode somehow removes essential code from apk.

manifest have been updated as documents says.

Code Sample

import 'dart:isolate';

import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:flutter/material.dart';

void printHello() {
  final DateTime now = DateTime.now();
  final int isolateId = Isolate.current.hashCode;
  print("[$now] Hello, world! isolate=$isolateId function='$printHello'");
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AndroidAlarmManager.initialize();

  const int helloAlarmID = 0;
  await AndroidAlarmManager.periodic(
      const Duration(minutes: 1), helloAlarmID, printHello);
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        brightness: Brightness.dark,
      ),
      home: const MyHomePage(title: 'Test'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: const SizedBox(),
    );
  }
}

Logs

E/flutter: [ERROR:flutter/shell/common/shell.cc(93)] Dart Error: Dart_LookupLibrary: library 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart' not found.
E/flutter: [ERROR:flutter/runtime/dart_isolate.cc(668)] Could not resolve main entrypoint function.
E/flutter: [ERROR:flutter/runtime/dart_isolate.cc(167)] Could not run the run main Dart entrypoint.
E/flutter: [ERROR:flutter/runtime/runtime_controller.cc(385)] Could not create root isolate.
E/flutter: [ERROR:flutter/shell/common/shell.cc(604)] Could not launch engine with configuration.

Flutter Doctor

[√] Flutter (Channel stable, 3.3.0, on Microsoft Windows [Version 10.0.22000.918], locale en-FI)
    • Flutter version 3.3.0 on channel stable at d:\sdk\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ffccd96b62 (9 days ago), 2022-08-29 17:28:57 -0700
    • Engine revision 5e9e0e0aa8
    • Dart version 2.18.0
    • DevTools version 2.15.0

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at C:\Users\user\AppData\Local\Android\sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.3.3)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.3.32825.248
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2021.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)

[√] VS Code (version 1.71.0)
    • VS Code at C:\Users\user\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.48.0

[√] Connected device (4 available)
    • moto g 7 (mobile) • ZY225F3C4M • android-arm64  • Android 10 (API 29)
    • Windows (desktop) • windows    • windows-x64    • Microsoft Windows [Version 10.0.22000.918]
    • Chrome (web)      • chrome     • web-javascript • Google Chrome 105.0.5195.54
    • Edge (web)        • edge       • web-javascript • Microsoft Edge 105.0.1343.27

[√] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

patzjo avatar Sep 07 '22 12:09 patzjo

I confirm the issue. It is a blocker because no alarms are triggered. I see the following log:

2022-09-08 20:19:04.272 10913-10913/? I/AndroidAlarmManagerPlugin: onAttachedToEngine
2022-09-08 20:19:04.620 10913-10913/? I/FlutterBackgroundExecutor: Starting AlarmService...
2022-09-08 20:19:04.630 10913-10913/? I/AndroidAlarmManagerPlugin: onAttachedToEngine
2022-09-08 20:19:04.661 10913-11065/? E/flutter: [ERROR:flutter/shell/common/shell.cc(93)] Dart Error: Dart_LookupLibrary: library 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart' not found.

When try to perform cancel I see: 2022-09-08 20:19:05.722 10913-10913/? I/AlarmService: cancel: broadcast receiver not found

And when the alarm is supposed to be fired the log is: 2022-09-08 20:21:00.056 10913-11339/? I/AlarmService: AlarmService has not yet started.

sirmamedical avatar Sep 08 '22 17:09 sirmamedical

Searching for Dart_LookupLibrary on GitHub gives multiple results: https://github.com/search?q=Dart_LookupLibrary&type=issues

One comment points to downgrade to 3.0.5: https://github.com/rmawatson/flutter_isolate/issues/117#issuecomment-1236579313

Check this solution: https://github.com/rmawatson/flutter_isolate/pull/118/files

to add @pragma('vm:entry-point') to your isolated method (printHello in your example).

Let me know if that helps, if so, we will have to update docs.

If anyone finds an official document explaining why that vm:entry-point thing is necessary that is also more than welcome

miquelbeltran avatar Sep 08 '22 19:09 miquelbeltran

https://github.com/dart-lang/sdk/blob/master/runtime/docs/compiler/aot/entry_point_pragma.md

miquelbeltran avatar Sep 08 '22 19:09 miquelbeltran

Another example: https://github.com/fluttercommunity/flutter_workmanager/issues/422#issuecomment-1236618119

miquelbeltran avatar Sep 08 '22 20:09 miquelbeltran

For me the only way to make it work was to add @pragma('vm:entry-point') above my static alarm callback handler and also in android_alarm_manager_plus.dart above _alarmManagerCallbackDispatcher() as:

@pragma('vm:entry-point')
void _alarmManagerCallbackDispatcher() {
  // Initialize state necessary for MethodChannels.
  WidgetsFlutterBinding.ensureInitialized();
...

sirmamedical avatar Sep 08 '22 20:09 sirmamedical

Thanks for checking! Can you open a pull request with the change? I'll be sure to review it quickly.

miquelbeltran avatar Sep 09 '22 04:09 miquelbeltran

Having the same issue, since upgrading flutter/dart - worked well before. Even tried adding the pragma annotation to my callbacks, but did not help - callbacks not launched. Added as follows:

@pragma('vm:entry-point')
Future<bool> myAlarmOneTask() async {

SherwinDeveloper avatar Sep 14 '22 11:09 SherwinDeveloper

I think that the @pragma('vm:entry-point') needs to be added to the plugin code, as explained in the previous comments. If anyone can do it and submit a PR, I'll quickly review and merge.

miquelbeltran avatar Sep 14 '22 11:09 miquelbeltran

Adding @pragma('vm:entry-point') above callbackDispatcher() causing app to keep crashes (closed) in release mode!

AnasSafi avatar Sep 22 '22 14:09 AnasSafi

just to confirm @AnasSafi , are you getting crash reports with 2.0.7 but not with 2.0.6?

miquelbeltran avatar Sep 23 '22 08:09 miquelbeltran

@miquelbeltran sorry for mistake, i face same problem with workmanager: 0.5.0 not android_alarm_manager_plus.

AnasSafi avatar Sep 23 '22 10:09 AnasSafi

Can you please tell me what I need to add in documentation?

bhattabhi013 avatar Oct 06 '22 21:10 bhattabhi013

Can be closed due to #1229

vbuberen avatar Oct 16 '22 08:10 vbuberen