使用 react-native run-android 运行时出现 React-Native 错误
Posted
技术标签:
【中文标题】使用 react-native run-android 运行时出现 React-Native 错误【英文标题】:React-Native error when running with react-native run-android 【发布时间】:2020-07-27 00:44:33 【问题描述】:我对 React-Native 有一些问题。我尝试更改 react-native 版本或编辑 build.gradle 移动 google() 或 jcenter(),但错误仍然存在。
有人可以帮助我吗? 我认为是build.gradle的问题,但我不确定。
环境:
React-Native: "^0.59.10"
NodeJS: "v10.16.3"
project/android/build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript
ext
buildToolsVersion = "28.0.2"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 28
supportLibVersion = "27.1.1"
repositories
jcenter()
google()
maven
url 'https://maven.fabric.io/public'
dependencies
//classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.4.1'
// Check for v3.1.2 or higher
classpath 'com.google.gms:google-services:4.3.0' // Google Services plugin
// Add dependency
classpath 'io.fabric.tools:gradle:1.31.0' // Crashlytics plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
allprojects
repositories
mavenLocal()
google()
jcenter
url "http://jcenter.bintray.com/"
maven
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
//google()
wrapper
gradleVersion = '4.4'
distributionUrl = distributionUrl.replace("bin", "all")
project/android/app/src/build.gradle:
apply plugin: "com.android.application"
apply plugin: 'io.fabric'
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"
project.ext.vectoricons = [
iconFontNames: [ 'MaterialIcons.ttf', 'EvilIcons.ttf' ] // Name of the font files you want to copy
]
apply from: "../../node_modules/react-native-vector-icons/fonts.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
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig
applicationId "xxx"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
multiDexEnabled true
versionCode 5
versionName "1.0.4"
ndk
abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
signingConfigs
release
if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE'))
storeFile file(MYAPP_UPLOAD_STORE_FILE)
storePassword MYAPP_UPLOAD_STORE_PASSWORD
keyAlias MYAPP_UPLOAD_KEY_ALIAS
keyPassword MYAPP_UPLOAD_KEY_PASSWORD
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"
signingConfig signingConfigs.release
// 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
configurations.all resolutionStrategy force 'com.facebook.android:facebook-android-sdk:4.34.0'
dependencies
implementation project(':@react-native-community_async-storage')
implementation project(':react-native-firebase')
implementation project(':react-native-sensitive-info')
implementation project(':react-native-iap')
implementation project(':react-native-exit-app')
implementation project(':react-native-splash-screen')
implementation project(':@react-native-community_netinfo')
implementation project(':react-native-android-location-enabler')
implementation project(':lottie-react-native')
implementation project(':react-native-restart')
implementation project(':react-native-linear-gradient')
implementation project(':react-native-vector-icons')
implementation project(':react-native-android-location-services-dialog-box')
implementation project(':react-native-geocoder')
implementation project(':react-native-navigation')
implementation project(':react-native-firebase')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibVersion"
implementation 'com.android.support:design:27.1.0'
implementation "com.facebook.react:react-native:+" // From node_modules
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-auth:18.0.0'
implementation 'com.google.firebase:firebase-database:18.0.0'
// (Recommended) Add Analytics
implementation 'com.google.firebase:firebase-analytics:17.0.1'
// Add dependency
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'androidx.multidex:multidex:2.0.0'
//FOR GIF
implementation 'com.facebook.fresco:fresco:2.1.0'
// For animated GIF support
implementation 'com.facebook.fresco:animated-gif:1.+'
// For WebP support, including animated WebP
implementation 'com.facebook.fresco:animated-webp:1.+'
implementation 'com.facebook.fresco:webpsupport: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'
configurations.all
resolutionStrategy
force "com.airbnb.android:lottie:2.5.5"
apply plugin: 'com.google.gms.google-services'
这是错误的输出:
* What went wrong:
Could not determine the dependencies of task ':app:preReleaseBuild'.
> Could not resolve all task dependencies for configuration ':app:releaseRuntimeClasspath'.
> Could not resolve com.facebook.fresco:fresco:2.1.0.
Required by:
project :app
> Could not resolve com.facebook.fresco:fresco:2.1.0.
> Could not get resource 'http://jcenter.bintray.com/com/facebook/fresco/fresco/2.1.0/fresco-2.1.0.pom'.
> Could not GET 'http://jcenter.bintray.com/com/facebook/fresco/fresco/2.1.0/fresco-2.1.0.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.facebook.fresco:animated-gif:1.+.
Required by:
project :app
> Failed to list versions for com.facebook.fresco:animated-gif.
> Unable to load Maven meta-data from http://jcenter.bintray.com/com/facebook/fresco/animated-gif/maven-metadata.xml.
> Could not get resource 'http://jcenter.bintray.com/com/facebook/fresco/animated-gif/maven-metadata.xml'.
> Could not GET 'http://jcenter.bintray.com/com/facebook/fresco/animated-gif/maven-metadata.xml'. Received status code 403 from server: Forbidden
> Could not resolve com.facebook.fresco:animated-webp:1.+.
Required by:
project :app
> Failed to list versions for com.facebook.fresco:animated-webp.
> Unable to load Maven meta-data from http://jcenter.bintray.com/com/facebook/fresco/animated-webp/maven-metadata.xml.
> Could not get resource 'http://jcenter.bintray.com/com/facebook/fresco/animated-webp/maven-metadata.xml'.
> Could not GET 'http://jcenter.bintray.com/com/facebook/fresco/animated-webp/maven-metadata.xml'. Received status code 403 from server: Forbidden
> Could not resolve com.facebook.fresco:webpsupport:1.+.
Required by:
project :app
> Failed to list versions for com.facebook.fresco:webpsupport.
> Unable to load Maven meta-data from http://jcenter.bintray.com/com/facebook/fresco/webpsupport/maven-metadata.xml.
> Could not get resource 'http://jcenter.bintray.com/com/facebook/fresco/webpsupport/maven-metadata.xml'.
> Could not GET 'http://jcenter.bintray.com/com/facebook/fresco/webpsupport/maven-metadata.xml'. Received status code 403 from
server: Forbidden
> Could not resolve com.android.billingclient:billing:1.1.
Required by:
project :app > project :react-native-iap
> Could not resolve com.android.billingclient:billing:1.1.
> Could not get resource 'http://jcenter.bintray.com/com/android/billingclient/billing/1.1/billing-1.1.pom'.
> Could not GET 'http://jcenter.bintray.com/com/android/billingclient/billing/1.1/billing-1.1.pom'. Received status code 403 from
server: Forbidden
> Could not resolve com.airbnb.android:lottie:2.5.5.
Required by:
project :app > project :lottie-react-native
> Could not resolve com.airbnb.android:lottie:2.5.5.
> Could not get resource 'http://jcenter.bintray.com/com/airbnb/android/lottie/2.5.5/lottie-2.5.5.pom'.
> Could not GET 'http://jcenter.bintray.com/com/airbnb/android/lottie/2.5.5/lottie-2.5.5.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.aurelhubert:ahbottomnavigation:2.0.6.
Required by:
project :app > project :react-native-navigation
> Could not resolve com.aurelhubert:ahbottomnavigation:2.0.6.
> Could not get resource 'http://jcenter.bintray.com/com/aurelhubert/ahbottomnavigation/2.0.6/ahbottomnavigation-2.0.6.pom'.
> Could not GET 'http://jcenter.bintray.com/com/aurelhubert/ahbottomnavigation/2.0.6/ahbottomnavigation-2.0.6.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.balysv.materialmenu:material-menu-toolbar:1.5.4.
Required by:
project :app > project :react-native-navigation
> Could not resolve com.balysv.materialmenu:material-menu-toolbar:1.5.4.
> Could not get resource 'http://jcenter.bintray.com/com/balysv/materialmenu/material-menu-toolbar/1.5.4/material-menu-toolbar-1.5.4.pom'.
> Could not GET 'http://jcenter.bintray.com/com/balysv/materialmenu/material-menu-toolbar/1.5.4/material-menu-toolbar-1.5.4.pom'.
Received status code 403 from server: Forbidden
> Could not resolve com.facebook.infer.annotation:infer-annotation:0.11.2.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve com.facebook.infer.annotation:infer-annotation:0.11.2.
> Could not get resource 'http://jcenter.bintray.com/com/facebook/infer/annotation/infer-annotation/0.11.2/infer-annotation-0.11.2.pom'.
> Could not GET 'http://jcenter.bintray.com/com/facebook/infer/annotation/infer-annotation/0.11.2/infer-annotation-0.11.2.pom'. Received status code 403 from server: Forbidden
> Could not resolve javax.inject:javax.inject:1.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve javax.inject:javax.inject:1.
> Could not get resource 'http://jcenter.bintray.com/javax/inject/javax.inject/1/javax.inject-1.pom'.
> Could not GET 'http://jcenter.bintray.com/javax/inject/javax.inject/1/javax.inject-1.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.facebook.fresco:fresco:2.1.0.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve com.facebook.fresco:fresco:2.1.0.
> Could not get resource 'http://jcenter.bintray.com/com/facebook/fresco/fresco/2.1.0/fresco-2.1.0.pom'.
> Could not GET 'http://jcenter.bintray.com/com/facebook/fresco/fresco/2.1.0/fresco-2.1.0.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.facebook.fresco:imagepipeline-okhttp3:1.10.0.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve com.facebook.fresco:imagepipeline-okhttp3:1.10.0.
> Could not get resource 'http://jcenter.bintray.com/com/facebook/fresco/imagepipeline-okhttp3/1.10.0/imagepipeline-okhttp3-1.10.0.pom'.
> Could not GET 'http://jcenter.bintray.com/com/facebook/fresco/imagepipeline-okhttp3/1.10.0/imagepipeline-okhttp3-1.10.0.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.facebook.soloader:soloader:0.6.0.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve com.facebook.soloader:soloader:0.6.0.
> Could not get resource 'http://jcenter.bintray.com/com/facebook/soloader/soloader/0.6.0/soloader-0.6.0.pom'.
> Could not GET 'http://jcenter.bintray.com/com/facebook/soloader/soloader/0.6.0/soloader-0.6.0.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.google.code.findbugs:jsr305:3.0.2.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve com.google.code.findbugs:jsr305:3.0.2.
> Could not get resource 'http://jcenter.bintray.com/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.pom'.
> Could not GET 'http://jcenter.bintray.com/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.squareup.okhttp3:okhttp:3.12.1.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve com.squareup.okhttp3:okhttp:3.12.1.
> Could not get resource 'http://jcenter.bintray.com/com/squareup/okhttp3/okhttp/3.12.1/okhttp-3.12.1.pom'.
> Could not GET 'http://jcenter.bintray.com/com/squareup/okhttp3/okhttp/3.12.1/okhttp-3.12.1.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.squareup.okhttp3:okhttp-urlconnection:3.12.1.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve com.squareup.okhttp3:okhttp-urlconnection:3.12.1.
> Could not get resource 'http://jcenter.bintray.com/com/squareup/okhttp3/okhttp-urlconnection/3.12.1/okhttp-urlconnection-3.12.1.pom'.
> Could not GET 'http://jcenter.bintray.com/com/squareup/okhttp3/okhttp-urlconnection/3.12.1/okhttp-urlconnection-3.12.1.pom'. Received status code 403 from server: Forbidden
> Could not resolve com.squareup.okio:okio:1.15.0.
Required by:
project :app > com.facebook.react:react-native:0.59.10
> Could not resolve com.squareup.okio:okio:1.15.0.
> Could not get resource 'http://jcenter.bintray.com/com/squareup/okio/okio/1.15.0/okio-1.15.0.pom'.
> Could not GET 'http://jcenter.bintray.com/com/squareup/okio/okio/1.15.0/okio-1.15.0.pom'. Received status code 403 from server:
Forbidden
> Could not resolve com.google.auto.value:auto-value-annotations:1.6.3.
Required by:
project :app > com.google.firebase:firebase-auth:18.0.0 > com.google.firebase:firebase-common:18.0.0
> Could not resolve com.google.auto.value:auto-value-annotations:1.6.3.
> Could not get resource 'http://jcenter.bintray.com/com/google/auto/value/auto-value-annotations/1.6.3/auto-value-annotations-1.6.3.pom'.
> Could not GET 'http://jcenter.bintray.com/com/google/auto/value/auto-value-annotations/1.6.3/auto-value-annotations-1.6.3.pom'.
Received status code 403 from server: Forbidden
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
【问题讨论】:
【参考方案1】:错误明确指出 fresco 库在 android 中丢失,请手动将它们添加到您的 gradle 文件中https://github.com/facebook/fresco 并且您的问题将得到解决,如果您要添加任何库,请不要忘记链接它。一个建议转向最新版本的 react-native。
【讨论】:
您好,感谢您的帮助,我添加了“implementation 'com.facebook.fresco:fresco:2.1.0'”,但错误并没有消失。我已经更新了我的问题,将我的 build.gradle 文件发布到 android/app/ 文件夹和新的控制台输出中。 然后我更新到 react-native 0.62.2。什么都没有改变。 @DanieleMenchetti 尝试调试,url 抛出 403,尝试用 maven url "jcenter.bintray.com" 替换 jcenter(),这是 https 不是 http,因为 jcenter 不允许 http不再请求 是的,昨天我看到了这个问题。非常感谢你的帮助! :)以上是关于使用 react-native run-android 运行时出现 React-Native 错误的主要内容,如果未能解决你的问题,请参考以下文章
React-native:如何在 React-native 中使用(和翻译)带有 jsx 的 typescript .tsx 文件?
使用 react-native router-flux 时,BackHandler 在 react-native 侧面菜单中不起作用