diff --git a/packages/flutter_tools/lib/src/build_info.dart b/packages/flutter_tools/lib/src/build_info.dart index 9fb09eec69f85179507cf059689429c1628f36a1..b5e72567af1708fc8c41cb5cca73203eadec9c10 100644 --- a/packages/flutter_tools/lib/src/build_info.dart +++ b/packages/flutter_tools/lib/src/build_info.dart @@ -364,6 +364,7 @@ class OhosBuildInfo { OhosArch.arm64_v8a, OhosArch.x86_64, ], + this.enableImpellerFlag = null, }); // The build info containing the mode and flavor. @@ -372,6 +373,8 @@ class OhosBuildInfo { /// The target platforms for the build. final Iterable targetArchs; + // enable impeller option, default is true + final bool? enableImpellerFlag; } diff --git a/packages/flutter_tools/lib/src/ohos/hvigor.dart b/packages/flutter_tools/lib/src/ohos/hvigor.dart index 76765f4b1593643018e0114239a7c46a065bdb99..7a365c6ca6475bf0db7ed647573a45510e2eeb13 100644 --- a/packages/flutter_tools/lib/src/ohos/hvigor.dart +++ b/packages/flutter_tools/lib/src/ohos/hvigor.dart @@ -51,6 +51,7 @@ const String APP_SO = 'libapp.so'; const String HAR_FILE_NAME = 'flutter.har'; const String BUILD_INFO_JSON_PATH = 'src/main/resources/base/profile/buildinfo.json5'; +const String BUILD_INFO_JSON_DES_PATH = 'src/main/resources/rawfile/buildinfo.json5'; final bool isWindows = globals.platform.isWindows; @@ -101,6 +102,36 @@ Future copyFlutterBuildInfoFile(OhosProject ohosProject) async { await sourceFile.delete(); } +Future setImpellerEnableFlag(OhosProject ohosProject, OhosBuildInfo ohosBuildInfo) async { + final String buildinfoFilePath = globals.fs.path.join(ohosProject.flutterModuleDirectory.path, BUILD_INFO_JSON_DES_PATH); + + final File file = globals.localFileSystem.file(buildinfoFilePath); + + if (!await file.exists()) { + throw Exception('Failed to find buildinfo.json5 file: $buildinfoFilePath'); + } + + final String content = await file.readAsString(); + + final Map json = jsonDecode(content) as Map; + + // find "enable_impeller" in json file + final List stringList = json['string'] as List; + final Map? enableImpellerItem = stringList.firstWhere( + (dynamic item) => (item as Map)['name'] == 'enable_impeller', + orElse: () => null, + ) as Map?; + + if (enableImpellerItem != null) { + enableImpellerItem['value'] = ohosBuildInfo.enableImpellerFlag?.toString(); + } + + final String updatedContent = const JsonEncoder.withIndent(' ').convert(json); + + // save setting + await file.writeAsString(updatedContent); +} + /// eg:entry/src/main/resources/rawfile String getProjectAssetsPath(String ohosRootPath, OhosProject ohosProject) { return globals.fs.path.join(ohosProject.flutterModuleDirectory.path, @@ -349,12 +380,12 @@ Future flutterAssemble(FlutterProject flutterProject, } /// 清理和拷贝flutter产物和资源 -void cleanAndCopyFlutterAsset( +Future cleanAndCopyFlutterAsset( OhosProject ohosProject, OhosBuildInfo ohosBuildInfo, Logger? logger, String ohosRootPath, - String output) { + String output) async { logger?.printTrace('copy flutter assets to project start'); // clean flutter assets final String desFlutterAssetsPath = @@ -367,7 +398,11 @@ void cleanAndCopyFlutterAsset( /// copy flutter assets copyFlutterAssets(globals.fs.path.join(output, FLUTTER_ASSETS_PATH), desFlutterAssetsPath, logger); - copyFlutterBuildInfoFile(ohosProject); + await copyFlutterBuildInfoFile(ohosProject); + + if (ohosBuildInfo.enableImpellerFlag != null) { + await setImpellerEnableFlag(ohosProject, ohosBuildInfo); + } final String desAppSoPath = getAppSoPath(ohosRootPath, ohosBuildInfo.targetArchs.first, ohosProject); @@ -542,7 +577,7 @@ class OhosHvigorBuilder implements OhosBuilder { final String output = await flutterAssemble(flutterProject, ohosBuildInfo, target); - cleanAndCopyFlutterAsset(ohosProject, ohosBuildInfo, _logger, ohosRootPath, output); + await cleanAndCopyFlutterAsset(ohosProject, ohosBuildInfo, _logger, ohosRootPath, output); cleanAndCopyFlutterRuntime(ohosProject, ohosBuildInfo, _logger, ohosRootPath, ohosBuildData); diff --git a/packages/flutter_tools/lib/src/ohos/ohos_device.dart b/packages/flutter_tools/lib/src/ohos/ohos_device.dart index 5ab5688595eddc5f6d5ef2613e77e8bc18474e6c..74d27cafe2c381343dcc67802d25a2d75849fc9d 100644 --- a/packages/flutter_tools/lib/src/ohos/ohos_device.dart +++ b/packages/flutter_tools/lib/src/ohos/ohos_device.dart @@ -289,6 +289,7 @@ class OhosDevice extends Device { ohosBuildInfo: OhosBuildInfo( debuggingOptions.buildInfo, targetArchs: [ohosArch], + enableImpellerFlag: debuggingOptions.enableImpeller.asBool, ), target: mainPath ?? 'lib/main.dart', );