Flutter应用在运行`flutter run`时构建失败。

huangapple go评论80阅读模式
英文:

Flutter App Build Failed while flutter run

问题

我是新手,开始学习开发Flutter应用。接手了一个Flutter项目,并尝试运行,但在终端命令中遇到了“构建失败异常”。

使用以下终端命令运行:

flutter run -t lib/main_prod.dart

终端中出现了以下错误:

在使用Android SDK为x86构建的设备上使用硬件渲染。如果您注意到图形伪影,
请考虑通过使用“--enable-software-rendering”启用软件渲染。
在调试模式下启动 Android SDK 为x86的 lib/main_prod.dart...
                                        
FAILURE: 构建失败,发生异常。                                    
                                        
* 出现位置:                                                          
构建文件'/Users/ashishsciit/Desktop/Ashish/onfees/edflylearnGit/android/app/build.gradle'的第 62 行
                                        
* 发生了什么:                                                        
在评估项目'app'时发生问题。                           
> java.net.URISyntaxException: 不合法的字符在不透明部分中的索引 2 处:C:\Work\edflylearn\edfly_learn_release_key.jks
                                        
* 尝试:                                                              
使用--stacktrace选项运行以获取堆栈跟踪。 使用--info或--debug选项运行以获取更多日志输出。 使用--scan 选项获取完整信息。
                                        
* 获取更多帮助 https://help.gradle.org                               
                                        
42秒内构建失败。                                                     
运行 Gradle 任务'assembleDebug'...                                  
运行 Gradle 任务'assembleDebug'... 已完成                       45.0秒
异常:Gradle 任务 assembleDebug 以退出代码 1 失败

Android应用层级的 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。请在 local.properties 文件中使用 flutter.sdk 定义其位置。")
}

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"

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

android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "easy.tech.innovations.edflylearn"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        debug {
            debuggable true
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        release {
            minifyEnabled true
            shrinkResources true
            debuggable false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-messaging:20.2.0'
}

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

在上述代码中,构建文件'/Users/ashishsciit/Desktop/Ashish/onfees/edflylearnGit/android/app/build.gradle'的第 62 行指向:

storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null

尝试过使用 iOS 和 Android 模拟器以及实体设备,但没有成功。现在该怎么办?
为任何英语语法错误感到抱歉。

英文:

I am new to flutter app development. Got handover of the flutter project and tried to run, but getting Build Failed Exception by terminal command.

flutter run -t lib/main_prod.dart

Got these errors in terminal

Using hardware rendering with device Android SDK built for x86. If you notice graphics artifacts,
consider enabling software rendering with "--enable-software-rendering".
Launching lib/main_prod.dart on Android SDK built for x86 in debug mode...
FAILURE: Build failed with an exception.                                
* Where:                                                                
Build file '/Users/ashishsciit/Desktop/Ashish/onfees/edflylearnGit/android/app/build.gradle' line: 62
* What went wrong:                                                      
A problem occurred evaluating project ':app'.                           
> java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Work\edflylearn\edfly_learn_release_key.jks
* 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 42s                                                     
Running Gradle task 'assembleDebug'...                                  
Running Gradle task 'assembleDebug'... Done                        45.0s
Exception: Gradle task assembleDebug failed with exit code 1

Android app level build.gradle code

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"
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
android {
compileSdkVersion 29
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
applicationId "easy.tech.innovations.edflylearn"
minSdkVersion 21
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
debug {
debuggable true
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
shrinkResources true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.firebase:firebase-messaging:20.2.0'
}
apply plugin: 'com.google.gms.google-services'

In above code Build file '/Users/ashishsciit/Desktop/Ashish/onfees/edflylearnGit/android/app/build.gradle' line: 62 points to

storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null

Tried with both iOS and Android emulators as well as physical devices but no success
What should i do now ?
Sorry for any english grammatical mistakes

答案1

得分: 1

这是一个已配置了 Firebase 的项目。

这个错误是由于一些 Firebase 配置错误引起的。错误是由于生成的密钥库造成的。

您应该查看您的 Firebase 控制台项目和您的应用程序的配置,并通过以下命令重新生成 SHA 指纹:

  • keytool -genkey -v -keystore c:\Users\USER_NAME\key.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias key
  • 然后运行 keytool -list -v -keystore debug.keystore -alias androiddebugkey 来查看您的密钥。
  • 将这些密钥复制粘贴到 Firebase 项目应用中。

第二个解决方案是:

  • 查看 android/key.properties 中是否存在 key.properties 文件。
  • 通过在命令提示符中运行以下命令检查生成的密钥是否与该文件中的密钥匹配。
  • 运行 keytool -list -v -keystore debug.keystore -alias androiddebugkey 来查看您的密钥。
  • 将相同的密钥复制到您的 Firebase 项目中,并下载“google-services.json”文件,将其粘贴到项目中的应用文件夹中,路径为“C:\Users\USERNAME\AndroidStudioProjects\PROJECT_NAME\android\app\google-services.json”。
  • 运行 flutter clean
  • 通过运行 pub get 来再次获取所有依赖项。

您的问题很可能会得到解决。

英文:

It is Firebase configured project.

This error is because of some configuration error of firebase firebase. The error is due to the generated key store.

You should see the configuration of your firebase console project and your app and regenerate the SHA fingerprints by running:

  • keytool -genkey -v -keystore c:\Users\USER_NAME\key.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias key
  • Then run keytool -list -v -keystore debug.keystore -alias androiddebugkey to view your keys.
  • Copy and paste those keys in firebase project app.

And the second solution would be

  • See if key.properties file is present in android/key.properties.
  • Check if the generated key is matching with key mentioned in this file by running below command in command prompt.
  • Run keytool -list -v -keystore debug.keystore -alias androiddebugkey to view your keys.
  • Copy the same key to your firebase project and download "google-services.json" file and paste it your app folder in project at "C:\Users\USERNAME\AndroidStudioProjects\PROJECT_NAME\android\app\google-services.json".
  • run flutter clean
  • Get all the dependencies again by running pub get.

Your problem will be solved most probably.

huangapple
  • 本文由 发表于 2020年9月19日 02:58:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63961393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定