4

我正在尝试在我的 Android 版 Flutter 应用中实现 App Check,并遵循了 flutterfire 文档。我已经完成了此处概述的安装部分:https ://firebase.flutter.dev/docs/app-check/overview 现在我正在关注此文档以供使用:https ://firebase.flutter.dev/docs/app-检查/使用

因此await FirebaseAppCheck.instance.activate(webRecaptchaSiteKey: 'recaptcha-v3-site-key');,在调用初始化 firebase 之后,我将其添加到了我的 Main 方法中。

现在我需要使用 App Check 启用调试,并且文档说我应该将此依赖项添加到我的 app/build.gradle 文件中:implementation 'com.google.firebase:firebase-appcheck-debug:16.0.0-beta01'并将以下代码片段添加到我的 MainActivity.java onCreate 方法中:

import com.google.firebase.appcheck.FirebaseAppCheck;

FirebaseApp.initializeApp(/*context=*/ this);
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
firebaseAppCheck.installAppCheckProviderFactory(DebugAppCheckProviderFactory.getInstance());

我试图通过在我的项目根文件夹中使用以下命令创建 MainActivity.java 来做到这一点:flutter create -a java .

所以我的 MainActivity.java 看起来像这样:

import io.flutter.embedding.android.FlutterActivity;
import com.google.firebase.appcheck.FirebaseAppCheck;

public class MainActivity extends FlutterActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FirebaseApp.initializeApp(/*context=*/ this);
            FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
            firebaseAppCheck.installAppCheckProviderFactory(
            DebugAppCheckProviderFactory.getInstance());
      }
}

尝试在调试模式下运行应用程序时出现此错误: Execution failed for task ':app:compileDebugJavaWithJavac'.

我错过了什么?看到其他帖子有同样的问题,但没有解决方案。

4

1 回答 1

0

这应该是你的main.dart

import 'package:firebase_app_check/firebase_app_check.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await FirebaseAppCheck.instance.activate();
}

注:Android/iOS Appawait FirebaseAppCheck.instance.activate();不具备webRecaptchaSiteKey: 'recaptcha-v3-site-key'

这应该是你的MainActivity.kt

package com.example.app // your package

import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory

class MainActivity: FlutterActivity() {
    private val CHANNEL = "samples.flutter.dev/example"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            call, result ->
            FirebaseApp.initializeApp(/*context=*/this)
            val firebaseAppCheck = FirebaseAppCheck.getInstance()
            firebaseAppCheck.installAppCheckProviderFactory(
                    DebugAppCheckProviderFactory.getInstance()
            )
        }
    }
}
于 2021-08-19T10:21:06.377 回答