You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter文档扫描器集成报错:Gradle构建失败求助

问题:Flutter文档扫描器包构建失败异常

我正尝试在Flutter应用中使用flutter document scanner包,每次调用扫描器时都会触发构建异常,无法完成图片扫描。

相关代码

import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'settings.dart';
import 'package:hello/settings.dart';
import 'package:flutter_document_scanner/flutter_document_scanner.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:io';
import 'package:document_scanner_flutter/configs/configs.dart';
import 'package:camera/camera.dart';
import 'package:http/http.dart' as http;

class Browserpage extends StatefulWidget {
  //constructor

  @override
  State<Browserpage> createState() => _Browserpage();
}

class _Browserpage extends State<Browserpage> {
  //file manipulation
  Image? scannedDoc;
  //constructor

  //delete
  var InvoiceResponse;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            //this takes in the scanner
            await OpenImageScanner(context, scannedDoc);
            //send data to the server
           
          },
          backgroundColor: Colors.lightBlue[500],
          child: Icon(
            Icons.add,
            size: 40,
          ),
        ),
        body: ListView(
          children: [
            ListTile(
                title: Padding(
                  padding: EdgeInsets.only(top: 30, left: 150),
                  child: Text(
                    "Veazy",
                    style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                  ),
                ),
                trailing: GestureDetector(
                  onTap: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => Settingspage()));
                  },
                  child: Padding(
                    padding: EdgeInsets.only(right: 10, top: 24),
                    child: Icon(Icons.account_circle, size: 40),
                  ),
                )),
            Divider(),
            Padding(
              padding: EdgeInsets.only(right: 300, left: 10),
              child: ElevatedButton(
                child: Text("Edit", style: TextStyle(fontSize: 20)),
                onPressed: () {
                  //what happens
                },
              ),
            ),
            Padding(
                padding: EdgeInsets.only(top: 10, left: 10, right: 10),
                child: Container(
                    decoration: BoxDecoration(
                        color: Colors.grey[300],
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(20),
                            topRight: Radius.circular(20))),
                    height: 600,
                    child: scannedDoc == null
                        ? Text("")
                        : FutureBuilder<PermissionStatus>(
                            builder: (BuildContext context,
                                    AsyncSnapshot<PermissionStatus> snapshot) =>
                                ListTile(
                                    // here add a gesture when clicked openes a new widget with file
                                    leading: GestureDetector(
                                      onTap: () {
                                        //open a widget here
                                      },
                                      child: scannedDoc == null
                                          ? Text("")
                                          : Image.file(scannedDoc as File),
                                    ),
                                    title: Text("Image")),
                          )))
          ],
        ));
  }
}

//function to open imange scanner
Future<void> OpenImageScanner(BuildContext context, Image? img) async {
  var img = await DocumentScannerFlutter.launch(context,
      source: ScannerFileSource.CAMERA,
      labelsConfig: {
        ScannerLabelsConfig.ANDROID_SAVE_BUTTON_LABEL: "SAVE",
      });
}

异常信息

FAILURE: Build failed with an exception.

* What went wrong:
A problem was found with the configuration of task ':camera_android:checkDebugManifest' (type 'CheckManifest').
  - In plugin 'com.android.build.gradle.api.AndroidBasePlugin' type 'com.android.build.gradle.internal.tasks.CheckManifest' property 'manifest' has @Input annotation used on property of type 'File'.

    Reason: A property of type 'File' annotated with @Input cannot determine how to interpret the file.

    Possible solutions:
      1. Annotate with @InputFile for regular files.
      2. Annotate with @InputDirectory for directories.
      3. If you want to track the path, return File.absolutePath as a String and keep @Input.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s
Exception: Gradle task assembleDebug failed with exit code 1
解决方案

这个异常是camera_android插件的Gradle配置问题,和业务代码无关,可按以下步骤修复:

  • 降级Gradle版本:若当前使用Gradle 7.5+,可降级到7.4.2版本。修改项目根目录gradle/wrapper/gradle-wrapper.properties文件中的distributionUrl:

    distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
    
  • 升级camera插件版本:检查pubspec.yaml中的camera插件版本,升级到最新稳定版,新版本通常会修复这类注解兼容问题。

  • 清理项目缓存:执行以下命令清理Flutter和Gradle缓存,重新构建项目:

    flutter clean
    rm -rf android/.gradle android/build ios/Pods ios/Podfile.lock
    flutter pub get
    cd ios && pod install && cd ..
    
  • 强制指定camera版本:flutter_document_scanner可能依赖旧版camera插件导致冲突,可在pubspec.yaml中直接指定最新版camera,覆盖传递依赖的旧版本。

内容的提问来源于stack exchange,提问作者fabrice lionel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 04:46:12