React-native-config: Android Build Exception - Could not find method android() for arguments

Created on 12 Apr 2018  路  2Comments  路  Source: luggit/react-native-config

I am receiving the following error when building with version 0.11.5. I have tried downgrading to version 0.9.0 but it also fails immediately.

I wonder if the issue could be with the gradle version compatibility. I'm using 3.1.0.

The error occurs both from the command line react-native run-android and when running from Android Studio.

Error when building

FAILURE: Build failed with an exception.

* Where:
Script '/Users/me/Coding/primarybid/PrimaryBid/node_modules/react-native-config/android/dotenv.gradle' line: 71

* What went wrong:
A problem occurred evaluating script.
> Could not find method android() for arguments [dotenv_b5kcwccrnqlch2sbpicyolac4$_run_closure2@693715b9] on project ':app' of type org.gradle.api.Project.

Removing the line resolve this but obviously this defeats the purpose.
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

Files

android/build.gradle

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

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.fabric.io/public'
        }
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        classpath 'com.google.gms:google-services:3.1.2'
        classpath 'io.fabric.tools:gradle:1.24.4'
    }
}

allprojects {
    repositories {
        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://maven.google.com"
        }
        google()
    }
}

android/app/build.gradle

import com.android.build.OutputFile

project.ext.envConfigFiles = [
        debug: ".env",
        staging: ".env.staging",
        release: ".env.production",
]
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"


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

apply from: '../../node_modules/react-native/react.gradle'
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
apply from: '../../node_modules/react-native-vector-icons/fonts.gradle'

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

/**
 * 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 {
    dexOptions {
        javaMaxHeapSize "4g"
    }

    lintOptions {
        // checkReleaseBuilds false
        // // Or, if you prefer, you can continue to check for errors in release builds,
        // // but continue the build even when errors are found:
        abortOnError false
    }

    compileSdkVersion 26

    defaultConfig {
        applicationId 'com.primarybid.prod'
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 10708
        versionName '2.0'
        ndk {
            abiFilters 'armeabi-v7a', 'x86'
        }
        resValue 'string', 'build_config_package', 'com.primarybid.prod'
    }

    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
        staging {
            if (project.hasProperty('STAGING_STORE_FILE')) {
                storeFile file(STAGING_STORE_FILE)
                storePassword STAGING_STORE_PASSWORD
                keyAlias STAGING_KEY_ALIAS
                keyPassword STAGING_KEY_PASSWORD
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include 'armeabi-v7a', 'x86'
        }
    }
    buildTypes {
        debug {
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "false"]
            applicationIdSuffix '.debug'
        }
        staging {
            applicationIdSuffix '.staging'
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "false"]
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.staging
        }
        release {
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "true"]
            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]
            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-config')
    implementation project(':react-native-gesture-handler')
    implementation project(':appcenter-crashes')
    implementation project(':appcenter-analytics')
    implementation project(':appcenter')
    implementation project(':react-native-code-push')
    implementation project(':react-native-share')
    implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true
    }
    implementation project(':react-native-bgimage')
    implementation project(':react-native-splash-screen')
    implementation project(':react-native-svg')
    implementation project(':react-native-i18n')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-keychain')
    implementation(project(':react-native-firebase')) {
        transitive = false
    }

    // Firebase dependencies
    implementation 'com.google.android.gms:play-services-base:12.0.1'
    implementation 'com.google.firebase:firebase-core:12.0.1'
    implementation 'com.google.firebase:firebase-crash:12.0.1'
    implementation 'com.google.firebase:firebase-messaging:12.0.1'
    implementation 'me.leolin:ShortcutBadger:1.1.21@aar'

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'

    implementation 'com.facebook.react:react-native:+'  // From node_modules
    implementation project(path: ':worldpayLibrary')
}

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

android/gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

android/settings.gradle

rootProject.name = 'PrimaryBid'
include ':react-native-config'
project(':react-native-config').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-config/android')
include ':react-native-gesture-handler'
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')
include ':appcenter-crashes'
project(':appcenter-crashes').projectDir = new File(rootProject.projectDir, '../node_modules/appcenter-crashes/android')
include ':appcenter-analytics'
project(':appcenter-analytics').projectDir = new File(rootProject.projectDir, '../node_modules/appcenter-analytics/android')
include ':appcenter'
project(':appcenter').projectDir = new File(rootProject.projectDir, '../node_modules/appcenter/android')
include ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
include ':react-native-share'
project(':react-native-share').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-share/android')
include ':react-native-keychain'
project(':react-native-keychain').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-keychain/android')
include ':react-native-bgimage'
project(':react-native-bgimage').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-bgimage/android')
include ':react-native-splash-screen'
project(':react-native-splash-screen').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-splash-screen/android')
include ':react-native-svg'
project(':react-native-svg').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-svg/android')
include ':react-native-i18n'
project(':react-native-i18n').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-i18n/android')
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')
include ':react-native-firebase'
project(':react-native-firebase').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-firebase/android')
include ':worldpayLibrary'
include ':app'

Most helpful comment

Okay, this was easily resolved.

I just had to move this line to be the first line apply plugin: 'com.android.application'

It needs to be applied before the dotEnv.gradle plugin.

Now my android/app/build.gradle file looks like this

apply plugin: 'com.android.application'
import com.android.build.OutputFile

project.ext.envConfigFiles = [
        debug: ".env",
        staging: ".env.staging",
        release: ".env.production",
]
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

All 2 comments

Okay, this was easily resolved.

I just had to move this line to be the first line apply plugin: 'com.android.application'

It needs to be applied before the dotEnv.gradle plugin.

Now my android/app/build.gradle file looks like this

apply plugin: 'com.android.application'
import com.android.build.OutputFile

project.ext.envConfigFiles = [
        debug: ".env",
        staging: ".env.staging",
        release: ".env.production",
]
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

same issue here

Was this page helpful?
0 / 5 - 0 ratings