Android Studio 未正确构建 Flutter 应用程序

Moh*_*med 6 android gradle flutter

我有一个正在开发的应用程序,当我打开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'
}
Run Code Online (Sandbox Code Playgroud)

控制台输出:

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

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

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

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

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

  • https://help.gradle.org获取更多帮助

BUILD FAILED in 1s Finished with error: Gradle task assembleDebug failed with exit code 1

logcat中也有一条消息 Please configure Android SDK 在此处输入图片说明 我寻找了解决方案,但没有找到解决方案

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

小智 10

解决方案之一是这样的,

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'
}
Run Code Online (Sandbox Code Playgroud)

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

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


小智 7

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

classpath 'com.android.tools.build:gradle:3.5.0'
Run Code Online (Sandbox Code Playgroud)