'How Do I Solve Android Gradle Plugin Error On Android Studio 3.1.1?

I upgraded Android Gradle plugin to version 3.1.1 and Gradle to Version 4.4. I got the error. How can I solve it?. Please, Anyone.

Could not find method flavorDimension() for arguments [market] on ProductFlavor_Decorated{name=google, dimension=null, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=null, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}, mWearAppUnbundled=null} of type com.android.build.gradle.internal.dsl.ProductFlavor. Open File

My (OLD) build.gradle code.

apply plugin: 'com.android.application'

    android {
        signingConfigs {
            config {
                keyAlias 'XXXXXX'
                keyPassword 'XXXXXX'
                storeFile file('../XXXXXX.jks')
                storePassword 'XXXXXX'
            }
        }

        compileSdkVersion 26
        buildToolsVersion "26.0.3"

        defaultConfig {
            applicationId 'com.XXX.XXX'
            minSdkVersion 16
            targetSdkVersion 27
            versionCode 5
            versionName "5.0.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.config
            }
            debug {
                signingConfig signingConfigs.config
            }
        }

        productFlavors {
            dev {
                // Enable pre-dexing to produce an APK that can be tested on
                // Android 5.0+ without the time-consuming DEX build processes.
                minSdkVersion 21
            }
            prod {
                // The actual minSdkVersion for the production version.
                minSdkVersion 16
            }
        }
    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })

        // my lib
        compile project(':cdroid')
        compile project(':videoview')

        compile 'com.android.support:appcompat-v7:25.3.1'
        compile 'com.android.support:support-v4:25.3.1'
        compile 'com.squareup.okhttp:okhttp:2.5.0'
        compile 'org.jsoup:jsoup:1.10.1'
        compile 'com.android.support:recyclerview-v7:25.3.1'
        compile 'com.android.support:cardview-v7:25.3.1'
        compile 'com.squareup.picasso:picasso:2.5.2'
        compile 'com.google.firebase:firebase-ads:10.2.4'
        compile 'nz.co.delacour.exposure-core:exposurevideoplayer:1.0.3'
        compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
        compile 'fm.jiecao:jiecaovideoplayer:5.5.4'
        compile 'com.android.support:design:25.3.1'
        compile 'com.android.support:multidex:1.0.1'
        testCompile 'junit:junit:4.12'
    }

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

(I still see the error) I modified my build.gradle to

apply plugin: 'com.android.application'

    android {
        signingConfigs {
            config {
                keyAlias 'XXXXX'
                keyPassword 'XXXXX'
                storeFile file('../XXXXX.jks')
                storePassword 'XXXXX'
            }
        }

        compileSdkVersion 27
        buildToolsVersion "27.0.3"

        defaultConfig {
            applicationId 'com.XXXXX.XXXXX'
            minSdkVersion 16
            targetSdkVersion 27
            versionCode 5
            versionName "5.0.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.config
            }
            debug {
                signingConfig signingConfigs.config
            }
        }

        flavorDimensions "minApi"

        productFlavors {
            dev {
                // Enable pre-dexing to produce an APK that can be tested on
                // Android 5.0+ without the time-consuming DEX build processes.
                minSdkVersion 21
                dimension "minApi"
            }
            prod {
                // The actual minSdkVersion for the production version.
                minSdkVersion 16
                dimension "minApi"
            }
        }
    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })

        // my lib
        compile project(':cdroid')
        compile project(':videoview')



        compile 'com.android.support:appcompat-v7:27.1.1'
        compile 'com.android.support:support-compat:27.1.1'
        compile 'com.squareup.okhttp:okhttp:2.5.0'
        compile 'org.jsoup:jsoup:1.10.1'
        compile 'com.android.support:recyclerview-v7:27.1.1'
        compile 'com.android.support:cardview-v7:27.1.1'
        compile 'com.squareup.picasso:picasso:2.5.2'
        compile 'com.google.firebase:firebase-ads:15.0.0'
        compile 'nz.co.delacour.exposure-core:exposurevideoplayer:1.0.3'
        compile 'com.android.support.constraint:constraint-layout:1.1.0'
        compile 'fm.jiecao:jiecaovideoplayer:5.5.4'
        compile 'com.android.support:design:27.1.1'
        compile 'com.android.support:multidex:1.0.3'
        testCompile 'junit:junit:4.12'
    }

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

My New Error iS

Unable to load class 'org.gradle.api.internal.component.Usage'. Possible causes for this unexpected error include:

  • Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network)
  • The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. Stop Gradle build processes (requires restart)
  • Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.


Solution 1:[1]

As of Android Gradle plugin version 3.0, all flavors must be assigned to a flavor dimension.

flavorDimensions "minApi"

productFlavors {
    dev {
        // Enable pre-dexing to produce an APK that can be tested on
        // Android 5.0+ without the time-consuming DEX build processes.
        minSdkVersion 21
        dimension "minApi"
    }
    prod {
        // The actual minSdkVersion for the production version.
        minSdkVersion 16
        dimension "minApi"
    }
}

Also, you should be using the latest tools version and you should update various other parts of your build.gradle file (for instance, compile is deprecated and should be replace with implementation or, sometimes, api) For more information, see Migrate to Android Plugin for Gradle 3.0.0.

Solution 2:[2]

Just change buildToolsVersion of build.gradle(app) and set it to '27.0.3' sync the gradle.

buildToolsVersion '27.0.3'

Solution 3:[3]

the gradle 4.4 required flavour dimension, define default flavour dimension in you app build.gradle

android { 
    ...
    flavorDimensions "default"
    ...
}

Solution 4:[4]

I had the same issue. But after some research I came up with the code below, everything working fine now. Hopefully, this might helpful to someone else.

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion flutter.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.APPNAME.androidapp"
        minSdkVersion 21 //flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }

    flavorDimensions "default"
    productFlavors {
        production {
            dimension "default"
            resValue "string", "app_name", "APPNAME"
            applicationIdSuffix ""
        }
        staging {
            dimension "default"
            resValue "string", "app_name", "APPNAME Stg"
            applicationIdSuffix ".stg"
        }

        development {
            dimension "default"
            resValue "string", "app_name", "APPNAME Dev"
            applicationIdSuffix ".dev"
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ted Hopp
Solution 2 Ghulam Moinul Quadir
Solution 3 Ravindra-Ravi Verma
Solution 4 happycoder