颤振:失败:构建失败并出现异常

Posted

技术标签:

【中文标题】颤振:失败:构建失败并出现异常【英文标题】:flutter : FAILURE: Build failed with an exception 【发布时间】:2021-03-04 01:26:28 【问题描述】:

我尝试使用颤振开发应用程序,因此我添加了这些依赖项(file_picker:^2.1.0,路径:^1.7.0,dio:^3.0.10,)但是当我删除这些依赖项时出现此错误错误消失了,当我添加它们时,错误又回来了我在***中尝试了一些解决方案但无法解决

`
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Android resource linking failed
     C:\Users\hp\AndroidStudioProjects\youtubeclone\build\file_picker\intermediates\library_manifest\debug\AndroidManifest.xml:9:5-15:15: AAPT: error: unexpected element <queries> found in <manifest>.


* 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 1m 45s
The built failed likely due to AndroidX incompatibilities in a plugin. The tool is about to try using Jetfier to solve the incompatibility.
Building plugin file_picker...

C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:10: error: cannot find symbol
import androidx.lifecycle.DefaultLifecycleObserver;
                         ^
  symbol:   class DefaultLifecycleObserver
  location: package androidx.lifecycle
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:20: error: cannot find symbol
import io.flutter.embedding.engine.plugins.lifecycle.FlutterLifecycleAdapter;
                                                    ^
  symbol:   class FlutterLifecycleAdapter
  location: package io.flutter.embedding.engine.plugins.lifecycle
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:38: error: cannot find symbol
            implements Application.ActivityLifecycleCallbacks, DefaultLifecycleObserver 
                                                               ^
  symbol:   class DefaultLifecycleObserver
  location: class FilePickerPlugin
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:45: error: method does not override or implement a method from a supertype
        @Override
        ^
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:49: error: method does not override or implement a method from a supertype
        @Override
        ^
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:53: error: method does not override or implement a method from a supertype
        @Override
        ^
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:57: error: method does not override or implement a method from a supertype
        @Override
        ^
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:61: error: method does not override or implement a method from a supertype
        @Override
        ^
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:66: error: method does not override or implement a method from a supertype
        @Override
        ^
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:277: error: cannot find symbol
            this.lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(activityBinding);
                             ^
  symbol:   variable FlutterLifecycleAdapter
  location: class FilePickerPlugin
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:278: error: incompatible types: FilePickerPlugin.LifeCycleObserver cannot be converted to LifecycleObserver
            this.lifecycle.addObserver(this.observer);
                                           ^
C:\src\flutter\.pub-cache\hosted\pub.dartlang.org\file_picker-2.1.0\android\src\main\java\com\mr\flutter\plugin\filepicker\FilePickerPlugin.java:287: error: incompatible types: FilePickerPlugin.LifeCycleObserver cannot be converted to LifecycleObserver
            this.lifecycle.removeObserver(this.observer);
                                              ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
12 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileReleaseJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* 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 11s

Exception: The plugin file_picker could not be built due to the issue above.
`

这是我的 build.gradle

`def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) 
    localPropertiesFile.withReader('UTF-8')  reader ->
        localProperties.load(reader)
    


def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) 
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")


def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) 
    flutterVersionCode = '1'


def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) 
    flutterVersionName = '1.0'


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android 
    compileSdkVersion 29

    sourceSets 
        main.java.srcDirs += 'src/main/kotlin'
    

    lintOptions 
        disable 'InvalidPackage'
    

    defaultConfig 
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.zaidounmohamed.youtubeclone"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    

    buildTypes 
        release 
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        
    


flutter 
    source '../..'


dependencies 
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"


`

这是我的代码

