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

167

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.

Share:
167
RockingDice
Author by

RockingDice

Updated on December 11, 2022

Comments

  • RockingDice
    RockingDice over 1 year

    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.

  • RockingDice
    RockingDice almost 5 years
    I think it's clear to answer this question for my case, I'll find another way. But it really depends on the codes whether to do tree-shaking for other situations. It would be better if you can share more information about 'Dart tree-shaking is based on constant variables' part :)