仅限发行版APK版本的Flutter问题 [英] Flutter issues with release-mode only APK builds

查看:101
本文介绍了仅限发行版APK版本的Flutter问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从已安装的APK运行应用程序时出现问题,但直接从Android Studio运行时,该应用程序运行良好.已在物理设备和仿真器上进行了测试,并具有调试,配置文件和发布的构建模式(其中发布和配置文件模式仅在物理设备上有效)

When running the app from an installed APK I have issues but when running directly from Android studio the app works well. Tested on Both physical devices and emulators, and also with build modes debug, profile and release (where release and profile modes of course only works on physical devices)

摘要: a)共享首选项,路径提供者和权限处理程序产生一个MissingPluginException. b)Android后退按钮不起作用.

Summary: a) Shared Prefs, Path Provider and Permission Handler produces a MissingPluginException. b) The Android back button does not work.

这些可能是单独的问题,但是我强烈怀疑它们之间存在关联,因为这两个问题仅在应用程序是从不直接从Android Studio直接安装的APK启动时发生的.

These may be separate issues but I have a strong suspicion that they are related because both of them occur only when the app is started from an APK not installed directly from Android Studio.

非常清楚-如果我安装"了从Android Studio中下载该应用程序,即使没有连接AS,我也可以继续使用该应用程序.但是,当我构建一个APK时,请手动安装它,并尝试运行该应用程序时,我会遇到由上述错误引起的各种症状.如果随后我通过Android Studio连接Logcat,则问题将一直存在,直到我使用AS安装新版本为止.

To be very clear - if I "install" the app from within Android Studio, I can then continue using the app even without AS connected. However when I build an APK, install it manually, and try to run the app I get various symptoms caused by the above errors. If I then connect Logcat via Android Studio, the issues will persist until I install a new build using AS.

更多详细信息:

  1. 在用于发布模式的APK构建过程中,gradle抱怨缺少"libs.jar"文件,例如:

Execution failed for task ':app:lintVitalQaRelease'.                    
> Could not resolve all artifacts for configuration ':app:devProfileRuntimeClasspath'.
   > Failed to transform libs.jar to match attributes {artifactType=processed-jar, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for JetifyTransform: /home/johan/AndroidStudioProjects/teacher_app/build/app/intermediates/flutter/devProfile/libs.jar.
         > Transform's input file does not exist: /home/johan/AndroidStudioProjects/teacher_app/build/app/intermediates/flutter/devProfile/libs.jar. (See https://issuetracker.google.com/issues/158753935)

寻找上述解决方案会带来一些已记录在案的案例,只是它们通常没有风味,因此在我逐渐获得时,:app:lintVitalRelease代替了:app:lintVitalQaRelease.此外,该错误还将在报告的情况下将缺少的中间体显示为.../flutter/Profile/libs.jar,再次没有味道.

Searching for solutions to the above brings up several documented cases, except that they generally don't have Flavors, so :app:lintVitalRelease in stead of :app:lintVitalQaRelease as I am getting. In addition the error will show the missing intermediates as .../flutter/Profile/libs.jar in the reported cases, again without the flavors.

在我的情况下,额外的Qadev分别是两种不同的口味.在其他情况下,解决方法是构建调试和配置文件模式apk以解决问题",在我的情况下,解决方法是构建调试和配置文件并发布所有其他版本的APK,而不仅仅是您自己的版本重新尝试构建".

In my case the extra Qa and dev respectively are two different flavors. Where in the other cases the workaround is "build your debug and profile mode apk to fix the issue", in my case it is "Build your debug and profile AND release APKs for all the other flavors, not just the one you're trying to build".

因此,我在尝试构建风味"Qa"和"Qa"之间切换.模式释放",以及它抱怨的任何一个,一遍又一遍,直到满意并生成APK.必须构建一堆不相关的样式和模式很烦人,但至少是一种可以让我获得APK的解决方法.

So I toggle between trying to build flavor "Qa" mode "release", and whichever one it complains about, over and over about until it is satisfied and produces the APK. It is annoying to have to build a bunch of unrelated flavors and modes, but at least is is a workaround that gets me an APK.

此后,我可以安装APK并取消安装,但出现了问题. logcat显示发生了一些MissingPlugin异常:

After this I am able to install and un from the APK, but with issues. logcat shows some MissingPlugin exceptions occuring:

MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)

和....

MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

最后: 在调查所有内容时,我尝试了 https://pub.dev/packages/permission_handler/example中的权限处理程序示例.我只是将其添加为新的页面"进入我的应用导航到此页面时,我得到另一个异常:

Finally: While investigating this all I tried the permission handler example from https://pub.dev/packages/permission_handler/example. I just added it as a new "page" into my app. On navigatign to this page I get another exception:

MissingPluginException(No implementation found for method checkPermissionStatus on channel flutter.baseflow.com/permissions/methods)

应用中与这些插件相关的许多功能均无法正常工作-图片无法显示,用户需要在每次重新启动时进行身份验证,依此类推.这些显然取决于上述插件.

