在build.grade(Module:app)中添加productFlavor给出错误

时间:2017-08-10 12:16:13

标签: android build.gradle android-productflavors

我在build.gradle(Module:app)中添加以下内容:

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'

}

给出错误:(22,0)找不到方法productFlavors()对项目参数[build_bqh9qnip9k7nqy2kbpova0vtq $ _run_closure2 @ 5b30b3a7] ':应用'。类型org.gradle.api.Project的

我是否遗漏了在build.gradle中添加的任何内容(模块:app)或者我是否需要向build.gradle添加任何内容(Project:NewProject)

2 个答案:

答案 0 :(得分:1)

productFlavors标记移到android个大括号内,它必须是buildTypes的兄弟。

您的 build.gradle 最终将会是这样的:

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"'
        }
    }
}

答案 1 :(得分:0)

按照以下步骤创建构建风格:

productFlavors {

TestFlavourType {
                applicationId "com.test.flavour"
                minSdkVersion 17
                targetSdkVersion 25
                versionCode 3
                versionName "1.02"
                multiDexEnabled true
                }

定义要从不同文件夹中选择的源集:

sourceSets {
        main { //Main
            manifest.srcFile 'src/main/AndroidManifest.xml' - picks from Main
            java.srcDirs = ['src/main/java']
            resources.srcDirs = ['src/main/java']
            aidl.srcDirs = ['src/main/java']
            renderscript.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }               

如果你想维护不同的布局/样式/字符串,你需要定义如下:

TestFlavourType {
            res.srcDirs = ['src/TestFlavourType/res-TestFlavourType', 'src/CommonLayoutDir/res']
            // For resources it will look for the files in this path [src/TestFlavourType/res-TestFlavourType] else it will look in to this [src/CommonLayoutDir/res]
            assets.srcDirs = ['src/TestFlavourType/assets', 'src/main/assets']
        }       
}       

}//productFlavors end

在Src目录下创建一个文件夹来维护不同风格的单独布局文件。如果有帮助,请告诉我。