Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):
Describe the bug
I am not sure if this issue is related to the nativescript-cli or maybe more to webpack, but let my try to explain it here first.
When importing _isAndroid_ from "@nativescript/core" within the main.ts (app.ts) file, the app will not build on Android. On iOS, everything works as expected.
ERROR in ./main.ts 41:30
Module parse failed: Identifier 'isAndroid' has already been declared (41:30)
File was processed with these loaders:
- ../node_modules/@nativescript/webpack/helpers/android-app-components-loader.js
- ../node_modules/@nativescript/webpack/bundle-config-loader.js
- ../node_modules/@nativescript/webpack/hmr/hot-loader.js
- ../node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
|
| import "@nativescript/core/bundle-entry-points";import { Application, isAndroid, isIOS } from "@nativescript/core";| Application.on(Application.launchEvent, (args) => {
| if (isAndroid) {
...
When just importing _isAndroid_ from nativescript/core without using it, the build works fine and the app start correctly. But as soon as I try to use it anywhere (even just console.log(isAndroid ), the build fails on Android.
To Reproduce
Create a new nativescript app using the Hello World template (or use the sample app provided below) and add the following code to the main.ts:
import {Application, isAndroid, isIOS, LaunchEventData} from "@nativescript/core";
Application.on(Application.launchEvent, (args: LaunchEventData) => {
if(isAndroid) {
console.log("android app launched");
} else if(isIOS) {
console.log("ios launched");
}
});
Application.run({ moduleName: "app-root" });
Expected behavior
The build should work correctly for Android as well.
Sample project
ns7_issue_app_ts.zip
I also tried to import _isAndroid_ from @nativescript/core/platform (not from @nativescript/core), but the build fails with the same error message.
When importing the whole platform-module like this:
import * as platform from "@nativescript/core/platform";
Application.on(Application.launchEvent, (args: LaunchEventData) => {
if(platform.isAndroid) {
console.log("#################### ANDROID APP LAUNCHED");
} else if(platform.isIOS) {
console.log("#################### IOS APP LAUNCHED");
}
});
... then the build works as expected and the app starts correctly on Android.
same here. You can fix it for android by removing the import and saying declare const isAndroid: boolean, and then it'll work, but it'll fail for iOS.
However, you can use isIOS and it'll work fine for both platforms!
something new?
Same problem here, I resolved it:
import { Application } from '@nativescript/core';
const _isAndroid = !!Application.android;
const _isIOS = !!Application.ios;
works on both platforms, on macOS and windows
Most helpful comment
same here. You can fix it for android by removing the import and saying
declare const isAndroid: boolean, and then it'll work, but it'll fail for iOS.However, you can use isIOS and it'll work fine for both platforms!