为Android编译时,是否会删除iOS的Dart代码?

时间:2019-05-25 15:20:17

标签: android flutter dart compilation

我正在使用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二进制文件中。

1 个答案:

答案 0 :(得分:2)

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

Dart摇树是基于常量的。因此,以下内容将摇摇欲坠:

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的实现,可以看到它不是不是常数,而是一个吸气剂。

因此,我们可以推断if (Platform.isAndroid)不会摇摇欲坠。

相关问题