0%

flutter混编打包报错——无效buildMode

混编打包遇到环境不在有效范围内,爬坑记录

1
2
3
4
5
6
7
8
ERROR: Unknown FLUTTER_BUILD_MODE: distribution.
Valid values are 'Debug', 'Profile', or 'Release' (case insensitive).
This is controlled by the FLUTTER_BUILD_MODE environment variable.
If that is not set, the CONFIGURATION environment variable is used.

You can fix this by either adding an appropriately named build
configuration, or adding an appropriate value for FLUTTER_BUILD_MODE to the
.xcconfig file for the current build configuration (Distribution).

方案一(不推荐)

根据提示的路径找到对应的flutter_export_environment.sh文件和Generated.xcconfig文件,打开添加以下代码:

export "FLUTTER_BUILD_MODE=release"

注:这两个文件是自动生成的,每次生成会覆盖,所以这个方案算是临时方案,协同工作很不方便,极其不推荐

方案二

打开flutter的package地址:xxx/Library/Developer/flutter/packages/flutter_tools/bin/xcode_backend.dart

我们可以看到在选择模式的时候,先查找set中FLUTTER_BUILD_MODE配置,如果没有取CONFIGURATION,可以直接修改package代码,添加判断如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
String parseFlutterBuildMode() {
// Use FLUTTER_BUILD_MODE if it's set, otherwise use the Xcode build configuration name
// This means that if someone wants to use an Xcode build config other than Debug/Profile/Release,
// they _must_ set FLUTTER_BUILD_MODE so we know what type of artifact to build.
final String? buildMode =
(environment['FLUTTER_BUILD_MODE'] ?? environment['CONFIGURATION'])
?.toLowerCase();

if (buildMode != null) {
// 修改的地方
if (buildMode.contains('release') || buildMode.contains('xxx')) {
return 'release';
}
if (buildMode.contains('profile')) {
return 'profile';
}
if (buildMode.contains('debug')) {
return 'debug';
}
}
echoError(
'========================================================================');
echoError('ERROR: Unknown FLUTTER_BUILD_MODE: $buildMode.');
echoError(
"Valid values are 'Debug', 'Profile', or 'Release' (case insensitive).");
echoError(
'This is controlled by the FLUTTER_BUILD_MODE environment variable.');
echoError(
'If that is not set, the CONFIGURATION environment variable is used.');
echoError('');
echoError('You can fix this by either adding an appropriately named build');
echoError(
'configuration, or adding an appropriate value for FLUTTER_BUILD_MODE to the');
echoError(
'.xcconfig file for the current build configuration (${environment['CONFIGURATION']}).');
echoError(
'========================================================================');
exitApp(-1);
}

方案三(推荐★★★)

在方案二实施过程中,我们发现这个选择mode过程,会优先选择FLUTTER_BUILD_MODE,这个配置默认是没有的,方案一强制性设置为release会导致其他环境运行出错。
我们可以添加自定义配置项FLUTTER_BUILD_MODE,将你的环境指定对应的mode