gpt4 book ai didi

android - Hello World项目中的Gradle抛出异常

转载 作者:行者123 更新时间:2023-12-03 05:06:08 24 4
gpt4 key购买 nike

我正在尝试将Firebase和google服务添加到我的flutter应用程序中,但是Gradle抛出了一个异常,没有任何真实的解释。

我尝试重新安装android studio和flutter,但是错误仍然发生。我还下载了google-services.json文件并将其放在app文件夹中。

我的项目级别gradle如下所示:

buildscript {
ext.kotlin_version = '1.2.71'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.google.gms:google-services:4.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

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
}

我的应用程序级别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 28

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.example.facial_app"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
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 {
implementation 'com.google.firebase:firebase-analytics:17.2.0'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
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'

产生的错误消息是:
* Error running Gradle:
ProcessException: Process "C:\Users\user\AndroidStudioProjects\facial_app\android\gradlew.bat" exited abnormally:

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\user\AndroidStudioProjects\facial_app\android\app\build.gradle' line: 24

* What went wrong:
A problem occurred evaluating project ':app'.
> ASCII

* 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 1s
Command: C:\Users\user\AndroidStudioProjects\facial_app\android\gradlew.bat app:properties

Finished with error: Please review your Gradle project setup in the android/ folder.

最佳答案

这是由于AndroidX的兼容性,
创建一个新项目,然后按照以下步骤操作:

  • 为了使您的项目与AndroidX兼容,您需要在gradle.properties文件中添加以下两行:
    android.useAndroidX = true
    android.enableJetifier = true
  • 确保在您的应用程序build.gradle文件中,将compileSdkVersion和targetSdkVersion都设置为 28
  • 从主应用程序build.gradle文件中删除所有带有注释“Remove”的行:
    android {
    compileSdkVersion 28

    lintOptions {
    disable 'InvalidPackage'
    }

    defaultConfig {
    applicationId "YOUR_APPLICATION_ID"
    minSdkVersion 16
    targetSdkVersion 28
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //Remove
    }

    buildTypes {
    release {
    signingConfig signingConfigs.debug
    }
    }
    }


    dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2' // Remove
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' // Remove
    }
  • 如果您尝试构建项目,则将收到与AndroidX核心库有关的错误,要解决此问题,请在项目级别build.gradle文件的子项目类别中添加代码:
    buildscript {
    repositories {
    google()
    jcenter()
    }

    dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
    }

    subprojects {
    project.configurations.all {
    resolutionStrategy.eachDependency { details ->
    if (details.requested.group == 'androidx.core'
    && !details.requested.name.contains('androidx') ) {
    details.useVersion "1.0.1"
    }
    }
    }
    }
    }

  • 注意1:在第4步中,请记住子项目类别应在从属类别以下,如果不存在,则复制并粘贴给出的代码。

    我希望尝试在Android Studio中执行上述步骤,保存所有文件并重新启动项目,添加Firebase依赖项以及文档 Documentation中告诉您所做的所有更改。会的。

    祝你好运!

    关于android - Hello World项目中的Gradle抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58321076/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com