【问题标题】:Issue with Android Emulator no longer connecting to package serverAndroid Emulator 不再连接到包服务器的问题
【发布时间】:2019-08-07 18:18:06
【问题描述】:

为我的应用程序安装和配置react-native-firebase 包后,运行react-native run-android 不再创建index.android.bundle 文件。我已经能够通过运行以下命令来解决这个问题:

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

但是,我现在每次更改应用程序中的 javascript 时都需要运行此命令,因为应用程序不再尝试连接到 Metro Bundler。我也不能通过点击R R 来强制它连接,并且不会显示任何错误。该应用程序确实可以正确启动,只是有这么大的警告。

我不知道在哪里看,我不是 Android/Java 开发人员。我在 React-Native 版本 0.59.5

android/build.gradle

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

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath 'com.google.gms:google-services:4.3.0'

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

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

        // ADD THIS
        maven { url "https://jitpack.io" }
    }
}

android/app/build.gradle

apply plugin: "com.android.application"

project.ext.vectoricons = [
    iconFontNames: [ 'file.icomoon.ttf' ] // Name of the font files you want to copy
]

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

import com.android.build.OutputFile

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation
 *   entryFile: "index.android.js",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
 *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

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 27
    buildToolsVersion "27.0.3"
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.CLIENTNAME"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        targetSdkVersion 27
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    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, "arm64-v8a": 3, "x86_64": 4]
            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 {
    implementation project(':react-native-firebase')
    implementation project(':react-native-haptic-feedback')
    implementation project(':react-native-image-crop-picker')
    implementation project(':react-native-video')
    implementation project(':@react-native-community_async-storage')
    implementation project(':react-native-svg')
    implementation project(':react-native-webview')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-gesture-handler')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules

    // Firebase dependencies
    implementation "com.google.android.gms:play-services-base:16.1.0"
    implementation "com.google.firebase:firebase-core:17.0.1"
    implementation "com.google.firebase:firebase-analytics:17.0.1"
}

// 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'
}

apply plugin: 'com.google.gms.google-services'

我不确定这里是否还有其他文件可以提供上下文。如果我在这里遗漏任何信息,请告诉我。

我希望当我运行 react-native run-android 时,捆绑器将运行并创建应用程序需要运行的 android/app/src/main/res/index.android.bundle 文件,并且应用程序将从模拟器连接到 Metro Bundler。这使我能够轻松地在模拟器中查看对我的 JS 代码的更改。没有错误信息可言。

【问题讨论】:

    标签: android react-native gradle android-gradle-plugin react-native-android


    【解决方案1】:

    在您的MainApplication.java 类中,检查方法

    @Override
    public boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }
    

    BuildConfig.DEBUG 可能总是返回false,因此请确保BuildConfig 的导入包来自您的项目,如this answer 中所述,如果您错误地从不同的包,BuildConfig 导入可能属于已发布的库,其中DEBUG 是false。

    另外,请查看此问题的答案 Could not connect to development server on android emulator and on real device #15388 。

    【讨论】:

      【解决方案2】:

      尝试使用npm start reset --cache 启动打包程序

      如果针对 android X,还升级到最近的 react native cli 并使用 jetifiers 和 npx

      【讨论】:

      • 感谢您的回复。不幸的是,我尝试了所有这些解决方案都没有运气。
      • 做一件事。 1.删​​除节点模块文件夹。 2. 删除包 lock.JSON 文件夹 3. 升级所有开发依赖 ` "metro-react-native-babel-preset": "^0.53.1",` 4. 如果 react native 则将 react native CLI 降级到 1.0.2版本小于 0.60,否则升级到 2.0+。让我知道结论
      • 另外补充我的答案是,如果您从终端执行 npm start --reset cache 然后运行模拟器然后刷新它。它应该启动它。另请参阅端口是 8081
      • 我刚刚做了以下事情: 1. 删除了node_modules 2. 删除了package-lock.json 3. 升级了所有依赖项 4. 将metro-react-native-babel-preset: 设置为^0.53.1 5. 降级为react-native- cli 1.3.0 因为那是最新的 v1 版本 6. react-native start --reset-cache 7. 打开 Android Emulator 8. Ran react-native run-android 仍然没有创建包。
      猜你喜欢
      • 1970-01-01
      • 2014-01-13
      • 2020-12-02
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多