Muzurisana 

Google Play Services

  • October 15, 2016

... The Birthday app is split into two separate apps with the free version showing advertisements and the paid version not doing so. Even better, the paid version does not event include the code to serve advertisements or do analytics.

In the last week this separation was gone for a while as I could not figure out at first how to separate the paid version from the free one. The source of the problem was that if you add the GMS play plugin to your gradle configuration you need to have a definition for the Google Services for all flavors. So if you add the services for the free version to enable advertisements, the paid version will not compile unless it is also defined in the google-services.json file. If you do so, the services are added to the paid version automatically.

The question now is, how can you have different app flavors where some of them do not include the Google Play Services? How to solve this problem has been asked already an the question is available in the issue tracker here. There is no answer, just another link to this issue supposedly containing the answer. At the point in time when I read the thread there were 77 messages posted and none of them answered my question. Looking at the official documentation there is also some lack of information on how to do this.

In the end I figured it out myself and the solution is really simple when you remember Gradle scripts are just java code

          // Default is to use the Play Services
          def usePlayServices = true;

           android {
             signingConfigs {
               releasePaid {
                // Do not use the Play Services for the paid version release build
                usePlayServices = false;
                }
               }
             }
          }

          // Apply the plugin only if needed. This way the services are only added to apps needing them
          if (usePlayServices)
            apply plugin: 'com.google.gms.google-services'
          

Basically you just add a flag which is disabled for all flavors where you do not want the Play Services. This way you can leave out the paid version from the google-services.json file and the code compiles without the services being added.

Simple and effective