`import 'package:dio/dio.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:path/path.dart';

class CustomFilePicker extends StatefulWidget 
  @override
  State<StatefulWidget> createState() 
    return _CustomFilePicker();
  


class _CustomFilePicker extends State<CustomFilePicker> 
  FilePickerResult selectedfile;
  Response response;
  String progress;
  Dio dio = new Dio();

  selectFile() async 
    selectedfile = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['jpg', 'pdf', 'mp4', 'doc'],
      //allowed extension to choose
    );
    setState(() ); //update the UI so that file name is shown
  

  uploadFile() async 
    String uploadurl = "http://192.168.0.112/test/file_upload.php";
    //dont use http://localhost , because emulator don't get that address
    //insted use your local IP address or use live URL
    //hit "ipconfig" in windows or "ip a" in linux to get you local IP

    FormData formdata = FormData.fromMap(
      "file": await MultipartFile.fromFile(selectedfile.paths.toString(),
          filename: basename(selectedfile.paths.toString())
          //show only filename from path
          ),
    );

    response = await dio.post(
      uploadurl,
      data: formdata,
      onSendProgress: (int sent, int total) 
        String percentage = (sent / total * 100).toStringAsFixed(2);
        setState(() 
          progress = "$sent" +
              " Bytes of " "$total Bytes - " +
              percentage +
              " % uploaded";
          //update the progress
        );
      ,
    );

    if (response.statusCode == 200) 
      print(response.toString());
      //print response from server
     else 
      print("Error during connection to server.");
    
  

  @override
  Widget build(BuildContext context) 
    return Scaffold(
        appBar: AppBar(
          title: Text("Select File and Upload"),
          backgroundColor: Colors.orangeAccent,
        ), //set appbar
        body: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.all(40),
            child: Column(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.all(10),
                  //show file name here
                  child: progress == null
                      ? Text("Progress: 0%")
                      : Text(
                          basename("Progress: $progress"),
                          textAlign: TextAlign.center,
                          style: TextStyle(fontSize: 18),
                        ),
                  //show progress status here
                ),

                Container(
                  margin: EdgeInsets.all(10),
                  //show file name here
                  child: selectedfile == null
                      ? Text("Choose File")
                      : Text(basename(selectedfile.paths.toString())),
                  //basename is from path package, to get filename from path
                  //check if file is selected, if yes then show file name
                ),

                Container(
                    child: RaisedButton.icon(
                  onPressed: () 
                    selectFile();
                  ,
                  icon: Icon(Icons.folder_open),
                  label: Text("CHOOSE FILE"),
                  color: Colors.redAccent,
                  colorBrightness: Brightness.dark,
                )),

                //if selectedfile is null then show empty container
                //if file is selected then show upload button
                selectedfile == null
                    ? Container()
                    : Container(
                        child: RaisedButton.icon(
                        onPressed: () 
                          uploadFile();
                        ,
                        icon: Icon(Icons.folder_open),
                        label: Text("UPLOAD FILE"),
                        color: Colors.redAccent,
                        colorBrightness: Brightness.dark,
                      ))
              ],
            )));
  
`

【问题讨论】:

试试answer 所说的内容。 【参考方案1】:

    删除android文件夹内的.gradle文件夹

    在项目build.gradle文件中,升级你的类路径

    例子: classpath 'com.android.tools.build:gradle:4.0.0'

    转到android&gt;gradle&gt;gradle-wrapper.properties 文件并升级分发网址。

    例子: distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.0-all.zip

    使缓存无效并重新启动您的 android studio,它将下载新的 gradle 文件。

【讨论】:

【参考方案2】:

通常,androidResourceLinkingFailed 错误通过清除缓存来解决。

如果您使用的是 Android Studio,请转到 File &gt; Invalidate Caches / Restart 并重新启动您的 IDE,或在终端中运行此命令:flutter clean

【讨论】:

我运行它并没有任何改变,但我会再次运行它

以上是关于颤振:失败:构建失败并出现异常的主要内容,如果未能解决你的问题,请参考以下文章

android studio:失败:构建失败并出现异常

FAILURE:构建失败并出现异常。无法解析所有文件以进行配置

颤振 gradle 任务 assembleDebug 失败

离子构建失败:失败:构建失败并出现异常

颤振位置包在构建项目中有错误

长时间打开后无法构建颤振项目