Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter local notification causing crash, only in the APK version

I had this app working before the 1.9 update, then a failure to build had to be worked around by switching the gradle version from 3.2.1 to 3.3.1

Ever since, any non-configuration calls to the local notification pluggin are met with this stacktrace:

E/flutter (22738): [ERROR:flutter/shell/platform/android/platform_view_android_jni.cc(39)] java.lang.AssertionError: java.lang.NoSuchFieldException: Drawable
E/flutter (22738):      at c.a.a.b.a.ja$a.<init>(:12)
E/flutter (22738):      at c.a.a.b.a.V.a(:5)
E/flutter (22738):      at c.a.a.p.a(:18)
E/flutter (22738):      at c.a.a.b.a.p.a(:21)
E/flutter (22738):      at c.a.a.b.a.p.a(:37)
E/flutter (22738):      at c.a.a.b.a.p.a(:17)
E/flutter (22738):      at c.a.a.p.a(:18)
E/flutter (22738):      at c.a.a.b.a.c.a(:5)
E/flutter (22738):      at c.a.a.p.a(:18)
E/flutter (22738):      at c.a.a.p.a(:96)
E/flutter (22738):      at c.a.a.p.a(:86)
E/flutter (22738):      at c.a.a.p.a(:84)
E/flutter (22738):      at com.dexterous.flutterlocalnotifications.d.e(:7)
E/flutter (22738):      at com.dexterous.flutterlocalnotifications.d.a(:49)
E/flutter (22738):      at com.dexterous.flutterlocalnotifications.d.a(:157)
E/flutter (22738):      at com.dexterous.flutterlocalnotifications.d.b(:40)
E/flutter (22738):      at com.dexterous.flutterlocalnotifications.d.a(:132)
E/flutter (22738):      at d.a.b.a.m$a.a(:2)
E/flutter (22738):      at io.flutter.embedding.engine.a.c.a(:14)
E/flutter (22738):      at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(:2)
E/flutter (22738):      at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (22738):      at android.os.MessageQueue.next(MessageQueue.java:326)
E/flutter (22738):      at android.os.Looper.loop(Looper.java:160)
E/flutter (22738):      at android.app.ActivityThread.main(ActivityThread.java:6762)
E/flutter (22738):      at java.lang.reflect.Method.invoke(Native Method)
E/flutter (22738):      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/flutter (22738):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E/flutter (22738): Caused by: java.lang.NoSuchFieldException: Drawable
E/flutter (22738):      at java.lang.Class.getField(Class.java:1601)
E/flutter (22738):      at c.a.a.b.a.ja$a.<init>(:6)
E/flutter (22738):      ... 26 more
E/flutter (22738): 
F/flutter (22738): [FATAL:flutter/shell/platform/android/platform_view_android_jni.cc(76)] Check failed: CheckException(env). 

I made no changes to the code from the version that was working. The code used to configure the plugin is ran on init as follows

  void inicializarPluginNotificacoes() {
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    var initializationSettingsAndroid = new AndroidInitializationSettings('@drawable/app_icon24');
    var initializationSettings = new InitializationSettings(initializationSettingsAndroid, null);
    flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);
  }

and the call to the plugin is made like this:

  Future marcarNotificacao(int id, int hora, int minuto, String texto, String titulo) async {
    var scheduledNotificationDateTime = new DateTime.now().add(new Duration(hours: hora, minutes: minuto));
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails('ponto_unb', 'Ponto', 'Notificações para o ponto eletrônico');
    NotificationDetails platformChannelSpecifics = new NotificationDetails(androidPlatformChannelSpecifics, null);
    await flutterLocalNotificationsPlugin.cancel(id); // crashes
    await flutterLocalNotificationsPlugin.schedule(id, titulo, texto, scheduledNotificationDateTime, platformChannelSpecifics); // also crashes if the other one is not called first
  }

Now, the intresting thing is, the crash only takes place after installing the apk. On a development build with flutter run or a debug run the crash doesn't happen.

Things i've tried: - Checking the icon since the message mentions drawable. The configuration doesn't even run if the icon is not found.

  • Catching the exception with a try/catch around the call: the exception goes right through

  • Checking notification permissions, its all fine, as i mentioned, the code was working prior to the update

  • The calls are made with corect times ( hours and minutes ), and even if they weren't the cancel call still would crash the app

  • Check if the cancel call to an id without scheduled notifications would crash, it didn't on the version that worked prior to the update

  • adding importance, priority and changing the channel name and id, didn't work, was getting desperate at this point

I feel like there is some resource i'm missing, is there a way to obtain a more comprehensive look into the crash?

like image 897
Lucas Moreira Avatar asked Sep 27 '19 11:09

Lucas Moreira


2 Answers

I had the same problem and it was driving me nuts. It only happens on a release build and appears to be related to AndroidInitializationSettings('app_icon')

The flutter_local_notifications docs section on Android Integration says to add

-keep class com.dexterous.** { *; }

to /android/app/proguard-rules.pro

like image 66
Joao Avatar answered Oct 30 '22 20:10

Joao


If you export release apk format so after installed crash the app. You have to make these steps:

  1. Added proguard-files.pro file Than you have to added bottom of the file this code
## flutter_local_notification plugin rules
-keep class com.dexterous.** { *; }

  1. Your build.gradle file have to be
buildTypes {

    release {
        signingConfig signingConfigs.release
        // Enables code shrinking, obfuscation, and optimization for only
        // your project's release build type.
        minifyEnabled false

        // Enables resource shrinking, which is performed by the
        // Android Gradle plugin.
        shrinkResources false

        // Includes the default ProGuard rules files that are packaged with
        // the Android Gradle plugin. To learn more, go to the section about
        // R8 configuration files.
        proguardFiles getDefaultProguardFile(
                'proguard-android-optimize.txt'),
                'proguard-rules.pro'
    }
}

like image 5
user2532428 Avatar answered Oct 30 '22 20:10

user2532428