NativeScript / nativescript-dev-webpack

A package to help with webpacking NativeScript apps.
Apache License 2.0
97 stars 49 forks source link

Nativescript 4.1.0 using custom main activity with webpack #559

Closed tsonevn closed 6 years ago

tsonevn commented 6 years ago

From @RadouaneRoufid on June 7, 2018 21:11

Did you verify this is a real problem by searching the NativeScript Forum and the other open issues in this repo?

Tell us about the problem

I'm using a custom MainActivity declared as below in my nativescript project in a file named MainActivity.js. I'm importing this file in my vendor-platform.android.ts and declare this activity as my MainActivity in AndroidManifest.xml as <activity android:name="org.bricool.MainActivity"....

I updated recently my project from tns 4.0.0 to tns 4.1.0 and nativescript-dev-webpack from 0.10.1 to 0.12.0. When I start the ns app in dev mode (tns run android). The app works fine, but when I build a release bundled, aoted, uglified and snapshoted, the build succeed but tns does not find the MainActivity on start.

@JavaProxy('org.bricool.MainActivity')
export class Activity extends android.support.v7.app.AppCompatActivity {
...
}

I solved this problem in the previous version of nativescript by doing the following in the webpack.config.js

entry: {
            bundle: aot ?
                `./${nsWebpack.getAotEntryModule(appFullPath)}` :
                `./${nsWebpack.getEntryModule(appFullPath)}`,
            vendor: "./vendor",
            [mainSheet]: `./${mainSheet}`,
            MainActivity: "./MainActivity"
        },

I tried the following with the last version of dev-webpack but it does not work.

entry: {
            bundle: entryPath,
MainActivity: "./MainActivity"
        },

Which platform(s) does your issue occur on?

Android

Please provide the following version numbers that your issue occurs with:

Please tell us how to recreate the issue in as much detail as possible.

1- Create a new ns project 2- Create a custom MainActivity 3- build the app with : tns build android --env.profile=mprod --bundle --env.aot --env.uglify --env.snapshot

Copied from original issue: NativeScript/NativeScript#5928

vchimev commented 6 years ago

@RoyiNamir, I updated the appComponents path to activity in order to get the module found also on Windows.

@jibon57, yes, extending Activity works fine, but there is a problem when extending Application which I'm going to look into next.

jibon57 commented 6 years ago

Thanks @vchimev . Any work around so far? I can't use --env.snapshot too :(

RadouaneRoufid commented 6 years ago

I have this error when trying to bundle with --env.snapshot


Warning: there already is an extend called org.myApp.MainActivity.
Warning: The static binding generator will generate extend from:vendor.js implementation
Exception in thread "main" java.io.IOException: File already exists. This may lead to undesired behavior.
Please change the name of one of the extended classes.
File:/home/ruser/IdeaProjects/deepan/deepan-parent/deepan-front/src/gui/nativescript/platforms/android/app/src/main/java/org/myApp/MainActivity.java Class: org.myApp.MainActivity
        at org.nativescript.staticbindinggenerator.Generator.writeBindings(Generator.java:78)
        at org.nativescript.staticbindinggenerator.Main.main(Main.java:48)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:runSbg'.
> Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1

Any solution to this ? #568

vchimev commented 6 years ago

Hey Guys,

I'm closing this issue as extending Activity works fine.

The problem with extending Application is going to be addressed and commented here. CC: @jibon57

vchimev commented 6 years ago

Hey Guys,

Let me just follow up that adding a custom android app component to the array on the top of the webpack.config.js file should be resolved to an absolute path, i.e. resolve(__dirname, "app/activity.android.ts") in order to be included in the common vendor.js chunk. Adding it by a relative path is only a workaround for this issue on Windows, which fix is on a pull request.

RoyiNamir commented 6 years ago

@vchimev , just for full closure summarize : all the problems:

And the right way is to use abosolute path like :

resolve(__dirname, "app/activity.android.ts")

Will be in resolved in 0.14.2?

Or does some are resolved in 0.14 already ?

vchimev commented 6 years ago

Hey @RoyiNamir,

the issue that absolute paths in appComponents aren't resolved properly on Windows will be addressed in 0.14.2.

However, absolute paths work fine on macOS/Linux and also extending activity and application should work fine with the configurations described in this comment.