简体   繁体   中英

Flutter flavor sourceSet resource not refer flavor folder

In my Flutter mobile app have two flavors with separate res folders for each. I have added sourceSet in build.gradle file, but at runtime the running flavor does not show the relavant resource from flavor folder, instead the main folder resources.

Folder structure as below.

src
|
flavor1
------res(with sub folders)
|
flavor2
------res(with sub folders)
|
main
------res(with sub folders)

build.gradle file changes.

android {
    sourceSets {
        main {
            java.srcDirs += 'src/main/kotlin'
            res.srcDirs = ['src/main/res']
            manifest.srcFile 'src/main/AndroidManifest.xml'
        }
        flavor1 {
             res.srcDirs = [
                'src/flavor1/res/drawable-hdpi',
                'src/flavor1/res/drawable-ldpi',
                'src/flavor1/res/drawable-mdpi',
                'src/flavor1/res/drawable-v21',
                'src/flavor1/res/drawable-xhdpi',
                'src/flavor1/res/drawable-xxhdpi',
                'src/flavor1/res/drawable-xxxhdpi',
                'src/flavor1/res/drawable',
                'src/flavor1/res/mipmap-hdpi',
                'src/flavor1/res/mipmap-mdpi',
                'src/flavor1/res/mipmap-xhdpi',
                'src/flavor1/res/mipmap-xxhdpi',
                'src/flavor1/res/mipmap-xxxhdpi',
                'src/flavor1/res/raw',
                'src/flavor1/res/values',
                'src/flavor1/res'
                ]
            manifest.srcFile 'src/flavor1/AndroidManifest.xml'
        }
        flavor2 {
            res.srcDirs = [
                'src/flavor2/res/drawable-hdpi',
                'src/flavor2/res/drawable-ldpi',
                'src/flavor2/res/drawable-mdpi',
                'src/flavor2/res/drawable-v21',
                'src/flavor2/res/drawable-xhdpi',
                'src/flavor2/res/drawable-xxhdpi',
                'src/flavor2/res/drawable-xxxhdpi',
                'src/flavor2/res/drawable',
                'src/flavor2/res/mipmap-hdpi',
                'src/flavor2/res/mipmap-mdpi',
                'src/flavor2/res/mipmap-xhdpi',
                'src/flavor2/res/mipmap-xxhdpi',
                'src/flavor2/res/mipmap-xxxhdpi',
                'src/flavor2/res/raw',
                'src/flavor2/res/values',
                'src/flavor2/res'
                ]
            manifest.srcFile 'src/flavor2/AndroidManifest.xml'
        }
    }

    // Because of flavors and multiple res folders.
    afterEvaluate {
        android.applicationVariants.all { variant ->
            tasks.named("generate${variant.name.capitalize()}ResValues")
            .configure { task ->
                 task.outputs.upToDateWhen {false}
            }
        }
    }

    flavorDimensions "app"

    productFlavors {
        flavor1 {
            dimension "app"
            applicationIdSuffix ""
        }
        flavor2 {
            dimension "app"
            applicationIdSuffix ".flavor2"
        }
    }
}

Running the flavor2 app still refers to main resources is the issue. I want the respective flavor to refer only the given flavor specific resource folder in runtime.

sourceSet in not necessary, just add product flavors within app/build.gradle , folders structure is ok, at compilation time all resources are merged and main folder resources are overridden with flavor resources

just run next command

flutter run --flavor flavor2

more info here: https://medium.com/@salvatoregiordanoo/flavoring-flutter-392aaa875f36

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM