Android 为同一类的不同版本构建风味

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23698863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:21:41  来源:igfitidea点击:

Build flavors for different version of same class

androidandroid-gradle-plugin

提问by dumbfingers

I've got a project, structured like this:

我有一个项目,结构如下:

project/
   |
   |---src/
        |---flavorA2/
        |      |
        |      |---java/
        |      |     |---com.abc.flavorA.mk2
        |      |                 |-----classA.java
        |      |                 |-----classB.java
        |      |---res/
        |      |---AndroidManifest.xml
        |
        |---main
        |      |---java/
        |      |     |---com.abc.flavorA
        |      |                 |-----classA.java
        |      |                 |-----classB.java
        |      |                 |-----classC.java
        |      |                 |-----classD.java
        |      |---res/
        |      |    |---drawable/
        |      |    |---layout/
        |      |    |---values/
        |      |         
        |      |---AndroidManifest.xml
        |
        |---flavorA

flavorAwill use the source and assets from maincompletely while flavorA2has some small changes in classAand classBand the package name is also changed to com.abc.flavorA.mk2.

flavorA将完全使用源和资产,main同时flavorA2classA和 中classB进行了一些小的更改,并且包名称也更改为com.abc.flavorA.mk2

I had the build.gradlefile like this:

我有这样的build.gradle文件:

...
buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
        flavorA2 {
            packageName "com.abc.flavorA.mk2"
            versionCode 2
            versionName "1.0.1"
        }

        flavorA {
            packageName "com.abc.flavorA"
        }
    }
...

I run the code by selecting the build variant to flavorA2. However the running results shows that the gradle still choose the classes (classAand classB) from maininstead of using the changed version inside flavorA2.

我通过选择构建变体来运行代码flavorA2。但是运行结果显示 gradle 仍然选择类(classAclassB),main而不是使用内部更改的版本flavorA2

Am I missing something here?

我在这里错过了什么吗?

回答by Hassan Ibraheem

Since you have the classes under 2 different packages, these are totally different classes. So the classes aren't replacing each other.

由于您在 2 个不同的包下拥有这些类,因此它们是完全不同的类。所以这些类不会相互替换。

With flavors, you can't override class files. So, one way to accomplish what you want is move these classes out of main, and into flavorA.

使用口味,您不能覆盖类文件。因此,一个方式来完成你想要的是将这些班列的main,和成flavorA

So you would have something like this:

所以你会有这样的事情:

project/
   |
   |---src/
        |---flavorA2/
        |      |
        |      |---java/
        |      |     |---com.abc
        |      |                 |-----classA.java
        |      |                 |-----classB.java
        |      |---res/
        |      |---AndroidManifest.xml
        |
        |---main/
        |      |---java/
        |      |     |---com.abc.flavorA
        |      |                 |-----classC.java
        |      |                 |-----classD.java
        |      |---res/
        |      |    |---drawable/
        |      |    |---layout/
        |      |    |---values/
        |      |         
        |      |---AndroidManifest.xml
        |
        |---flavorA/
        |      |---java/
        |      |     |---com.abc
        |      |                 |-----classA.java
        |      |                 |-----classB.java

This way, whenever you pick a flavor, only one version of ClassA and ClassB will be visible.

这样,无论何时您选择一种口味,都只会看到 ClassA 和 ClassB 的一个版本。

回答by Scott Barta

In the main build variant, Class A is com.abc.flavorA.classA, and in flavorA2it's com.abc.flavorA.mk2.classA. These are two different fully-qualified class names and therefore two different classes.

在主要构建变体中,A 类是com.abc.flavorA.classA,在flavorA2它的com.abc.flavorA.mk2.classA. 这是两个不同的完全限定类名,因此是两个不同的类。

You can't really override entire classes in a flavor. Depending on what you want to do, you might want to look into the BuildConfigmechanism -- in short, this is a class that's generated by the build system which can have values or statements that vary depending on the build type and flavor. You can have runtime code that looks at constants in that class and varies its behavior.

您不能真正以一种风格覆盖整个类。根据您想要做什么,您可能想要查看BuildConfig机制——简而言之,这是一个由构建系统生成的类,它可以具有根据构建类型和风格而变化的值或语句。您可以拥有查看该类中的常量并改变其行为的运行时代码。

See Android Studio Update 0.4.0 could not find buildConfig()for more information on the syntax, but in brief, it looks like this:

有关语法的更多信息,请参阅Android Studio 更新 0.4.0 找不到 buildConfig(),但简而言之,它如下所示:

productFlavors {
    flavor {
      buildConfigField "boolean", "MY_FLAG", "true"
    }
}

回答by ashishduh

You need to specify sourceSetsin your build file. You need to modify your directory structure to make it so that only the folder names are different, everything under the java directory should be the same, so remove the mk2from the class name. I'm not sure if the syntax is entirely correct but it should look like this:

您需要sourceSets在构建文件中指定。您需要修改您的目录结构以使其仅文件夹名称不同,java目录下的所有内容都应该相同,因此mk2从类名称中删除。我不确定语法是否完全正确,但它应该是这样的:

android {
    sourceSets {
        flavorA {
            java {
                srcDirs = ['src/flavorA/java']
            }
        }

        flavorA2 {
            java {
                srcDirs = ['src/flavorA2/java']
            }
        }
    }
}