react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Android] How to change index.android.js entry?

Open holyxiaoxin opened this issue 11 years ago • 15 comments

How to change index.android.js entry? Thanks.

holyxiaoxin avatar Oct 15 '15 13:10 holyxiaoxin

Can you provide some more context on the issues you're having? Did you manage to run the sample app?

javache avatar Oct 15 '15 16:10 javache

I want to change where index is being bundled. Right now the hello world app builds from index.android.js. How do I get it to build if I renamed index.android.js?

holyxiaoxin avatar Oct 15 '15 16:10 holyxiaoxin

git grep index.android will show you what you need to change.

ide avatar Oct 15 '15 21:10 ide

Got it! Thanks. (:

holyxiaoxin avatar Oct 16 '15 00:10 holyxiaoxin

I know this is closed, but what if we want to change the path of the entry file?

old: /index.android.js new: /src/index.android.js

jayfunk avatar Oct 24 '15 16:10 jayfunk

Same thing -- the references to index.android are paths, not juts file names so grepping should point you in the right direction.

ide avatar Oct 24 '15 22:10 ide

I need to change path of index.android.js, By default it access the file from root of project created (created by react native), I want to put it in asset folder of android structure.

shivangbtech avatar Mar 14 '16 05:03 shivangbtech

@ide I tried to change all the variables shown in the git grep index.android, but it's still not working.

Here's the result.

git grep index.android
android/app/build.gradle: *   bundleAssetName: "index.android.bundle",
android/app/build.gradle: *   entryFile: "index.android.js",
android/app/react.gradle:def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
android/app/react.gradle:def entryFile = config.entryFile ?: "index.android.js"
index.android.js:          To get started, edit index.android.js

I tried renaming the file and the strings to indexx.android.js, but the npm start is still looking for index.android.js.

Perhaps you can provide the patch file to show me how to change the entry file, let's say to src/index.android.js?

petrabarus avatar Apr 04 '16 22:04 petrabarus

You just need to override getJSMainModuleName() method in your react activity and return the path as below:

@Override protected String getJSMainModuleName() { return "android/demo_app/index.android"; }

here android is project folder, demo_app is directory in which all js files are situated. You can give any path like this.

shivangbtech avatar Apr 05 '16 04:04 shivangbtech

@shivangbtech:

Somewhat it's not working. I uploaded a fresh ReactNative app here https://github.com/petrabarus/TestReactNative/commit/4579d374f32a0efe21547749adb8b4a47b540c18

I tried this

$npm start
> [email protected] start /home/petra/Projects/TestReactNative
> node node_modules/react-native/local-cli/cli.js start

 ┌────────────────────────────────────────────────────────────────────────────┐ 
 │  Running packager on port 8081.                                            │ 
 │                                                                            │ 
 │  Keep this packager running while developing on any JS projects. Feel      │ 
 │  free to close this tab and run your own packager instance if you          │ 
 │  prefer.                                                                   │ 
 │                                                                            │ 
 │  https://github.com/facebook/react-native                                  │ 
 │                                                                            │ 
 └────────────────────────────────────────────────────────────────────────────┘ 
Looking for JS files in
   /home/petra/Projects/TestReactNative 

[12:14:37 PM] <START> Building Dependency Graph
[12:14:37 PM] <START> Crawling File System
[Hot Module Replacement] Server listening on /hot

React packager ready.

[12:14:46 PM] <END>   Crawling File System (8954ms)
[12:14:46 PM] <START> Building in-memory fs for JavaScript
[12:14:46 PM] <END>   Building in-memory fs for JavaScript (121ms)
[12:14:46 PM] <START> Building in-memory fs for Assets
[12:14:46 PM] <END>   Building in-memory fs for Assets (81ms)
[12:14:46 PM] <START> Building Haste Map
[12:14:46 PM] <START> Building (deprecated) Asset Map
[12:14:46 PM] <END>   Building (deprecated) Asset Map (26ms)
[12:14:46 PM] <END>   Building Haste Map (239ms)
[12:14:46 PM] <END>   Building Dependency Graph (9399ms)
[12:14:48 PM] <START> request:/index.android.bundle
[12:14:48 PM] <START> find dependencies
[12:14:48 PM] <END>   find dependencies (479ms)
[12:14:48 PM] <START> transform

When I tried curl http://localhost:8081/index.android.bundle. It seems doing okay.

And I tried moving the index.android.js to src/index.android.js.

Here's the commit

https://github.com/petrabarus/TestReactNative/commit/ae84ba494a5762deb42e47dd8c49348a7a1f3aa4

I tried restarting the npm start and curl the link again. Here's what it output.

{"message":"Cannot find entry file index.android.js in any of the roots: [\"/home/petra/Projects/TestReactNative\"]","name":"NotFoundError","type":"NotFoundError","status":404,"errors":[{}]}

petrabarus avatar Apr 05 '16 05:04 petrabarus

Try like this: @Override protected String getJSMainModuleName() { return "android/app/src/index.android"; }

shivangbtech avatar Apr 05 '16 05:04 shivangbtech

@shivangbtech still not working.

petrabarus avatar Apr 05 '16 06:04 petrabarus

Take a look at this, this is what you'll be overriding: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java#L60. So basically you just need to change that to include your new path from the root of your project.

dslounge avatar May 17 '16 09:05 dslounge

@dslounge

I changed the content of android/app/src/main/java/com/testreactnative/MainActivity.java

    @Override
    protected String getJSMainModuleName() {
        return "src/index.android";
    }

and then I run react-native start.

When I tried curl http://localhost:8081/index.android.bundle it says

{"message":"Cannot find entry file index.android.js in any of the roots: [\"/home/petra/Projects/TestReactNative\"]","name":"NotFoundError","type":"NotFoundError","status":404,"errors":[{}]}

Anything that I missed?

petrabarus avatar May 17 '16 23:05 petrabarus

You moved your file to /src, so the your path should be: http://localhost:8081/src/index.android.bundle

dslounge avatar May 18 '16 00:05 dslounge