gradle 在 Android Studio 中使用 Flutter 失败



我是Android Studio和Flutter的新手。我最近想尝试Flutter,因此在Android Studio中安装了该插件。我的 Flutter 代码没有错误,但 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'

模块级构建分级

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

我尝试包含一些依赖项,例如播放服务版本和Firebase分析版本,但是此错误不会消失,并且属性和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

当我打开应用级构建分级时 图像颤振(它说它无法解析属性()和FileNotFoundException() )

但是,当我单击"在 Android Studio 中打开以进行编辑"图像 androidStudio 时,同一组错误得到解决并且 gradle 同步成功

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

您是否尝试过将Google Maven存储库添加到项目级别的build.gradle文件中?

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

"buildscript"部分或"allprojects"部分。

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

in 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

我希望它能帮助你。

在 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

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

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

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

  1. 如果可能,请增加项目>>android>>app>>build.gradle文件中的最低sdk版本 2.然后在project>>android>>app>>build.gradle中添加以下行 依赖{

实现 'androidx.multidex:multidex:2.0.1'

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

最新更新