【问题标题】:Flutter local notification causing crash, only in the APK versionFlutter 本地通知导致崩溃,仅在 APK 版本中
【发布时间】:2019-09-27 11:52:13
【问题描述】:

我在 1.9 更新之前让这个应用程序工作,然后必须通过将 gradle 版本从 3.2.1 切换到 3.3.1 来解决构建失败

从那时起,对本地通知插件的任何非配置调用都会遇到此堆栈跟踪:

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). 

我没有对正在运行的版本中的代码进行任何更改。 用于配置插件的代码在init上运行如下

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

对插件的调用是这样的:

  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
  }

现在,有趣的是,只有在安装 apk 后才会发生崩溃。在使用颤振运行或调试运行的开发构建中,不会发生崩溃。

我尝试过的事情: - 检查图标,因为消息提到可绘制。如果找不到图标,配置甚至不会运行。

  • 在调用周围使用 try/catch 捕获异常:异常直接通过

  • 检查通知权限,一切正常,正如我所提到的,代码在更新之前可以工作

  • 调用是用正确的时间(小时和分钟)进行的,即使它们不是取消调用仍然会使应用程序崩溃

  • 检查在没有预定通知的情况下对 id 的取消调用是否会崩溃,它不在更新之前工作的版本上

  • 添加重要性、优先级和更改频道名称和 ID,没有用,此时已绝望

我觉得我缺少一些资源,有没有办法更全面地了解崩溃?

【问题讨论】:

    标签: android flutter


    【解决方案1】:

    我遇到了同样的问题,这让我发疯了。它只发生在发布版本上,并且似乎与 AndroidInitializationSettings('app_icon')

    Android 集成的 flutter_local_notifications 文档部分说要添加

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

    到 /android/app/proguard-rules.pro

    【讨论】:

    • 为我工作。而且 apk 的大小也大大减少了。
    • 也为我工作。我认为是应用程序的资源。
    • 为我工作!我想我花了大约 3 天的时间试图在我的发布版本中修复这个崩溃。吸取教训,阅读所有文档。这应该是这个问题的解决方案。
    • 快速跟进,Flutter 建议现在使用 R8,因此不再需要 ProGuard。 flutter.dev/docs/deployment/android#r8
    • 不知道为什么它在 Android 中如此复杂,我试图解决这个问题好几天...在我的颤振应用程序中我找不到 proguard 文件...我需要创建一个吗?
    【解决方案2】:

    如果您导出发布 apk 格式,则安装后应用程序崩溃。 您必须执行以下步骤:

    1. 已添加proguard-files.pro file 比你必须在文件底部添加这段代码
    ## flutter_local_notification plugin rules
    -keep class com.dexterous.** { *; }
    
    
    1. 您的 build.gradle 文件必须是
    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'
        }
    }
    
    

    【讨论】:

      【解决方案3】:

      我尝试添加:

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

      在proguard-rules.pro中,但在APK版本中仍然崩溃,然后我尝试在中设置false

      build.gradle
        buildTypes {
          release {
             minifyEnabled false
             shrinkResources false
             }
            }
      

      它对我有用

      【讨论】:

        【解决方案4】:

        我也遇到过同样的问题。我将以下行添加到 proguard-rules.pro -keep class com.dexterous.** { *; } 但没有任何效果。问题是 proguard-rules.pro 文件位于本地通知插件库中。

        所以我在android/app 目录中创建了这个文件的副本,最后它可以工作了。我在这个问题上花了将近 2 天的时间。

        不要忘记将this 文件添加到res/raw 目录以保留您的图标资源以供您通知。

        【讨论】:

          【解决方案5】:

          设置minifyEnabled false 将禁用你的proguard!

          您可以将此规则 -keepclassmembers enum * {*;} 添加到您的 /android/app/proguard-rules.pro 以解决此问题。

          【讨论】:

            【解决方案6】:

            我遇到了同样的问题,这 2 个更改对我有用:(我正在构建一个 appbundle)

            1)

            如果您构建 App Bundle 编辑 android/gradle.properties 并添加标志:

            android.bundle.enableUncompressedNativeLibs=false
            

            如果您构建 APK,请确保 android/app/src/AndroidManifest.xml 未在 &lt;application&gt; 标记中设置 android:extractNativeLibs=false

            2)

            flutter build appbundle --no-shrink
            

            https://flutter.dev/docs/deployment/android#r8

            【讨论】:

              【解决方案7】:

              我也有同样的问题。

              我使用了以下内容:

              buildTypes {
                      release {
                         minifyEnabled false
              }
                  }
              

              请注意,这会禁用您的 proguard。

              【讨论】:

              • 欢迎来到 StackOverflow。尝试更清楚地解释为什么这是问题的答案。
              • @JeroenHeier 感谢您的热烈欢迎:)。据我所知->“AAPT2 根据对应用程序清单、布局和其他应用程序资源中的类的引用生成保留规则。例如,AAPT2 为您在应用程序清单中注册的每个活动都包含一个保留规则作为入口点。”所以,这个“Drawable”正在“优化”,所以我猜它在发布版本中已经消失了,因此产生了这个问题。
              • 很有希望,但仍然遇到同样的错误,必须激活 shrinkResources false 才能使构建工作
              • 禁用缩小不是一个好的解决方案。您应该将 -keep class com.dexterous.** { *; } 行添加到您的 proguard-rules 文件中。
              【解决方案8】:

              基本上你应该做的是:

              1. 创建一个名为 proguard-rules.pro 的文件
              2. 复制 this 并粘贴到创建的文件 (proguard-rules.pro) 中。
              -keep class com.dexterous.** { *; }
              
              1. 构建。

              您可以在包文档中阅读有关为什么要这样做的更多详细信息。

              【讨论】:

              • “this”链接有问题;能否请edit 提供包含所有必需代码的答案?
              猜你喜欢
              • 2023-02-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-12
              • 1970-01-01
              相关资源
              最近更新 更多