Use different resources for different application flavors using gradle

痞子三分冷 提交于 2019-11-29 17:05:05

问题



I have an android application and I would like to have different flavors. Specifically I would like to have 2 flavors and for each flavor use different strings (different strings.xml file) and maybe have some icons different.

I have tried creating two folders in the project's root folder: flav1 and flav2 and used the following build.gradle

android {
    compileSdkVersion "Google Inc.:Google APIs:15"
    buildToolsVersion "17.0.0"

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

        instrumentTest.setRoot('tests')
    }

    productFlavors {
        flav1 {
            packageName "com.ic.flav1"
        }

        flav2 {
            packageName "com.ic.flav2"
        }
    }

    android.sourceSets.flav2 {
        res {
            srcDir 'flav2'
        }
        resources {
            srcDir 'flav2'
        }
    }
    android.sourceSets.flav1 {
        res {
            srcDir 'flav1'
        }
        resources {
            srcDir 'flav1'
        }
    }
}

The result of this is that none of the strings is recognized, getting multiple errors of the following type:

build FAILED :

error: Error: No resource found that matches the given name (at 'contentDescription' with value '@string/txt_addr').

Am I missing something? How should the build.gradle be?

Thanks


回答1:


The solution was to add directory

values

under each corresponding res folder, and all the strings were recognized.



来源:https://stackoverflow.com/questions/17103601/use-different-resources-for-different-application-flavors-using-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!