在Android模拟器无法再连接到软件包服务器的问题。
在Android模拟器无法再连接到软件包服务器的问题。
在为我的应用安装和配置了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代码的更改。没有错误消息。
问题出现的原因可能有以下几点:
1. Android模拟器无法连接到包服务器。
2. 使用的React Native CLI版本过旧。
3. 使用的依赖版本不兼容。
4. 缓存未正确清除。
解决方法如下:
1. 尝试使用npm start reset --cache
命令启动打包器。
2. 升级到最新的React Native CLI版本,并使用jetifiers和npx来支持Android X。
3. 删除node_modules
文件夹和package-lock.json
文件,然后升级所有的开发依赖项,将metro-react-native-babel-preset
设置为^0.53.1
,如果React Native版本小于0.60,则降级React Native CLI到1.0.2,否则升级到2.0+。
4. 从终端运行npm start --reset-cache
命令,然后运行模拟器并刷新它,确保端口号为8081。
5. 删除node_modules
文件夹和package-lock.json
文件,升级所有的依赖项,将metro-react-native-babel-preset
设置为^0.53.1
,并降级到最新的v1版本的React Native CLI(1.3.0),然后运行react-native start --reset-cache
命令,打开Android模拟器,并运行react-native run-android
命令。
希望以上解决方法能解决您的问题。
在你的`MainApplication.java`类中,检查方法`getUseDeveloperSupport()`。可能发生的情况是`BuildConfig.DEBUG`始终返回`false`,所以确保`BuildConfig`的导入包是你的项目中的,如此答案所述,如果你错误地从不同的包导入了`BuildConfig`,那么`BuildConfig`的导入可能属于一个发布库,其中`DEBUG`是`false`。
此外,检查此问题的答案。
问题解决方法:
1. 确保`BuildConfig`的导入包是你的项目中的。
2. 检查`getUseDeveloperSupport()`方法,确保`BuildConfig.DEBUG`返回正确的值。
3. 检查此问题的答案,寻找其他可能的解决方法。