Hemal Shah Hemal Shah - 6 months ago 54
Java Question

Gradle build failing on different productFlavors

I am developing an application with two flavors as follow:

productFlavors {
free {
applicationId "com.udacity.gradle.builditbigger.free"
versionName "1.0-free"
}
paid {
applicationId "com.udacity.gradle.builditbigger.paid"
versionName "1.0-paid"
}
}


Now I have admob dependency for the free version and want to make the paid version completely ad-free, hence deciding on compiling the dependency only for free version. I tried doing this as follow:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(":showmessage")
testCompile 'junit:junit:4.12'
compile project(path: ':backend', configuration: 'android-endpoints')
compile 'com.android.support:appcompat-v7:23.1.0'
freeCompile 'com.google.android.gms:play-services-ads:8.4.0'}


and when I try to call the gradle task
installPaidDebug
it gives me the following error:


/home/hemal/Desktop/Udacity-android-nd/Build It Bigger/app/build/intermediates/manifests/full/paid/debug/AndroidManifest.xml
Error:(33, 28) No resource found that matches the given name (at 'value' with value '@integer/google_play_services_version').
Error:Execution failed for task ':app:processPaidDebugResources'.
com.android.ide.common.process.ProcessException: Failed to execute aapt


But when I simple add the following line:
compile 'com.google.android.gms:play-services-ads:8.4.0'

the project is build seamlessly.

How do I overcome this?

Answer

As mentioned in the comments you need to move the meta data tag from the manifest in your src/main directory to the manifest in your src/free directory.

<!-- This meta-data tag is required to use Google Play Services. --> 
         <meta-data 
             android:name="com.google.android.gms.version" 
             android:value="@integer/google_play_services_version" /> 

Your free manifest will look something like:

 <?xml version="1.0" encoding="utf-8"?> 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.udacity.gradle.builditbigger" > 


     <!-- Include required permissions for Google Mobile Ads to run --> 
     <uses-permission android:name="android.permission.INTERNET" /> 
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 


     <application 
         android:allowBackup="true" 
         android:icon="@mipmap/ic_launcher" 
         android:label="@string/app_name" 
         android:theme="@style/AppTheme" > 


         <!-- This meta-data tag is required to use Google Play Services. --> 
         <meta-data 
             android:name="com.google.android.gms.version" 
             android:value="@integer/google_play_services_version" /> 


         <activity 
             android:name=".MainActivity" 
             android:label="@string/app_name" > 
             <intent-filter> 
                 <action android:name="android.intent.action.MAIN" /> 


                 <category android:name="android.intent.category.LAUNCHER" /> 
             </intent-filter> 
         </activity> 
         <!-- Include the AdActivity configChanges and theme. --> 
         <activity 
             android:name="com.google.android.gms.ads.AdActivity" 
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
             android:theme="@android:style/Theme.Translucent" /> 
     </application> 


 </manifest>