【问题标题】:Using alternate resources with Build Flavors in Gradle在 Gradle 中通过 Build Flavors 使用替代资源
【发布时间】:2014-10-24 20:44:08
【问题描述】:

我已阅读官方文档以及此处的其他几个问题,无论我尝试什么,我的备用资源都不会加载。请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">

【问题讨论】:

    标签: android gradle android-gradle-plugin build.gradle


    【解决方案1】:

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

    应该是app/src/flavor1/res

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

    注意:这可能是一个错字,但您的代码应该是 productFlavors { 而不是 productFlavor {

    【讨论】:

    • 这就是我现在设置的方式,它似乎适用于我的 strings.xml,但不适用于 drawables 文件夹。结构看起来像:app/src/flavor/res/drawable/ic_launcher.png 但即使我已经从项目中删除了 ic_launcher 的所有其他替代资源,所有风格仍然使用默认的 android 图标而不是我的自定义资产加载在风味文件夹中提供。对那里可能出现的问题有任何见解吗?
    • @swilkeni 发布您的文件夹结构。图片会很好。
    • @swilkeni 你的真实 build.gradle 碎片也请。
    • 请查看我对问题的编辑,build.gradle 已粘贴到其中
    • @swilkeni 你得到一个错误,或者drawable根本没有使用?
    猜你喜欢
    • 2016-09-24
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多