RockingDice
RockingDice

Reputation: 2120

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

I'm using flutter to write an app for both iOS and Android platforms. Some functions are not the same.

For example:

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

When I build for the Android platform, will the codes for iOS be compiled into the binary file? Or they are just removed for optimization? For security reason, I don't want any of the codes for iOS appeared in the Android binary file.

Upvotes: 5

Views: 234

Answers (1)

Rémi Rousselet
Rémi Rousselet

Reputation: 277537

This depends on what expression you are evaluating.

Dart tree-shaking is based on constant variables. As such, the following will be tree-shaked:

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

But this example won't:

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

Now if we look at the implementation of Platform.isAndroid, we can see that it is not a constant, but instead a getter.

Therefore we can deduce that if (Platform.isAndroid) won't be tree-shaked.

Upvotes: 4

Related Questions