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

时间:2023-02-02
本文介绍了为同一类的不同版本构建风味的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

flavorA 将完全使用 main 中的源和资产,而 flavorA2classA 中有一些小的变化,并且classB,包名也改为com.abc.flavorA.mk2.

flavorA will use the source and assets from main completely while flavorA2 has some small changes in classA and classB and the package name is also changed to com.abc.flavorA.mk2.

我有这样的 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"
        }
    }
...

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

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

我错过了什么吗?

推荐答案

因为你有 2 个不同包下的类,所以它们是完全不同的类.所以这些类不会互相替换.

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

使用风味,您不能覆盖类文件.因此,实现您想要的一种方法是将这些类移出 main,并移入 flavorA.

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.

所以你会有这样的东西:

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

这样,每当您选择一种风味时,只会看到 ClassA 和 ClassB 的一个版本.

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

这篇关于为同一类的不同版本构建风味的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何读取 build.gradle 中 local.properties 中定义的属性 下一篇:如何在 android studio 中使用库项目

相关文章

最新文章