在Gradle中使用Build Flavors的备用资源

时间:2014-10-24 20:44:08

标签: android gradle android-gradle build.gradle

我已经在这里阅读了官方文档以及其他几个问题,无论我尝试什么,我的替代资源永远不会加载。请ELI5!如果我有以下产品风味

productFlavor {
    flavor1 {
        applicationId "com.packagename.flavor1"
    }
}

我究竟在哪里创建项目层次结构中的备用资源文件夹,以及我究竟需要如何更改build.gradle文件以便在构建特定风味时知道查看所述位置?文档使这听起来很容易,但我整个下午一直在尝试不同的变化,没有任何运气。我误解了什么?

编辑:build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'

defaultConfig {
    applicationId "com.example.android"
    minSdkVersion 14
    targetSdkVersion 20
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

productFlavors {
    default_flavor {
        resValue "string", "build_variant", "default"
        resValue "string", "app_title", "Android"

    }
    bengals {
        applicationId "com.example.Bengals"
        resValue "string", "build_variant", "bengals"
        resValue "string", "app_title", "Cincinnati Bengals"
    }

    bills {
        applicationId "com.example.Bills"
        resValue "string", "build_variant", "bills"
        resValue "string", "app_title", "Buffalo Bills"
    }

    boilermakers {
        applicationId "com.example.Boilermakers"
        resValue "string", "build_variant", "boilermakers"
        resValue "string", "app_title", "Purdue Boilermakers"
    }

    cardinals {
        applicationId "com.example.Cardinals"
        resValue "string", "build_variant", "cardinals"
        resValue "string", "app_title", "Cardinals"
    }

    colts {
        applicationId "com.example.Colts"
        resValue "string", "build_variant", "colts"
        resValue "string", "app_title", "Indianapolis Colts"
    }
    cyclones {
        applicationId "com.example.Cyclones"
        resValue "string", "build_variant", "cyclones"
        resValue "string", "app_title", "Cincinnati Cyclones"
    }

    eagles {
        applicationId "com.example.Eagles"
        resValue "string", "build_variant", "eagles"
        resValue "string", "app_title", "Philadelphia Eagles"
    }

    hawkeyes {
        applicationId "com.example.Hawkeyes"
        resValue "string", "build_variant", "hawkeyes"
        resValue "string", "app_title", "Hawkeyes"
    }

    hoosiers {
        applicationId "com.example.Hoosiers"
        resValue "string", "build_variant", "hoosiers"
        resValue "string", "app_title", "Indiana Hoosiers"
    }

    packers {
        applicationId "com.example.Packers"
        resValue "string", "build_variant", "packers"
        resValue "string", "app_title", "Green Bay Packers"
    }

    test_flavor {
        applicationId "com.example.test"
        resValue "string", "build_variant", "test"
        resValue "string", "app_title", "Dolly Test"
    }

    titans {
        applicationId "com.example.Titans"
        resValue "string", "build_variant", "titans"
        resValue "string", "app_title", "Tennessee Titans"
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:20.0.0'
    compile project(':admsAppLibrary')
    compile 'com.google.android.gms:play-services:5.0.89'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':volley')
    compile 'com.google.code.gson:gson:2.2.4'
    compile project(':Minimogrify')
    compile project(':urbanairship-lib-5.0.1')
}

AndroidManifest.xml的相关部分:

<application android:name=".Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

1 个答案:

答案 0 :(得分:2)

确保您在正确的路径中创建了文件夹。

应为app/src/flavor1/res

为了更有意义,您的主要资源位于src/main/res

注意:它可能是拼写错误,但您的代码应该说productFlavors {而不是productFlavor {