Search code examples
androidbuild.gradleandroid-productflavors

adding productFlavors in build.grade(Module:app) giving error


i am adding in build.gradle(Module:app) the following:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        applicationId "com.sis.newpro"
        minSdkVersion 22
        targetSdkVersion 25
        testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
    }

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

    }
}

productFlavors {
    prod {
        buildConfigField 'String', 'URL', '"http://api.abcd.com"'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

}

giving Error:(22, 0) Could not find method productFlavors() for arguments [build_bqh9qnip9k7nqy2kbpova0vtq$_run_closure2@5b30b3a7] on project ':app' of type org.gradle.api.Project.

do i missing any thing to add in build.gradle(Module:app) or do i need to add any thing to build.gradle(Project:NewProject)


Solution

  • Move your productFlavors tag inside the android braces, it needs to be a sibling of buildTypes.


    Your build.gradle would end up being something like this:

    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.3"
    
        defaultConfig {
            applicationId "com.sis.newpro"
            minSdkVersion 22
            targetSdkVersion 25
            testInstrumentationRunner
            "android.support.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'),
                        'proguard-rules.pro'
            }
    
        }
        productFlavors {
            prod {
                buildConfigField 'String', 'URL', '"http://api.abcd.com"'
            }
        }
    }