Flutter AppCenter Gradle Plugin 4.0.0+ 构建问题

Posted

技术标签:

【中文标题】Flutter AppCenter Gradle Plugin 4.0.0+ 构建问题【英文标题】:Flutter AppCenter Gradle Plugin 4.0.0+ build issue 【发布时间】:2021-07-20 13:20:01 【问题描述】:

我正在尝试在 AppCenter.ms 中构建颤振应用程序,但我遇到了清理中间文件夹的 gradle 命令。

我已按照这些教程进行设置:

https://www.rocksolidknowledge.com/articles/using-appcenter-for-flutter-projects https://buildflutter.com/deploying-flutter-apps-via-appcenter/ https://medium.com/@subodhpushpak/flutter-continuous-build-deployment-using-appcenter-e105d00e6f9c

我添加了一个名为 appCenterproductFlavor 来容纳这个库:https://pub.dev/packages/flutter_appcenter_bundle

这是我的appcenter-post-clone.sh

#!/usr/bin/env bash
#Place this script in project/android/app/

cd ..

# fail if any command fails
set -e
# debug log
set -x

cd ..
git clone -b stable https://github.com/flutter/flutter.git
export PATH=`pwd`/flutter/bin:$PATH

flutter channel stable
flutter doctor

# prevent missing flutter.jar error
flutter precache `pwd`/flutter/bin/cache/artifacts/engine

# Set APP_CENTER_VERSION_MODIFIER if you want to modify the version name
# to x.y.z-$APP_CENTER_VERSION_MODIFIER
BUILD_NAME=$(grep -E '^version' pubspec.yaml | cut -d':' -f2 | tr -d '[:space:]')
if [ ! -z "$APPCENTER_BUILD_ID" ]
then
  BUILD_NAME=$BUILD_NAME.$APPCENTER_BUILD_ID
fi
if [ ! -z "$APP_CENTER_VERSION_MODIFIER" ]
then
  BUILD_NAME=$BUILD_NAME-$APP_CENTER_VERSION_MODIFIER
fi

flutter build apk --debug --flavor appCenter
flutter build apk --release --flavor appCenter --build-name=$BUILD_NAME -t lib/main.dart

mkdir -p android/app/build/outputs/apk/
mv build/app/outputs/flutter-apk/app-appcenter-release.apk $_

appcenter-post-clone.sh 脚本完成后运行 gradle 时出现错误:

+ flutter build apk --debug --flavor appCenter
Running "flutter pub get" in s...                                  11.2s

???? Building with sound null safety ????

Running Gradle task 'assembleAppCenterDebug'...                 
Gradle 6.7
...
Running Gradle task 'assembleAppCenterDebug'...                   193.4s
✓ Built build/app/outputs/flutter-apk/app-appcenter-debug.apk.

+ flutter build apk --release --flavor appCenter --build-name=1.42 -t lib/main.dart
Running "flutter pub get" in s...                                  15.7s

???? Building with sound null safety ????

Running Gradle task 'assembleAppCenterRelease'...               
Gradle 6.7
...
Running Gradle task 'assembleAppCenterRelease'...                 104.7s
✓ Built build/app/outputs/flutter-apk/app-appcenter-release.apk (25.9MB).
+ mkdir -p android/app/build/outputs/apk/
+ mv build/app/outputs/flutter-apk/app-appcenter-release.apk android/app/build/outputs/apk/
+ mv -v android/app/build/outputs/apk/app-appcenter-release.apk android/app/build/outputs/apk/app-release.apk
android/app/build/outputs/apk/app-appcenter-release.apk -> android/app/build/outputs/apk/app-release.apk

//SCRIPT HAS FINISHED
//RUNNING NORMAL GRADLE

Description  : Build using a Gradle wrapper script
Version      : 1.128.0
Author       : Microsoft Corporation
Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613720)
==============================================================================
(node:3364) Warning: Use Cipheriv for counter mode of aes-256-ctr
...
(node:3364) Warning: Use Cipheriv for counter mode of aes-256-ctr
SYSTEMVSSCONNECTION exists true
[command]/.../android/gradlew -DAPPCENTER_BUILD_VERSION=42 -DMOBILECENTER_BUILD_VERSION=42 -p android clean :app:assembleAppCenterRelease :app:lintAppCenterRelease
...
> Task :app:lintVitalAppCenterRelease FAILED
Resolved com.android.tools.lint:lint-gradle:27.1.0 in :app:lintClassPath 

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:lintVitalAppCenterRelease'.
> Could not resolve all artifacts for configuration ':app:appCenterDebugRuntimeClasspath'.
   > Failed to transform libs.jar to match attributes artifactType=processed-jar, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime.
      > Execution failed for JetifyTransform: /.../build/app/intermediates/flutter/appCenterDebug/libs.jar.
         > Transform's input file does not exist: /.../build/app/intermediates/flutter/appCenterDebug/libs.jar. (See https://issuetracker.google.com/issues/158753935)

问题:

Transform's input file does not exist: /.../intermediates/flutter/appCenterDebug/libs.jar

我遇到了这个 SO 帖子: Generate signed apk fails with build\app\intermediates\flutter\profile\libs.jar (The system cannot find the path specified) 指向https://github.com/flutter/flutter/issues/58247#issuecomment-636500680

“解决方案”是先运行flutter build apk --debug,然后运行flutter build apk --release