A lot of functionality in the app related to these plugins are not working - Photos don't display, user needs to authenticate on each restart, and so on. These are clearly dependent on the above plugins.

我尝试将WRITE_EXTERNAL_STORAGE添加到AndroidManifest,甚至将其添加到每种口味的单独清单中. (我通常将共享配置仅添加到主要的AndroidManifest.xml中)

I've tried to add WRITE_EXTERNAL_STORAGE to the AndroidManifest, even added it to the separate manifests for each of the flavors. (I normally add shared config to the main AndroidManifest.xml only)

为了防止Android不愿向未签名的APK授予权限,我已经实现了对发布模式的签名.

I've implemented signing for release mode just in case Android doesn't like to give permissions to unsigned APKs.

为了消除导致问题的原因,我将所有实例改回了创建项目时使用的原始程序包名称.这意味着一次只能在我的设备上安装一种口味.但是,它没有任何区别.

To eliminate the application-ID as being the cause of the issue I've changed all instances back to the original package name used when the project was created. This means only one flavor can be installed on my device at a time. It however does not make any difference.

很明显,我已经完成了多次清理工作,每次都迫使我经历一个接一个地构建所有缺失的中间体的过程.

I've obviously done flutter-clean and flutter-get many times, each time forcing me to go through the process of building all the missing intermediates one by one.

另一件事..."Android后退"按钮不起作用.如果我直接从Android Studio运行该应用程序,则效果很好,但仅从APK运行该应用程序却无法正常工作(似乎被忽略了).

One other thing... The Android Back button doesn't work. It works fine if I run the app directly from Android Studio, but doesn't work (seems to be ignored) when running it from APK only.

我为此感到无所适从.

进度!
我创建了一个新项目,并复制了源代码(lib/*).我还复制了pubspec.yaml和资产.我没有对build.gradle文件进行任何更改,仅对主AndroidManifest.xml进行了两项更改-应用程序名称并添加了Internet权限.

EDIT #2: Progress!
I created a new project and copied the source (lib/*) over. I also copied pubspec.yaml and the assets. I have made no changes to the build.gradle file, and only made two changes to the main AndroidManifest.xml - the Application Name and added the Internet permission.

我也确实设置了应用程序图标.

I also did set the app icon.

复制源非常麻烦-导入很多很多,但最终结果是可行的构建.

Copying the source over was ardorous - there being many many many imports but the end result is a working build.

SharedPrefs在重启后仍然存在 显示下载的图像 Android后退按钮有效 存储的设备权限显示为已授予 从应用程序内打开相机会在第一次请求权限. 该应用可以将用户发送到Android应用设置屏幕.

SharedPrefs survives restarts Downloaded images are displayed Android back-button works Device Permissions for Storage shows as granted Opening the camera from within the app asks for permission the first time. The app can send the user to the Android App settings screen.

一切似乎都可行.我可能在某个地方犯了一个错误,因为新应用程序在主屏幕上显示了正确的应用程序图标,但在该应用程序的Android设置页面中显示了Flutter默认图标.

Everything seems to work. I probably made a mistake somewhere because the new app shows the correct app Icon on the home screen but shows the Flutter default icon in the Android settings page for the app.

下一步是对APK进行签名,然后重新引入风味,即使只是为了设置飞镖目标,也希望可以设置其他属性.

The next steps would be to sign the APK and then to re-introduce flavors, even if just to be able to set the dart target, but hopefully also to set other properties.

推荐答案

我遇到了这个确切的问题.我用几种不同的方法解决了这个问题:

I had this exact problem. I solved it with a couple different ways:

  1. 将proguard-rules.pro添加到android/app/proguard-rules.pro

#Flutter Wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
-keep class androidx.lifecycle.** { *; } #https://github.com/flutter/flutter/issues/58479
#https://medium.com/@swav.kulinski/flutter-and-android-obfuscation-8768ac544421

  1. 在应用程序级别build.gradle中将proguard添加到buildTypes

buildTypes {
    release {
        profile {
            matchingFallbacks = ['debug', 'release']
        }
    minifyEnabled true
    useProguard true
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    signingConfig signingConfigs.release
    }
}
lintOptions {
    disable 'InvalidPackage'
    checkReleaseBuilds false
}

  1. 您也可以尝试在终端中运行主频道:

flutter channel master

  1. 我还通过导航到SDK管理器-> SDK工具->检查并下载Google Play服务,将Android迁移到Android Studio中的AndroidX

  1. I also migrated android to AndroidX in Android Studio by navigating to SDK manager->SDK tools->checking and downloading Google Play services

我还确保编译SDK和目标SDK为29;目前,SDK 30的permission_handler软件包存在问题.

I also made sure that compile SDK and target SDK is 29; there are issues with permission_handler package with SDK 30 at the moment.

我还编辑了kotlin主要活动文件:

I also edited the kotlin main activity file:

package yourpackage
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

这篇关于仅限发行版APK版本的Flutter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