[英]In Flutter, I am using local_auth 0.6.2+3 package and implemented the example code. I get the following error when I click the authenticate button
Package used https://pub.dev/packages/local_auth/versions Code implemented exactly as in the example. Package 使用了 https://pub.dev/packages/local_auth/versions代码实现与示例中完全相同。 https://pub.dev/packages/local_auth#-example-tab-
https://pub.dev/packages/local_auth#-example-tab-
Error Description: I/flutter ( 7978): PlatformException(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null)错误描述:I/flutter(7978):PlatformException(no_fragment_activity,local_auth 插件要求活动为 FragmentActivity。,null)
Here is the screen shot of final result.这是最终结果的屏幕截图。 Error occurs on pressing the third button "Authenticate".
按第三个按钮“验证”时发生错误。 It changes to "Cancel", and throws the above error to the console for "no_fragment_activity".
它更改为“取消”,并将上述错误抛出到“no_fragment_activity”的控制台。
I've just built the local_auth sample and it works (vers. 0.6.3.4).我刚刚构建了 local_auth 示例,它可以工作(版本 0.6.3.4)。 Here after the steps I've done:
在我完成的步骤之后:
create a new flutter app:创建一个新的 flutter 应用程序:
flutter create -a java -t app --project-name mywallet mywallet flutter create -a java -t app --project-name mywallet mywallet
Note, I'm using the java language.注意,我使用的是 java 语言。
dependencies:
local_auth: ^0.6.3+4
copy the main.dart class from the: local_auth page从以下位置复制 main.dart class: local_auth 页面
modify the MainActivity.java in this manner:以这种方式修改 MainActivity.java:
MainActivity.java
MainActivity.java
package com.example.mywallet;
import android.os.Bundle;
import io.flutter.app.FlutterFragmentActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.os.Bundle;
import io.flutter.app.FlutterFragmentActivity;
import io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin;
import io.flutter.plugins.localauth.LocalAuthPlugin;
@SuppressWarnings("deprecation")
public class MainActivity extends FlutterFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FlutterAndroidLifecyclePlugin.registerWith(
registrarFor(
"io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin"));
LocalAuthPlugin.registerWith(registrarFor("io.flutter.plugins.localauth.LocalAuthPlugin"));
}
}
Then, modify the android manifest in order to allow the flutter fragment to start:然后,修改 android 清单以允许 flutter 片段启动:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mywallet">
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
<application android:name="io.flutter.app.FlutterApplication" android:label="local_auth_example" android:icon="@mipmap/ic_launcher">
<activity android:name="io.flutter.embedding.android.FlutterFragmentActivity"
android:launchMode="singleTop"
android:theme="@style/Theme.AppCompat.Light"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
</activity>
<meta-data android:name="flutterEmbedding" android:value="2"/>
</application>
</manifest>
Now, the app is working if you are building it in debug mode, unfortunately in release mode it will take the following exception:现在,如果您在调试模式下构建应用程序,则该应用程序正在运行,不幸的是,在发布模式下,它将出现以下异常:
E/AndroidRuntime(27487): FATAL EXCEPTION: main
E/AndroidRuntime(27487): Process: com.example.mywallet, PID: 27487
E/AndroidRuntime(27487): java.lang.IllegalAccessError: Interface androidx.lifecycle.b implemented by class io.flutter.plugins.localauth.AuthenticationHelper is inaccessible (declaration of 'io.flutter.plugins.localauth.AuthenticationHelper' appears in /data/app/com.example.mywallet-t-m1EbOiZo85yTCgTK9tWw==/base.apk)
E/AndroidRuntime(27487): at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(Unknown Source:16)
E/AndroidRuntime(27487): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(27487): at io.flutter.embedding.engine.h.g.a.a(Unknown Source:25)
E/AndroidRuntime(27487): at io.flutter.embedding.android.FlutterFragmentActivity.g(Unknown Source:0)
In order to avoid this issue, you can add the file:为了避免这个问题,您可以添加文件:
proguard-rules.pro
in android/app/src with the following line:在 android/app/src 中使用以下行:
-keep class androidx.lifecycle.DefaultLifecycleObserver
Now it works!现在它起作用了!
Here after my flutter doctor:在我的 flutter 医生之后:
~/Projects/mywallet$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 1.25.0-5.0.pre.93, on Linux, locale it_IT.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc4)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 4.0)
[✓] IntelliJ IDEA Community Edition (version 2020.2)
[✓] VS Code (version 1.51.1)
[✓] Connected device (4 available)
• No issues found!
The simple app was tested on Samsung (with knox) in release mode and on the emulator:这个简单的应用程序在三星(使用 knox)上以发布模式和模拟器进行了测试:
~/Projects/mywallet$ flutter devices
4 connected devices:
SM A307FN (mobile) • RF8M9278QSF • android-arm64 • Android 10 (API 29)
Android SDK built for x86 (mobile) • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)
Linux (desktop) • linux • linux-x64 • Linux
Chrome (web) • chrome • web-javascript • Google Chrome 87.0.4280.88
You just forgot to change from FlutterActivity
to FlutterFragmentActivity
.您只是忘记从
FlutterActivity
更改为FlutterFragmentActivity
。
This step is present at the README of local_auth
package, in the section Android Integration
此步骤出现在
local_auth
package 的自述文件中,在Android Integration
部分中
Note that local_auth plugin requires the use of a FragmentActivity as opposed to Activity.
请注意,local_auth 插件需要使用 FragmentActivity 而不是 Activity。 This can be easily done by switching to use FlutterFragmentActivity as opposed to FlutterActivity in your manifest (or your own Activity class if you are extending the base class).
这可以通过在清单中切换到使用 FlutterFragmentActivity 而不是 FlutterActivity 来轻松完成(如果您正在扩展基类,则可以使用您自己的 Activity class)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.