理论上,这将为appCenterDebugappCenterRelease 生成带有所需文件的intermediates 目录。

通过运行gradlew lintAppCenterDebug 在本地对其进行测试,然后运行gradlew lintAppCenterRelease

两个颤振命令都成功构建。

问题出在 gradle 开始运行时这一行:

[command]/.../android/gradlew -DAPPCENTER_BUILD_VERSION=42 -DMOBILECENTER_BUILD_VERSION=42 -p android clean :app:assembleAppCenterRelease :app:lintAppCenterRelease

它在命令中进行了清理,删除了intermediates 目录(所以这个/.../appCenterDebug/libs.jar 不再存在)并且只构建:app:...Release 版本。然后它继续并中断并给出input file does not exist 错误。

如何解决这个问题?

如何从 gradle 命令中删除 clean 部分? (我在任何地方都找不到)

    我已经在本地测试了这个确切的命令,但失败了。 通过运行两个 gradle lint 命令重新生成 intermediates 内容 运行相同的 gradle 命令但没有 clean 并且它可以工作。

在本地和 appcenter 中调试、发布构建工作正常(直到 gradle 运行并删除所需的文件)

附加信息


pubspec.yaml

environment:
  sdk: ">=2.12.0 <3.0.0"

gradle.properties

org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true

build.gradle

buildscript 
    ext.kotlin_version = '1.3.50'
    repositories 
        google()
        jcenter()
        mavenCentral()
    

    dependencies 
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    


allprojects 
    repositories 
        google()
        jcenter()
        mavenCentral()
    


rootProject.buildDir = '../build'
subprojects 
    project.buildDir = "$rootProject.buildDir/$project.name"

subprojects 
    project.evaluationDependsOn(':app')


task clean(type: Delete) 
    delete rootProject.buildDir

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 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 30

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

    signingConfigs 
        defaultKey 
            /* truncated */
        
    

    defaultConfig 
        applicationId "com.example.app"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    

    buildTypes 
        release 
            signingConfig = signingConfigs.defaultKey
        
    

    flavorDimensions "distribute"
    productFlavors 
        appCenter 
            dimension "distribute"
        
    


flutter 
    source '../..'


dependencies 
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

gradle/wrapper/gradle-wrapper.properties

#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

【问题讨论】:

【参考方案1】:

构建成功。

我删除了:

https://pub.dev/packages/flutter_appcenter_bundle 产品风味

我变了:

gradle 插件到旧版本&lt; 4.0.0

构建成功

只有 Gradle 插件 4.0.0+ appCenter 想要 intermediates/flutter/appCenterDebug/libs.jar 存在,但这个命令在颤振脚本运行后运行,删除它:

[command]/.../android/gradlew -DAPPCENTER_BUILD_VERSION=42 -DMOBILECENTER_BUILD_VERSION=42 -p android clean :app:assembleAppCenterRelease :app:lintAppCenterRelease

文件更改


app/appcenter-post-clone.sh

#!/usr/bin/env bash
#Place this script in project/android/app/

cd ..

# fail if any command fails
set -e
# debug log
set -x

cd ..
git clone -b beta https://github.com/flutter/flutter.git
export PATH=`pwd`/flutter/bin:$PATH

flutter channel stable
flutter doctor

# Set APP_CENTER_VERSION_MODIFIER if you want to modify the version name
# to x.y.z-$APP_CENTER_VERSION_MODIFIER
BUILD_NAME=$(grep -E '^version' pubspec.yaml | cut -d':' -f2 | tr -d '[:space:]')
if [ ! -z "$APPCENTER_BUILD_ID" ]
then
  BUILD_NAME=$BUILD_NAME.$APPCENTER_BUILD_ID
fi
if [ ! -z "$APP_CENTER_VERSION_MODIFIER" ]
then
  BUILD_NAME=$BUILD_NAME-$APP_CENTER_VERSION_MODIFIER
fi
./update-app-settings.sh

flutter build apk --release --build-name=$BUILD_NAME -t lib/main.dart

mkdir -p android/app/build/outputs/apk/
mv build/app/outputs/flutter-apk/app-release.apk $_

build.gradle

buildscript 
    ext.kotlin_version = '1.3.50'
    repositories 
        google()
        jcenter()
    

    dependencies 
        classpath 'com.android.tools.build:gradle:3.6.4'
        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

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'


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android 
    compileSdkVersion 30

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

    signingConfigs 
        defaultKey 
            /* truncated */
        
    

    defaultConfig 
        applicationId "com.example.app"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    

    buildTypes 
        release 
            signingConfig = signingConfigs.defaultKey
        
    


flutter 
    source '../..'


dependencies 
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

gradle/wrapper/gradle-wrapper.properties

#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip

【讨论】:

降级到这么旧的版本我们会失去什么?有什么缺点吗?

以上是关于Flutter AppCenter Gradle Plugin 4.0.0+ 构建问题的主要内容,如果未能解决你的问题,请参考以下文章

带有 Flutter 的 AppCenter(IOS 和 Android)

使用风味使用 MS AppCenter 构建 Flutter android APK 的问题

如何解决flutter gradle build error?C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 991

构建 appbundle 时出现颤振错误(脚本'/Users/****/Developer/flutter/packages/flutter_tools/gradle/flutter.gradle'

构建失败 - flutter.gradle

[Flutter] flutter项目一直卡在 Running Gradle task 'assembleDebug'...