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

问题描述

现在我在Gradle中有两个productFlavors。我需要添加第三个。两种现有的产品口味几乎分享一切。叫他们猫和狗 - 他们都是动物。我需要添加的第三个风格实质上是应用程序的沙盒版本。我们可以称它为自行车。



说我的应用程序现在有12项活动全部由Cat and Dog共享。但自行车产品的味道应该只能访问其中的三种活动,而自行车需要有自己的发射器活动。我如何重构我的代码以适应这种智能?再次,两种口味分享几乎所有东西,而一种口味与另外两种口味分享得少得多。

UPDATE



似乎有一个聪明的方法来解决这个问题, a href =https://developer.android.com/studio/build/build-variants.html#sourcesets =noreferrer>更改默认源集配置。基本上,我会保留 / main sourceSet的所有在整个应用程序中常见的东西;一个 / dogCat sourceSet用于Dog和Cat共有的所有内容;和 / bike sourceSet仅适用于Bike。



经过了解,我仍然在编写gradle sourceSets部分时遇到了一些问题。

  android {
...
sourceSets {
。 。 。



解决方案

保留主要口味中的所有常见类。例如,你有三种口味,猫,狗和自行车。在这三种口味中,猫和狗大部分是相同的,除了一些。另一方面,自行车也有一些常见的类别。

三种情景:

01。当所有味道都有共同的功能时,就像Cat,Dog和Bike都有一个名为PriceInformation的类。然后保持这个类的主要味道。

02。当猫和狗有相同的功能,但Bike不会。

只有味道。

03。当只有自行车有共同的功能,但其他两种味道不。

然后保持特定的类只在自行车味。

  apply plugin:'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion 25.0.2
defaultConfig {
applicationIdcom.productflavor
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName1.0
testInstrumentationRunnerandroid.support.test.runner.AndroidJUnitRunner
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt' ),'proguard-rules.pro'
}
}

productFlavors {

cat {
applicationIdcom.cat.theme
}
狗{
applicationIdcom.dog。主题

自行车{
applicationIdcom.bike.theme
}
}
}


因为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