【问题标题】:Android Studio is not building flutter app correctlyAndroid Studio 未正确构建颤振应用程序
【发布时间】:2020-07-20 08:57:29
【问题描述】:

我有一个正在开发的应用程序,当我打开 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 from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 28

    lintOptions {
        disable 'InvalidPackage'
    }

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

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

控制台输出:

在调试模式下在 IA 模拟器上的 AOSP 上启动 lib\main.dart... 正在运行 Gradle 任务 'assembleDebug'...

FAILURE:构建失败并出现异常。

  • 出了什么问题:任务 ':app:stripDebugDebugSymbols' 执行失败。

    没有与请求的版本 20.0.5594570 匹配的 NDK 版本。本地可用版本:21.0.6113669

  • 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。

  • 通过https://help.gradle.org获得更多帮助

BUILD FAILED in 1s Finished with error: Gradle task assembleDebug 退出代码 1 失败

logcat 中也有一条消息 Please configure Android SDK 我寻找解决方案,但没有找到解决方案

我只是需要帮助,不知道我是否违反了网站的法律,因为我不会说流利的英语

【问题讨论】:

    标签: android flutter gradle


    【解决方案1】:

    解决方案之一是这样,

    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 from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
    
    android {
        compileSdkVersion 28
    
    lintOptions {
        disable 'InvalidPackage'
    }
    
    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.shopapp"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    
    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
    
    ndkVersion "21.0.6113669" ///  <<---Add this, 
    
    
    
    }
    
    flutter {
        source '../..'
    }
    
    dependencies {
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    }
    

    原因是你安装了其他的NDK,而android studio有其他版本,其他的解决方法是,-> 去sdk工具,卸载NDK和Cmake。

    对不起我的英语,我不是母语人士。祝你好运!

    【讨论】:

    • 这对我不起作用。但是,我使用了系统所说的可用版本并且它有效! ndkVersion "20.0.5594570"
    • 我遇到了同样的问题,在/android/app/build.gradle中添加了ndkVersion的指定版本后,没有构建错误。我加了:ndkVersion "21.0.6113669"
    【解决方案2】:

    此问题是由于与最新的 android gradle 插件版本不兼容造成的。您可以尝试在android/build.gradle 中将其版本降级到 3.5.0:

    classpath 'com.android.tools.build:gradle:3.5.0'
    

    【讨论】:

    • 我创建了一个新应用程序,该应用程序创建了 classpath 'com.android.tools.build:gradle:4.1.1' 将其更改为 classpath 'com.android.tools.build:gradle:3.5.0' 工作
    • 请注意,用于编译的 gradle 版本会影响您应在此处注明的版本号developer.android.com/studio/releases/gradle-plugin
    【解决方案3】:

    修改 app/build.gradle 并添加 ndkVersion。我用过以下版本。你必须使用你的兼容版本

    【讨论】:

      【解决方案4】:

      尝试将 android/build.gradle 中的类路径更改为

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

      并确保您的 gradle/wrapper/gradle-wrapper.properties 具有

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

      或此处显示的版本https://developer.android.com/studio/releases/gradle-plugin

      【讨论】:

        【解决方案5】:
        1. 请检查安装在 Extensions inside settings 中的 flutter 和 dart 插件。
        2. 然后运行 ​​flutter clean。
        3. 运行颤振升级。
        4. 如果仍然无法正常工作,请将 Flutter SDK 路径添加到环境变量中。
        5. 我看到了服务错误。所以请验证您的 build.gradle 依赖项中的 com.google.services。 希望它有效。

        【讨论】:

          【解决方案6】:

          修改 app\build.gradle 文件如下: 在 android 块中添加这个

          ndkVersion "21.0.6113669"

              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 = '2'
          }
          
          def flutterVersionName = localProperties.getProperty('flutter.versionName')
          if (flutterVersionName == null) {
              flutterVersionName = '1.1'
          }
          
          def keystoreProperties = new Properties()
          def keystorePropertiesFile = rootProject.file('key.properties')
          if (keystorePropertiesFile.exists()) {
              keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
          }
          
          apply plugin: 'com.android.application'
          apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
          
          android {
              compileSdkVersion 28
              ndkVersion "21.0.6113669"
          
              lintOptions {
                  disable 'InvalidPackage'
              }
          
              defaultConfig {
                  applicationId "com.dawamisr.app"
                  minSdkVersion 19
                  targetSdkVersion 28
                  multiDexEnabled true
                  versionCode flutterVersionCode.toInteger()
                  versionName flutterVersionName
                  testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
              }
          
              signingConfigs {
                  release {
                      keyAlias keystoreProperties['keyAlias']
                      keyPassword keystoreProperties['keyPassword']
                      storeFile file(keystoreProperties['storeFile'])
                      storePassword keystoreProperties['storePassword']
                  }
              }
              buildTypes {
                  release {
                      signingConfig signingConfigs.release
                  }
              }
          }
          flutter {
              source '../..'
          }
          

          依赖{ testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' 实施 'com.google.firebase:firebase-core:17.2.1' 实施 'com.google.firebase:firebase-messaging:20.1.0' 实施“org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version” }

          应用插件:“com.google.gms.google-services” 应用插件:'kotlin-android'

          【讨论】:

            【解决方案7】:

            解决办法是重新安装Android Studio 考虑到从 android studio 正确安装(Java & All packages)

            【讨论】:

              猜你喜欢
              • 2021-02-18
              • 1970-01-01
              • 2019-12-08
              • 2021-01-05
              • 2021-03-01
              • 2020-05-11
              • 1970-01-01
              • 1970-01-01
              • 2019-04-03
              相关资源
              最近更新 更多