You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加--release标志后Patrol无法运行测试的问题求助

Patrol集成测试Release模式Gradle报错:找不到assembleReleaseAndroidTest任务

Debug模式下运行Patrol集成测试完全正常,但添加--release标志执行测试时,Gradle抛出任务不存在的错误,以下是详细信息及解决方案。


Debug模式运行情况

执行命令

# 自定义Makefile命令
patrol test --target integration_test/$(test_path)  --dart-define="MODE=test" --dart-define="ENV=local" --dart-define="SUPABASE_ADMIN_TEST_PASSWORD=$(SUPABASE_ADMIN_LOCAL_TEST_PASSWORD)" --verbose -d emulator-5554

成功日志

...
        : BUILD SUCCESSFUL in 7s
        : 327 actionable tasks: 6 executed, 321 up-to-date
✓ Completed executing apk with entrypoint test_bundle.dart on emulator-5554 (8.1s)

Release模式报错情况

执行命令(添加--release)

patrol test --release --target integration_test/$(test_path)  --dart-define="MODE=test" --dart-define="ENV=local" --dart-define="SUPABASE_ADMIN_TEST_PASSWORD=$(SUPABASE_ADMIN_LOCAL_TEST_PASSWORD)" --verbose -d emulator-5554

Gradle错误日志

FAILURE: Build failed with an exception.

* What went wrong:
Cannot locate tasks that match ':app:assembleReleaseAndroidTest' as task 'assembleReleaseAndroidTest' not found in project ':app'.

* Try:
> Run gradlew tasks to get a list of available tasks.
> 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

相关配置文件

app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

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 {
    namespace "net.xxx.xxx.xxx"
    compileSdk flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {
        applicationId "net.xxx.xxxx.xx"
        minSdkVersion 21
        multiDexEnabled true
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "pl.leancode.patrol.PatrolJUnitRunner"
        testInstrumentationRunnerArguments clearPackageData: "true"
    }

    testOptions {
        execution "ANDROIDX_TEST_ORCHESTRATOR"
    }

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

flutter {
    source '../..'
}

dependencies {
    androidTestUtil "androidx.test:orchestrator:1.4.2"
    implementation 'com.android.support:multidex:1.0.3'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}

pubspec.yaml

name: xxxx_zzzz
description: xxxx xxxx
publish_to: 'none'

version: 1.1.0+01

environment:
  sdk: '>=3.1.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  supabase_flutter: ^1.7.0
  flutter_dotenv: ^5.0.2
  table_calendar: ^3.0.0
  syncfusion_flutter_pdfviewer: ^22.2.12
  flutter_launcher_icons: ^0.13.1
  firebase_core: ^2.10.0
  firebase_messaging: ^14.4.1
  mocktail: ^1.0.0
  week_of_year: ^2.0.0
  supabase_auth_ui: ^0.2.1
  dart_dotenv: ^1.0.1
  firebase_crashlytics: ^3.3.5
  event_bus: ^2.0.0
  url_launcher: ^6.1.14
  package_info: ^2.0.2
  flutter_local_notifications: ^15.1.1
  jwt_decoder: ^2.0.1
  pub_semver: ^2.1.4
  flutter_markdown: ^0.6.18+2
  timezone: ^0.9.2
  infinite_scroll_pagination: ^4.0.0
  intl: ^0.18.1

dev_dependencies:
  patrol: ^3.6.1
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter
  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true
  assets:
    - assets/.local.env
    - assets/.test.env
    - assets/.prod.env
    - assets/.staging.env
flutter_launcher_icons:
  android: true
  ios: true
  image_path_android: "assets/images/logo_android.png"
  adaptive_icon_background: "#ffffff"
  adaptive_icon_foreground: "assets/images/logo_android.png"
  image_path_ios: "assets/images/logo_ios.png"

patrol:
  app_name: XXX xxxx
  android:
    package_name: net.xxx.xxxx
  ios:
    bundle_id: net.xxx.xxx

环境检查结果

flutter doctor

$ flutter doctor                                                                   130 
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.5, on macOS 14.0 23A344 darwin-arm64, locale en-ES)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.6)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

patrol doctor

$ patrol doctor                                                                    130 
Patrol doctor:
Patrol CLI version: 2.7.0
Flutter command: flutter 
  Flutter 3.19.5 • channel stable
Android: 
• Program adb found in /Users/zzz/Library/Android/sdk/platform-tools/adb
• Env var $ANDROID_HOME set to /Users/zzz/Library/Android/sdk
iOS / macOS: 
• Program xcodebuild found in /usr/bin/xcodebuild
• Program ideviceinstaller found in /opt/homebrew/bin/ideviceinstaller

解决方案

问题根源是Android默认不为Release构建类型自动创建Android Test变体,需手动在app/build.gradle中启用Release模式的测试支持:

  1. 修改buildTypes块,为Release构建类型添加测试配置:
buildTypes {
    release {
        signingConfig signingConfigs.release
        // 启用Release模式的测试变体
        testBuildType "release"
        // 可选:若测试依赖Debug模式的配置,添加fallback
        matchingFallbacks = ['debug']
    }
}
  1. 或在android根块中全局指定测试构建类型:
android {
    // ...其他配置
    testBuildType "release"
}

保存配置后重新运行Release模式的测试命令,Gradle会自动生成assembleReleaseAndroidTest任务。若仍有问题,执行./gradlew tasks确认任务是否生成。

内容的提问来源于stack exchange,提问作者Bertuz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 16:05:55