gradle 在 Android Studio 中使用颤振失败

Posted

技术标签:

【中文标题】gradle 在 Android Studio 中使用颤振失败【英文标题】:gradle fails on using flutter in Android Studio 【发布时间】:2018-12-19 18:51:03 【问题描述】:

我是 android Studio 和 Flutter 的新手。我最近想尝试 Flutter,因此在 Android Studio 中安装了该插件。我的 Flutter 代码没有 bug,但是 gradle 失败了。

应用级构建等级:

def localProperties = new Properties()  //here, Properties gets highlighted as error
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.")        //here,GradleException gets highlighted as error


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

android 
    compileSdkVersion 27

    lintOptions 
        disable 'InvalidPackage'
    

    defaultConfig 
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.mee.createevent"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    

    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 
    compile 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.android.gms:play-services-analytics:16.0.1'
    implementation 'com.google.firebase:firebase-analytics:16.0.1'
    implementation 'com.google.firebase:firebase-firestore:17.0.2'
    implementation 'com.google.firebase:firebase-functions:16.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'



apply plugin: 'com.google.gms.google-services'

模块级构建gradle

buildscript 
    repositories 
        google()
        jcenter()
    

    dependencies 
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.google.gms:google-services:4.0.1'
    


allprojects 
    repositories 
        google()
        jcenter()
    


rootProject.buildDir = '../build'
subprojects 
    project.buildDir = "$rootProject.buildDir/$project.name"

subprojects 
    project.evaluationDependsOn(':app')


task clean(type: Delete) 
    delete rootProject.buildDir

我的错误:

FAILURE: Build failed with an exception.

* What went wrong:
Failed to notify dependency resolution listener.
> The library com.google.android.gms:play-services-measurement-base is being requested by various other libraries at [[16.0.0,16.0.0]], but resolves to 15.0.2. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.
> The library com.google.firebase:firebase-analytics is being requested by various other libraries at [[16.0.1,16.0.1]], but resolves to 15.0.2. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.

* 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 20s
Finished with error: Gradle build failed: 1

我尝试包含一些依赖项,例如 play-service 版本和 firebase 分析版本,但此错误不会消失,Properties 和 GradleException 仍以红色突出显示为错误。我不明白如何解决这个问题。

pubspec.yaml

name: create_event
description: A new Flutter application.

dependencies:
  cloud_firestore:

  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for ios style icons.
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.io/custom-fonts/#from-packages

当我打开应用级构建 gradle image flutter (它说它无法解析 Properties() 和 FileNotFoundException() )

但是,当我点击“在 Android Studio 中打开编辑”image androidStudio

但是,在 Android Studio 案例中的成功,并没有体现在 Flutter 中。它仍然有这些错误。

【问题讨论】:

鉴于您粘贴的失败,我建议阅读此链接。它也与 Android 项目有关。 docs.gradle.org/current/dsl/… 该错误显示了依赖问题,但诸如 Properties() 和 FileNotFoundException() 之类的其他内容在实际代码中被突出显示为错误(我刚刚附上了突出显示的图像) 老实说,我对 Flutter 应用程序不是很熟悉。属性是必需的吗?试着把它放在你的根 build.gradle 中。另外,不要把 IDE 高亮看得太认真。重要的是当你尝试构建时会发生什么。我肯定会建议解决所有 BUILD 错误,看看你会得到什么。 你是对的,那个亮点并不重要。谢谢 :) 很高兴听到这个消息!在这种情况下,这只是工具(您的 IDE)的限制。我的 build.gradle 中也有“红色”代码,但一切都构建得很好,所以我有一条评论“不要删除这个!!!!!!” :) 【参考方案1】:

可以通过以下步骤消除此错误:

    如果可能的话,在你的项目>>android>>app>>build.gradle 文件中增加你的最小 sdk 版本 2.然后在project>>android>>app>>build.gradle中添加如下一行 依赖项

实现'androidx.multidex:multidex:2.0.1'

我希望你的问题现在已经解决了。

【讨论】:

【参考方案2】:

更改类路径“com.google.gms:google-services:4.3.2” 类路径'com.google.gms:google-services:4.2.0'

【讨论】:

【参考方案3】:

当我遇到同样的问题时,我能找到解决它的唯一方法是更新 gradle 版本:

android/gradle/wrapper/gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip

android/build.gradle

dependencies classpath 'com.android.tools.build:gradle:3.5.3' classpath 'com.google.gms:google-services:4.3.2'

并且在 android/build.gradle

buildscript 
    ext.kotlin_version = '1.3.61'

pubspec.yaml

firebase_core: ^0.4.2+1 
  cloud_firestore: ^0.12.11

希望对你有帮助。

【讨论】:

【参考方案4】:

我创建了另一个应用程序并替换了所有 .dart 文件和 pubspec.yaml。这是我发现的最快的方法

【讨论】:

【参考方案5】:

在 android/build.gradle 中仅使用此依赖项并删除任何其他依赖项

classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:3.2.1' 

并且在 pubspec.yaml 中只使用这些版本来避免 gradle 错误:

cloud_firestore: 0.7.3 
firebase_admob: 0.5.5
firebase_auth: 0.5.11
google_sign_in: 3.0.4
firebase_analytic: 1.0.1
firebase_core: 0.2.4 
firebase_database: 1.0.1 
firebase_dynamic_links: 0.02
firebase_messaging: 1.0.2
firebase_performance: 0.0.3
firebase_remote_config: 0.0.4
firebase_storage: 0.3.7

【讨论】:

【参考方案6】:

您是否尝试将 google maven 存储库添加到您的项目级 build.gradle 文件中?

buildscript 
repositories 
    google()
    jcenter()
    maven 
        url "https://maven.google.com"
    

buildscript 部分或 allprojects 部分。

【讨论】:

你试过降级你的依赖吗?也取出一些,直到你找出哪个给你错误 是的,这绝对有效,thxx :) 使用像 15.0.2 这样的统一版本会导致不正确的同步。我按照另一位朋友的建议使用了“+”,终于解决了问题 我怎样才能结束这个问题? @Aivi 这很好解释:link

以上是关于gradle 在 Android Studio 中使用颤振失败的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio中如何清理gradle缓存

android studio更新gradle在哪个目录下

如何在android studio中查看当前使用的gradle版本和gradle插件版本

如何在android studio中查看当前使用的gradle版本和gradle插件版本

如何在android studio中查看当前使用的gradle版本和gradle插件版本

如何在android studio中查看当前使用的gradle版本和gradle插件版本