簡體   English   中英

在為Android編譯時,是否會刪除iOS的dart代碼?

[英]Will the dart codes for iOS be removed when compiling for Android?

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

這取決於您正在評估的表達式。

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的實現,我們可以看到它不是一個常數,而是一個getter。

因此,我們可以推斷出, if (Platform.isAndroid)不會被樹木化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM