react native run android has an error. Cannot change dependencies of configuration ':packagename:androidApis' after it has been resolved error

主宰稳场 提交于 2019-12-11 17:15:28

问题


I have a React Native project with many libraries like React Native Fetch Blob, React Native Camera, etc.

For run the project I clone my repository and install node modules so run react-native run command encounter below error:

After hours of search and try, I can't solve the problem. after that, I remove React Native Fetch Blob from the project and run android again so a new error encounter for React Native Camera

I think this error raise on my android configuration and doesn't have to React Native libraries.

android > app > 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"
]

project.ext.envConfigFiles = [
        debug             : ".env.development",
        release           : ".env.production",
        anothercustombuild: ".env",
]

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.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 = true

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

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "**.***.*****"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode project.env.get("ANDROID_VERSION_CODE") as Integer
        versionName project.env.get("ANDROID_VERSION_NAME")
        missingDimensionStrategy 'react-native-camera', 'general'
        multiDexEnabled true
    }
    signingConfigs {
        release {
            if (project.env.get("ANDROID_RELEASE_STORE_FILE")) {
                storeFile file(project.env.get("ANDROID_RELEASE_STORE_FILE"))
                storePassword project.env.get("ANDROID_RELEASE_STORE_PASSWORD")
                keyAlias project.env.get("ANDROID_RELEASE_KEY_ALIAS")
                keyPassword project.env.get("ANDROID_RELEASE_KEY_PASSWORD")
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk true  // If true, also generate a universal APK
            include "x86", "x86_64", "armeabi-v7a", "arm64-v8a"
        }
    }
    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 = ['x86':1, 'x86_64':2, 'armeabi-v7a':3, 'arm64-v8a':4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi)  + defaultConfig.versionCode * 100
            } else {
                output.versionCodeOverride = defaultConfig.versionCode * 100
            }
        }
    }
}

dependencies {
    implementation project(':react-native-config')
    implementation project(':react-native-camera')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-spinkit')
    implementation project(':react-native-share')
    implementation project(':react-native-image-resizer')
    implementation project(':react-native-gesture-handler')
    implementation project(':react-native-splash-screen')
    implementation project(':react-native-i18n')
    implementation project(':rn-fetch-blob')
    implementation project(':react-native-image-picker')
    implementation project(':react-native-fs')
    implementation project(':react-native-contacts')
    implementation project(':react-native-linear-gradient')
    implementation project(':react-native-view-shot')
    implementation project(':react-native-picker')
    implementation project(':react-native-document-picker')
    implementation project(':react-native-exit-app')
    implementation project(':react-native-firebase')
    compile('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') {
        transitive = true;
    }
    implementation project(':react-native-device-info')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    // From node_modules
    // Firebase dependencies
    implementation 'com.google.android.gms:play-services-base:16.0.1'
    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.google.android.gms:play-services-gcm:16.1.0'
    implementation "com.google.firebase:firebase-messaging:18.0.0"
    implementation 'me.leolin:ShortcutBadger:1.1.21@aar'
    compile 'com.android.support:multidex:1.0.0'
    compile project(':react-native-file-opener1')
}

// 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'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true

android > build.gradle file is:

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

buildscript {
    ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "27.1.1"
    }
    repositories {
        google()
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath("com.android.tools.build:gradle:3.4.0")
        classpath 'com.google.gms:google-services:4.0.2'
        classpath 'io.fabric.tools:gradle:1.+'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenLocal()
        maven { url "https://jitpack.io" }
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
        google()
    }
    configurations.all {
        resolutionStrategy.force 'com.google.android.gms:play-services-gcm:16.1.0', 'com.google.android.gms:play-services-basement:15.0.1'
        all*.exclude group: 'com.squareup.okhttp3', module: 'okhttp'
        all*.exclude group: 'com.squareup.okio', module: 'okio'

        resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.10.0'

        all*.exclude group: 'com.github.bumptech.glide', module: 'glide'
        all*.exclude group: 'org.litepal.android', module: 'core'
    }
}

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion rootProject.ext.compileSdkVersion
                buildToolsVersion rootProject.ext.buildToolsVersion
            }
        }
    }
}

anyone can help me to solve this problem???


回答1:


It was a confusing problem for me. For solving this problem I do some steps:

1) change ext variable like below:

ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "27.1.1"
    }

2) Increase react native version to "react-native": "0.59.8",

3) Enabled androidX in gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

4) Change appcompat library imports to AndroidX library in my java and manifest files. For example, I changed from this:

<provider android:name="android.support.v4.content.FileProvider" ... 

to this:

<provider android:name="androidx.core.content.FileProvider" ... 

5) Install jetifier

6) Run npx jetifier

7) Run npx react-native run-android to run android project

8) Enjoy your lovely application



来源:https://stackoverflow.com/questions/57694205/react-native-run-android-has-an-error-cannot-change-dependencies-of-configurati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!