android-studio - 在android studio中导入react-native项目

标签 android-studio react-native

在 Android studio 中打开/导入 React-native 时遇到一个小问题。

  1. 如果我使用打开项目对话框打开项目,它会告诉我该项目未启用 gradle,并且进行和测试代码更改非常痛苦。即使在浏览了帮助网站上的 Material 后,也无法找到如何将项目启用为 gradle 项目。
  2. 另一方面,如果我使用“导入 gradle 项目”对话框导入并选择 build.gradle 文件,则项目会被导入,但我只能看到 android 目录中的文件,而不是主项目目录。但这种方法允许我轻松地将更改推送到模拟器。

    如何解决我的问题?

谢谢

最佳答案

只需从 Android Studio 导入 android 目录即可, 更改您的 app/build.gradleapply 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: []
]

现在为你的gradle创建“react-native start”任务

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()
    }
}

您可以照常运行您的应用,将应用安装到您的设备后,运行 startReactNative 任务以激活热重载

关于android-studio - 在android studio中导入react-native项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38580363/

相关文章:

android - 单击按钮时应用程序停止工作

android-studio - 错误 :Unable to start the daemon process:

android - 当我尝试在Android程序中放入背景时出现错误

javascript - React 中的尖括号和星号是什么意思?

css - React Native 中的 Margin 或 Padding 速记

ios - undefined symbol _RCTRegisterModule

react-native - 异步/等待函数返回 _40 : 0, _65 : 0, _55 : null, _72: null

Android Studio 2.2 原生开发迁移问题

android - 使用android studio创建自定义测试文件夹

android - Android 的 Expo 构建失败