npm run android无法启动Android应用 [英] npm run android does not launch the Android app

查看:739
本文介绍了npm run android无法启动Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够清除上一个反应本机导航安装.这些警告消失了.

I was able to clear my last issue after ignoring the errors and following the rest of the steps in the React Native Navigation installation. Those warnings went away.

但是,当我运行npm run android时,它可以成功构建,但无法启动应用程序

However, when I run npm run android, it builds successfully but does not launch the app

Configure project :app
...
Configure project :react-native-navigation
...
Task :app:installDebug
...
BUILD SUCCESSFUL in 6s

android模拟器转到其主屏幕,并且该应用程序无法启动.

The android emulator goes to its home screen and the app does not launch.

  • 我尝试从android studio运行/构建/清理和重建.当我从白色空白屏幕刷新时,导致出现Could not connect to development server.
  • 我已经按照此处,方法是创建资产文件夹并运行此代码

  • I've tried running/building/clean&rebuilding from android studio. This led to Could not connect to development server when I refreshed from the white blank screen.
  • I've followed the steps in here by creating assets folder and running this code

react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 19
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        mavenLocal()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
        maven { url 'https://jitpack.io' }
    }
}

app/build.gradle

apply plugin: "com.android.application"

import com.android.build.OutputFile

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

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

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "com.appname"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        missingDimensionStrategy "RNN.reactNativeVersion", "reactNative57_5"
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    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 are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            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
            }
        }
    }
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support' && requested.name != 'multidex') {
            details.useVersion "${rootProject.ext.supportLibVersion}"
        }
    }
}

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

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

  • 本机:"0.57.8"
  • react-native-navigation:"^ 2.7.0"
  • Android 9.0 API 28
  • 推荐答案

    我认为您可能必须在运行npm run android之前运行metro bundler.

    I think you maybe have to run the metro bundler before running npm run android.

    react-native start --reset-cache
    

    这将启动捆绑器,因此您必须运行

    This will start the bundler, so you will have to run

    npm run android
    

    在另一个终端窗口中,但是在相同的项目目录中.下面提到了该脚本!这就是我这样做的方式.自RN 57.2以来一直存在一个问题,即地铁捆绑器存在一些问题,这是解决之道.同时,我已经更新到RN 57.8和最新的RNN,但是我仍然像这样运行该项目.

    in a different terminal window, but same project directory. This script is mentioned below! This is the way I'm doing it. It has been an issues since RN 57.2 that there were some problems with the metro bundler and this is the way to go. I have updated in the meantime to RN 57.8 and the latest RNN also, but I still run the project like that.

    我还必须提到,我是在具有API 26和Android Studio 3.3的仿真器上运行此程序的,通常,如果如上所述运行它,它将在仿真器中启动该应用程序.

    I also have to mention that I'm running this on a emulator with API 26 and Android Studio 3.3 and normally If I run it as I said above, it will start the app in the emulator.

    内置脚本

    "scripts": {
        "build-android": "cd ./android && ./gradlew app:assembleDebug && ./gradlew installDebug && cd ../",
        "android": "npm run build-android && (adb reverse tcp:8081 tcp:8081 || true) && react-native run-android"
    }
    

    编辑

    按照RNN小组在此链接下提供的说明在Android上运行您的应用程序尤为重要: RNN小组在Android上运行应用程序的说明模拟器或设备.

    It is especially important that you follow the instructions given by RNN team under this link for running your app on Android: The instructions from RNN team for running your app on an Android emulator or device.

    编辑2

    根据请求,需要API 26,因为这是您的编译SDK版本(当前为RNN v2.7.1)以及您的目标SDK版本.您可以在上述链接的RNN文档中找到此信息.

    As per request, API 26 is required as that is your compile SDK version(currently for RNN v2.7.1) as well as your target SDK version. This info you can find in the documentation for RNN at the same link as above.

    这篇关于npm run android无法启动Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