首页 文章

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

提问于
浏览
1

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

enter image description here

为解决第一个错误,我将appcompat版本更改为项目级build.gradle文件中的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

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

    • 在项目级build.gradle中将此行代码添加到repositoreis部分:
    maven {
        url "$rootDir/../node_modules/react-native/android"
    }
    
    • 第一步使构建过程成功,但在模拟器或物理设备上运行会显示一个红色屏幕,其中显示: Unable to load script from assets index.android.bundle . 要解决此问题,请按照instructions进行操作 .

    • 要完成第二步,您应该将react-native版本从版本0.56降级到0.55.4(如果您使用此版本),因为0.56版本中存在错误 . How to!

    • 如果在执行第二步后遇到 babel-preset-react-native\index.js" provided an invalid property of default... ,只需更改项目package.json中的以下行,然后使用以下命令同步:

    npm安装

    (package.json)
    
    "babel-preset-react-native": "^4.0.0"
    

    并非所有人都需要执行所有步骤,但是当我遇到错误时,此过程可帮助我解决这些问题 .

相关问题