本文介绍了三种产品口味:两种非常相似,一种较小但不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在 gradle 中有两个 productFlavors.我需要添加第三个.两种现有的产品口味几乎共享所有内容.称它们为猫和狗——它们都是动物.我需要添加的第三种风格本质上是该应用程序的沙盒版本.我们可以称之为自行车.

假设我的应用现在有 12 项活动,全部由猫和狗共享.但是 Bike 产品风格应该只能访问其中三个活动,并且 Bike 需要拥有自己的启动器活动.如何重构我的代码以智能地适应这种情况?同样,两种口味几乎可以共享所有东西,而一种口味与其他两种口味的共享要少得多.

更新

似乎有一种聪明的方法可以使用

由于 MainActivity 很常见,所以只在主要风格中提及.

Right now I have two productFlavors in gradle. I need to add a third one. The two existing product flavors share pretty much everything. Call them Cat and Dog — they are both animals. The third flavor I need to add is essentially a sandboxed version of the app. We might call it Bike.

Say my app now has twelve activities all shared by Cat and Dog. But the Bike product flavor should only have access to three of those Activities and Bike needs to have it’s own launcher activity. How do I refactor my code to accommodate this intelligently? Again, two flavors share pretty much everything, while one flavor share much less with the other two.

UPDATE

There seems to have a smart way to solve this using Change default source set configurations. Basically, I would keep the /main sourceSet for all that is common over the whole app; a /dogCat sourceSet for all that is common to both Dog and Cat; and a /bike sourceSet for what belongs only to Bike.

Having figured out so much, I am still having some problems writing the gradle sourceSets portion

android {
  ...
  sourceSets {
    . . .
  }
}
解决方案

Keep all the common classes in the main flavor. For Example, you have three flavors, Cat, Dog and Bike. In these three flavors, Cat and Dog are mostly same, except some. On the other hand Bike is also having some Classes which is common.

Three Scenarios:

01. When all flavor have common functionality

Like, Cat, Dog and Bike all have one class which is named as PriceInformation. Then keep this class in the main flavor.

02. When Cat and Dog have same functionality but Bike don't.

Like, Cat and Dog have a common functionality called LifeSpan, then keep this class in this flavor only.

03. When only Bike have common functionality, but the other two flavor don't.

Then keep that particular class only in Bike Flavor.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.productflavor"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {

        cat {
            applicationId "com.cat.theme"
        }
        dog {
            applicationId "com.dog.theme"
        }
        bike {
            applicationId "com.bike.theme"
        }
    }
}

Since, MainActivity is common then mention only in main flavor.

这篇关于三种产品口味:两种非常相似,一种较小但不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 22:42