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

Build flavors for different version of same class(为同一类的不同版本构建风味)
本文介绍了为同一类的不同版本构建风味的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)