Visual Studio Code 中的 build.gradle 错误(Flutter)

Posted

技术标签:

【中文标题】Visual Studio Code 中的 build.gradle 错误(Flutter)【英文标题】:Error on build.gradle In Visual Studio Code(Flutter) 【发布时间】:2020-01-16 17:39:32 【问题描述】:

我目前正在尝试为 Google Play 商店构建一个应用程序,并且需要制作一个签名并发布的 APK。我尝试更新两个 build.gradle 文件,但出现此错误:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/herb/Downloads/morningly/android/app/build.gradle' line: 30

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not find method android() for arguments [build_4k00465lacv9h9hnlvfrbb5oa$_run_closure2@7532b849] on project ':app' of type org.gradle.api.Project.

* 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.

这是我的 build.gradle 文件:

android build.gradle

buildscript 
    repositories 
        google()
        jcenter()
    

    dependencies 
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.google.gms:google-services:3.2.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

android/app 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'


def keystoreProperties = new Properties()
   def keystorePropertiesFile = rootProject.file('key.properties')
   if (keystorePropertiesFile.exists()) 
       keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
   

   android 
    compileSdkVersion 29
    buildToolsVersion "29.0.1"
    lintOptions 
        disable 'InvalidPackage'
    

    defaultConfig 
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.morningly"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    

    signingConfigs 
       release 
           keyAlias keystoreProperties['keyAlias']
           keyPassword keystoreProperties['keyPassword']
           storeFile file(keystoreProperties['storeFile'])
           storePassword keystoreProperties['storePassword']
       
   
   buildTypes 
        release 
            signingConfig signingConfigs.release
            minifyEnabled true
            useProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    


flutter 
    source '../..'


dependencies 
    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'

现在,我按照这篇文章 (https://flutter.dev/docs/deployment/android) 来帮助我完成这个过程。我确保我编辑了正确的 Gradle 文件,我什至检查了答案,但无济于事。请,如果您有任何意见,请告诉我。我非常想上传我的应用程序。

【问题讨论】:

您在 android/app build.gradle 文件的第 30 行上方缺少 apply plugin: 'com.android.application'。没有这个它不理解 android。也将 apply plugin: 'com.google.gms.google-services' 上移。 @Jon Goodwin 谢谢,这解决了 android 的问题,但现在 flutter() 出现了问题Could not find method flutter() for arguments [build_4k00465lacv9h9hnlvfrbb5oa$_run_closure3@2347bae0] on project ':app' of type org.gradle.api.Project 【参考方案1】:

你错过了:

 apply plugin: 'com.android.application'

就在您的 android/app build.gradle 文件的 line 30 上方。没有这个它不理解android移动

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

向上,也要保持整洁最好早点阅读,以后的东西可能依赖它。

【讨论】:

以上是关于Visual Studio Code 中的 build.gradle 错误(Flutter)的主要内容,如果未能解决你的问题,请参考以下文章

折叠 Visual Studio Code 中的所有方法 [重复]

Visual Studio Code 中的重复行

Visual Studio Code 中的高亮问题

Visual Studio Code 中的 Python 版本问题

如何从 Visual Studio Code 中的集成终端打开文件?

Visual Studio Code 中的远程调试