【问题标题】:Will the dart codes for iOS be removed when compiling for Android?为 Android 编译时,iOS 的 dart 代码会被删除吗?
【发布时间】:2019-05-25 15:20:17
【问题描述】:

我正在使用 Flutter 为 iOS 和 Android 平台编写应用程序。有些功能不一样。

例如:

if (Platform.isIOS) {
    int onlyForiOS = 10;
    onlyForiOS++;
    print("$onlyForiOS");
}
else if (Platform.isAndroid){
    int onlyForAndroid = 20;
    onlyForAndroid++;
    print("$onlyForAndroid");
}

当我为Android平台构建时,iOS的代码会被编译成二进制文件吗?或者它们只是为了优化而被删除? 出于安全原因,我不希望任何适用于 iOS 的代码出现在 Android 二进制文件中。

【问题讨论】:

  • 我并不声称自己是这方面的专家,但我认为您编译的 Android 代码还将包含 if 部分(适用于 iOS)
  • @CopsOnRoad 那么这是一个假设吗?我真的期待一个可靠的答案,因为如果你说的是真的,我必须采取替代计划来编码。
  • @RockingDice 这是我的假设,Remi 也提到了这一点。
  • @jamesdlin 感谢您的链接。他们真的很有帮助。

标签: android flutter dart compilation


【解决方案1】:

这取决于您正在评估的表达式。

Dart tree-shaking 基于常量变量。因此,以下内容将被摇树:

const foo = false;
if (foo) {
  // will be removed on release builds
}

但是这个例子不会:

final foo = false;
if (foo) {
  // foo is not a const, therefore this if is not tree-shaked
}

现在,如果我们查看 Platform.isAndroid 的实现,我们可以看到它不是一个常量,而是一个 getter。

因此我们可以推断if (Platform.isAndroid) 不会被摇树。

【讨论】:

  • 我认为对于我的情况回答这个问题很清楚,我会找到另一种方法。但这真的取决于代码是否对其他情况进行 tree-shaking。如果您可以分享有关“Dart tree-shaking is based on constant variables”部分的更多信息会更好:)
猜你喜欢
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 2018-03-24
  • 1970-01-01
相关资源
最近更新 更多