将React-Native的Android项目导入Android Studio

时间:2018-08-09 13:00:17

标签: android android-studio react-native

我创建了一个简单的React-Native项目,并尝试将其导入Android Studio IDE。问题是构建过程失败并出现以下两个错误:

enter image description here

为解决第一个错误,我在项目级别的build.gradle文件中将appcompat版本更改为27.0.2,在此文件中,我之前的本机android应用程序已成功构建。但是使用此API版本会抛出相同的错误。在下面的build.gradle文件已附加,以查看是否有问题。

(build.gradle) --> project level    
    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
}


ext {
    buildToolsVersion = "27.0.2"
    minSdkVersion = 16
    compileSdkVersion = 27
    targetSdkVersion = 27
    supportLibVersion = "27.0.2"
}

这是模块级别的build.gradle。

project.ext.react = [
    entryFile: "index.js"
]

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

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "com.sampleproj"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    compile "com.facebook.react:react-native:+"  // From node_modules
}

task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

我该如何解决这些问题?

1 个答案:

答案 0 :(得分:0)

我找到了解决这些问题的步骤。

  1. 在项目级别的build.gradle中将此代码行添加到repositoreis部分:

    maven {
        url "$rootDir/../node_modules/react-native/android"
    }
    
  2. 第一步使构建过程成功,但在模拟器或物理设备上运行时会显示红色屏幕,其中指出:无法从资产index.android.bundle加载脚本。要解决此问题,请遵循instructions

  3. 要完成第二步,应将本机版本从0.56降级到0.55.4(如果使用此版本),因为0.56版本中存在错误。 How to!
  4. 如果在执行第二步后遇到 babel-preset-react-native \ index.js“提供的默认属性无效... ,只需更改软件包中的以下行即可。项目的json,然后使用以下命令进行同步:
  

npm安装

(package.json)

"babel-preset-react-native": "^4.0.0"

并非所有人都需要执行所有步骤,但是当我遇到错误时,此过程可以帮助我解决它们。