1

First time using product flavors and I'm not sure if I need to put an exact duplicate of everything in each product folder eg. src/flavor1/com/app/pro/company/ "all my java files". I have the same file structure for free version. I try this and I get the "Duplicate class found in the file "C:\blah\blah\Settings.java" error. I setup a bool in the gradle to use for the separating free vs. paid version in the file mentioned. Any advice from here? Is there anyway to use one java file for both versions?

I'm using buildConfigField("boolean", "demomode", "false") in the gradle.

Thanks in advance!

abotrix
  • 138
  • 1
  • 12

1 Answers1

2

If a Java file is shared between all flavors in a flavor dimension, then you can keep a single copy in your main source set. This reduces duplication!

If any flavor in a flavor dimension requires a customized version of a specific Java file, then you must remove the copy of that same Java file from your main source set, and place appropriately customized versions in every flavor-specific source set for that flavor dimension. You can't have flavor specific copies of the Java file "override" the main copy.

Based on your specific error, it sounds like either

  1. paid and free are not part of the same flavor dimension, or
  2. you still have a copy of your Java files in your main source set (usually located in src/main).

If case 1, make sure both flavors are part of the same dimension.

If case 2, delete the copies of any files that are customized per-flavor from the main source set, and delete the copies of any files that are shared between flavors from the flavor-specific source sets (leaving them in the shared source set main). This may then allow you to avoid defining the BuildConfig boolean and leverage the fact that you have flavor-specific classes to implement the differences between free and paid modes.

See the docs for more background on source sets.

stkent
  • 19,772
  • 14
  • 85
  • 111
  • I see. So if I need separate java file for either or, then put it in the appropriate flavored folder and delete from main. But if everything is perfect the way it is now with the boolean in place, how do I compile without it complaining about package name not being right? It uses the src/main package name, not the paid and free package names when I pick which version to compile. No other java files in the paid and free folders. – abotrix Apr 06 '18 at 22:06
  • If the boolean is your preferred way of differentiating between the free and paid versions, then yes, you can not worry about flavor-specific source sets and just keep everything in `src/main`. In that case there should be no conflict as there is only one copy of every Java file. – stkent Apr 06 '18 at 23:06
  • 1
    Thank you very much for your help! I'll report back. – abotrix Apr 07 '18 at 02:51
  • Ok, I'm having issues. Just having resources folders for different icons for different flavors and I compile, I try to access the SettingsActivity class but it says, "Application is not installed on your phone." It's trying to access the default package name in manifest. So do I have to manually change the package name when compiling, or add something? Gradle for pro (same with free with free name): applicationId 'com.company.appname.pro' versionCode 1 versionName '1.0' versionNameSuffix '-pro' App manifest: package="com.company.appname" – abotrix Apr 08 '18 at 20:38
  • That is a separate question from the original, so I'll give some thoughts but you'd need to open a new question for more extended discussion. Your manifest package name should be computed based on what is set in the build.gradle file, so no need to change that manually. You probably want to replace `versionNameSuffix` with `applicationIdSuffix` unless I am misunderstanding what you're looking to achieve. – stkent Apr 08 '18 at 21:54
  • Ok. Will make another thread. I have tried applicationIdSuffix, app starts, but when just accessing the activity, it give that error. Thanks for your help. – abotrix Apr 08 '18 at 23:32
  • https://stackoverflow.com/questions/49723912/application-not-installed-on-your-phone-when-accessing-activity-using-product – abotrix Apr 08 '18 at 23:53