Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 650
RockingDice Avatar asked May 25 '19 15:05

RockingDice


1 Answers

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.

like image 178
Rémi Rousselet Avatar answered Nov 15 '22 05:11

Rémi Rousselet