如何根据释放/调试来设置不同产品口味和不同图标的Gradle



我想根据产品味道具有不同的图像,如果调试或发行版,那么实现此目的的RES文件夹的适当设置是什么?

在我的build.gradle中:

productFlavors {
    flavor1 {
        applicationId   "com.myapp.flavor1"
    }
    flavor2 {
        applicationId   "com.myapp.flavor2"
    }
}
buildTypes {
    debug {
        applicationIdSuffix '.debug'
        useProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    release {
        minifyEnabled true
        shrinkResources true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

在我的文件夹结构中,我现在有:

src/flavor1/res
   /flavor2/res
   /main/res
   /debug/res

我尝试在调试文件夹中使用Floation1和Floation2作为具有资源的子文件夹,但这并不适用。在调试中只有一个RES文件夹会产生两种口味的相同资产,而我希望它们与众不同。那么如何设置该项目,因此根据调试/发布和不同产品的口味,我的图像不同?

编辑:

更新的文件夹结构

来自android build build variants docs,

gradle会根据您的构建类型和产品口味自动创建构建变体,并根据product-flavor Build-Type

命名它们

因此,在您的情况下,您已经有4个变体。而且,如果您想要其他源集,则可以如下 src/flavor1Debug src/flavor2Release等。注意命名约定

这实际上比您想象的要容易。

您目前有:

src/flavor1/res
   /flavor2/res
   /main/res
   /debug/res

只是添加:

src/flavor1Debug/res
   /flavor1Release/res
   /flavor2Debug/res
   /flavor2Release/res

其中的资源将覆盖这些文件夹中的资源。

Android Studio中的"构建变体" UI有助于确认最终的可能性。

我本人将这种模式用于Google Maps API键,在我的情况下,该键需要是不同的(风味),并且由于应用程序签名的差异而在发布和调试之间已经有所不同。

您应该尝试此

productFlavors {
            flavor1 {
                    applicationId   "com.myapp.flavor1"
                    }
            flavor2 {
                    applicationId   "com.myapp.flavor2"
                    }
            }
            sourceSets.flavor1 {
                res {
                    srcDir 'flavor1'
                }
                resources {
                    srcDir 'flavor1'
                }
            }
            sourceSets.flavor2 {
                res {
                    srcDir 'flavor2'
                }
                resources {
                    srcDir 'flavor2'
                }
            }
    }

最新更新