There is an application N, it has two versions - paid and free, each version has its own ID and its own functionality.

Suppose I need to make a change in 5-7 classes, add them with something in both versions.

What is the best and most convenient way to do this?

ps Now I am writing in one version, then I copy it with pens to another.

3 answers 3

Use Flavors.

Recipe for Android Studio:

  1. Open File-Project Structure-Flavors. There will already be defaultConfig. Create two flavor'a: paid and free. Specify for each of them the ApplicationId. The rest of the data is optional, they will be taken from the defaultConfig (perhaps the signing config may be different, then it may or may not be specified, depending on your project).
  2. Now for each flavor you can create your own classes and resources, they will be combined with the existing code (on top). These files will be located next to main in the src directory. For example:

    myproject app src main java res assets ... paid java res ... free res ... 

    2.1. The directory structure itself is not created; you will have to do it manually. Although for resources, for example, you can select a sourceSet (by default, there is main, but you can choose any one you need) and then the structure and files (if necessary) are created.

  3. Open the Build Variants studio in the lower left and select the launch option you want there. For example, Module: app -> Build Variant: paidDebug. Switch to freeDebug, the studio itself will rebuild everything.
  4. Profit

Related Links:

  • Thanks for the detailed answer. - researcher

When it comes to code reuse, it’s very convenient to split the project into several independent modules . Then, you will actually have two unique applications using the same libraries.

Partitioning can be implemented, for example, on the basis of a version control system (for example, Git ). At the same time, you will have three (or more) repositories in which the code of paid and free applications will be stored, as well as shared libraries. Communication between repositories can be implemented (in the case of Git) through submodules .

When updating common classes, you change their source code, send changes to the repository, update dependencies in applications, and preserve them without manually copying / pasting the code.

  • This is certainly a working way, but still it is much more convenient and more correct to use the specialized Flavors mechanism, which is designed to solve just such problems (divide the code into common and individual parts). It is much more flexible and convenient in this task. - pavlofff
  • @pavlofff, you're right, but in my opinion, the more alternative ways to solve the problem, the better :) - Dmitriy Simushev

The easiest way to implement this is through build.gradle in Android Studio. Read about productFlavors . Add the following lines to build.gradle:

 productFlavors { lite { packageName = 'com.project.test.app' versionCode 1 versionName '1.0.0' } pro { packageName = 'com.project.testpro.app' versionCode 1 versionName '1.0.0' } } 

In this example, I added two types of applications: the lite version and the pro version. Each version has its own versionCode and versionName (for publication on Google Play).

And in the code, you can set separate logic by checking the variable BuildConfig.FLAVOR:

 if (BuildConfig.FLAVOR == "lite") { // ΠΊΠΎΠ΄ отобраТСния Ρ€Π΅ΠΊΠ»Π°ΠΌΡ‹ ΠΈΠ»ΠΈ ΠΏΡ€ΠΎΡ‡ΠΈΠ΅ ограничСния Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΎΠ½Π°Π»Π° } 

To launch and test various application logic options on the device, simply change the type of the application being launched on the "Build Variants" tab in Android Studio. The Build Variants list is automatically generated based on the productFlavors specified in build.gradle:

enter image description here

  • if (BuildConfig.FLAVOR == "lite") { - looks suspiciously like checking in runtime. When is this check performed? - Qwertiy ♦
  • Thank you for such a detailed answer! - researcher
  • @Qwertiy hmm .. what's wrong with that? - researcher
  • @Qwertiy Directly in code. For example, at some activity you need to show a block of ads. Or the menu does not display part of the items. Of course, if the structure of the version code is significantly different, then the option suggested by Yura Ivanov in item 2 with the breakdown of directories will work better. - Denis
  • @researcher 1. Overhead on execution. 2. The user is given the full version. It (theoretically) can decompile the code: either for its own purposes or for the sake of overriding this check and getting the full version. 3. Probably an increase in the size of the program, since it contains both versions. // In theory, all this should be decided not by run-time checks, but by conditional compilation, i.e., instead of if (...) use #if ... directives #if ... or their equivalents for the language you use. By the way, here is an interesting article on the topic . - Qwertiy ♦ 1:16 pm