Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

java - Change entry file path of android and ios in react-native project

After starting a new project with react-native init awesomeProject, i try to restructure my project, putting index.ios.js and index.android.ios inside a common folder named src.

When i execute react-native run-android, i get the following error:

Error on android device

Where i have to change to react-native search for entry files in the right path?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've used the following steps on react-native 0.35

For development you need to open a file

MyProject/android/app/src/main/java/com/MyProject/MainApplication.java

and override a method of ReactNativeHost called getJSMainModuleName:

package com.MyProject;

// ...

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {

    // ...

    // ADD THE LINES BELOW

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

    // ADD THE LINES ABOVE

  };

  // ...
}

This way the app will know where to fetch the module from a packager server.

For production, when you build your APK using cd android && ./gradlew assembleRelease you'll have to modify a file

MyProject/android/app/build.gradle

and add custom build options, make sure to place them before apply from: "../../node_modules/react-native/react.gradle" line:

apply plugin: "com.android.application"

import com.android.build.OutputFile

// ...

// ADD THE LINES BELOW

project.ext.react = [
    // the entry file for bundle generation
    entryFile: "src/index.android.js",
]

// ADD THE LINES ABOVE

// ...

apply from: "../../node_modules/react-native/react.gradle"

Unfortunately I don't have iOS setup right now, I can't help you with that yet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...