Platform specific variable for AOT


#1

Is there an AOT variable available for use to detect iOS / Android platform?

Using app.ios / app.android from application module, or using isIOS / isAndroid from platform module fails during AOT compile due to:
'isAndroid is not initialized at tns-core-modules/platform/platform.ts(11,12).

The value is required to be initialized at compile time since it is being used to setup API environment in a static readonly field of a Config class. The iOS / Android use different domain names for local debug, i.e. https://10.0.2.2 vs https://localhost.


#2

First, I’m using application.android / .ios or isIOS / isAndroid in may places of my app and still AOT complies as expected. Not sure where / how exactly you are using it in your app, so unsure why it fails.

But if you want to detect the platform at compile time, checkout this line in webpack.config.js

You can use that variable and define any platform specific configurations with define plugin which can be later used in app at run time.


#3

Issue with using env style variables is inability to pass them in dev builds (not using webpack).
I don’t think the -env.foo flags are sent downstream by a tns run (non-bundle) version.


#4

FWIW, here is a snippet of my Config class:

export class Config {

  public static readonly ENV_CONFIG: EnvConfig = {
    API: (!global.ENV_PROD) ? 
           (isAndroid ? 'https://10.0.2.2:1234/' : 'https://localhost:1234/') : 
           'https://foo.bar/',
}

#5

Yes, indeed. But a work around is to have default values in your code. I do something like this, where ENV_NAME is set by WebPack. During dev build it should always take development configuration as there won’t be a value set. You may think of something similar.

   switch (global.ENV_NAME) {
        case "production":
            return ProdConfig;
        case "development":
        default:
            return AppConfig;
    }

#6

And webpack is setting global.ENV_PROD to prod


#7

Still looking for an answer for a compile-time variable to determine platform…