打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
gradle

I would like to convert CommonsWare's comment to an answer. I'll then explain how the final directory setup should look like. I hope this helps out the people stumbling upon this question through search.

Well, you can override resources in flavors. So, have the common one in main/res/layout/ and the flavor-specific one in yourFlavorHere/res/layout/.

So, if the Customization activity's layout file is called activity_customization.xml, you'll leave its common copy shared among the three flavors under src/main/res/layout directory and place the modified layout xml to be used by, say flavorFour, under its corresponding source set directory src/flavorFour/res/layout.

The way this works is that since flavor one to three (unlike flavor four) haven't provided their own versions of activity_customization.xml, they'll inherit the one coming from the main source set.

It's the activity Java class that gets tricky. Another possibility for that is to configure the flavors with the same activity implementation to pull from two source directories: a flavor-specific one and a common one with the common class implementation.

Unlike resources, Java code files are not merged or overridden. So, you can't have Java files with the same fully qualified class name under main as well as in any of your flavor source sets. If you do, you'll receive a duplicate class error.

To resolve this issue, the simplest solution is to move Customization activity out of the main and into each flavor source set. This works because the flavor directories are mutually exclusive (with each other, not with main) hence avoiding the conflict.

But this means three out of the four flavors have a duplicate copy of the activity - a maintenance nightmare - just because one of the flavors required some changes to it. To resolve this issue we can introduce another source directory that keeps just the common code files shared between the three flavors.

So, the build.gradle script would look something like

android {    ...    productFlavors {        flavorOne {            ...        }        flavorTwo {            ...        }        flavorThree {            ...        }        flavorFour {            ...        }    }    sourceSets {        flavorOne.java.srcDir 'src/common/java'        flavorTwo.java.srcDir 'src/common/java'        flavorThree.java.srcDir 'src/common/java'    }}

Notice the use of java.srcDir (and not srcDirs) which adds another Java source directory to the already existing default src/flavorX/java.

Now all we need to do is to drop the common Customization activity file in src/common/java to make it available to the flavors one to three. The modified version required by flavorFour would go under its own source set at src/flavorFour/java.

So, the final project structure would look something like

+ App // module|- src   |- common // shared srcDir      |- java       |- path/to/pkg         |- CustomizationActivity.java // inherited by flavors 1, 2, 3   + flavorOne   + flavorTwo   + flavorThree   + flavorFour      |- java       |- path/to/pkg         |- CustomizationActivity.java // per-flavor activity class      |- res         |- layout            |- activity_customization.xml // overrides src/main/res/layout   |- main      + java      |- res         |- layout            |- activity_customization.xml // inherited by flavors 1, 2, 3
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Eclipse 安装Gradle插件
如何解决Gradle编译Javafaills sun.net.www.protocol.http.n...
Android Studio 添加Assets目录 / 蓝讯
面向初学者的 Android 应用开发基础知识
Android Studio 入门级教程(二):新建一个属于自己的工程并安装Genymotion模拟器
IntelliJ IDEA 13 Brings a Full Bag of Goodies to Android Developers | JetBrains IntelliJ IDEA Blog
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服