Flutter-支持多种构建类型的构建



我在我的android项目中创建了3个构建类型:

buildTypes {
release {
signingConfig signingConfigs.release
resValue "string", "app_name", "AppName"
}
debug {
applicationIdSuffix ".debug"
resValue "string", "app_name", "AppName - DEV"
}
staging {
initWith release
matchingFallbacks = ['release']
applicationIdSuffix ".debug"
}
}

我想组装我的暂存构建类型,所以我运行了";颤振构建apk-staging";,但flutter找不到这样的选择:

Could not find an option named "staging".

看来Flutter构建apk命令">颤振建立apk";仅支持3种类型:调试、配置文件、发布;在使用自定义buildType的情况下,没有对此的支持。

是否可以构建自定义BuildType的apk?

我最终使用了产品Flavors:

flavorDimensions 'app'

productFlavors {
dev {
dimension "app"
resValue "string", "app_name", "AppName - DEV"
applicationIdSuffix ".debug"
}
staging {
dimension "app"
resValue "string", "app_name", "AppName - DEV"
applicationIdSuffix ".debug"
}
prod {
dimension "app"
resValue "string", "app_name", "AppName"
}
}
一个解决方案是使用Android自己的Gradle构建任务,而不是使用Flutter命令。
  1. cd android/
  2. ./gradlew app:build

然后,在成功构建之后,您应该能够在build/app/outputs/apk/中为每个buildType找到一个apk。

我有这个问题的解决方案,打开$flutterRoot/packages/flutter_tools/gradle/flutter.gradle找到project.android.buildTypes

将您的构建类型添加到此块,然后返回到您的android项目,invalidata-cache/restart。

我的问题是启动一个构建类型自定义。

这个代码(参见"预印本"(对我有效(Flutter 3.0.4(:命令行:flutter run --flavor preprod --release

signingConfigs {
debug {
storeFile file('../keystores/debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
releasetest {
storeFile file('../keystores/releaseTest-keystore.jks')
storePassword 'releaseTest'
keyAlias 'releaseTest'
keyPassword 'releaseTest'
}
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "flavor-type"
productFlavors {
dev {
dimension "flavor-type"
applicationIdSuffix ".dev"
}
preprod {
dimension "flavor-type"
buildTypes.release.signingConfig signingConfigs.releasetest
}
prod {
dimension "flavor-type"
}
}

如果您实际上不需要将其作为构建类型,您可以使用Flutter的支持在Gradle中定义属性,如下所示:

flutter run --release --android-project-arg staging=1

然后在Gradle中,你可以检测到这个属性,并根据它更改内容:

buildTypes {
release {
signingConfig signingConfigs.release
resValue "string", "app_name", "AppName"
if (project.hasProperty("staging")) {
matchingFallbacks = ["release"]
applicationIdSuffix ".debug"
}
}
debug {
applicationIdSuffix ".debug"
resValue "string", "app_name", "AppName - DEV"
}
}

将这个参数添加到您的启动配置中也很容易,例如在VS代码中,只需将其添加到.vscode/launch.json:

"args": ["--android-project-arg", "staging=1"]

我有一个类似的问题,我试图用构建类型或风格来解决,但Flutter目前对此的支持非常有限。我最终使用了这样一处房产。

最新更新