Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing react-native project in android studio

Am having a small problem with opening/importing a react-native into Android studio.

  1. If I open the project using the open a project dialog, it tells me that the project is not gradle enabled and it is such a pain to make and test code changes. Couldn't find out how to enable the project as a gradle project after the fact even after going through the material on the help site.
  2. On the other hand, if I import using the import a gradle project dialog and select the build.gradle file, the project is imported, but I only see the files inside the android directory instead of the main project directory. But this method allows me to push changes easily to the emulator.

    How can I fix my problem?

Thanks,

like image 657
Gavin Ang Avatar asked Jul 26 '16 03:07

Gavin Ang


1 Answers

Just import android directory from Android Studio, make changes to your app/build.gradle add these codes before apply from: "../../node_modules/react-native/react.gradle"

project.ext.react = [
        bundleAssetName: "index.android.bundle",
        entryFile: "index.android.js",
        bundleInDebug: false,
        bundleInRelease: true,
        root: "../../",
        jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
        jsBundleDirRelease: "$buildDir/intermediates/assets/release",
        resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
        resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
        inputExcludes: ["android/", "ios/"],
        nodeExecutableAndArgs: ["node"],
        extraPackagerArgs: []
]

Now create task for "react-native start" to your gradle

task startReactNative(type: Exec) {
    workingDir "../../"
    commandLine  'cmd', '/c', 'react-native', 'start'
    println "Working Directory for React is: $workingDir"
    standardOutput = new ByteArrayOutputStream()
    ext.output = {
        println "React-Native Output: " + standardOutput.toString()
        return standardOutput.toString()
    }
}

You can run your app as usual, after app installed to your device, run startReactNative task in order to activate Hot Reload

like image 58
Putra Ardiansyah Avatar answered Sep 19 '22 12:09

Putra Ardiansyah