2015年11月3日火曜日

モジュール間の Product Flavor を同期させる

app モジュール('com.android.application')と api モジュール('com.android.library')があり、それぞれに staging と production flavor があるとする。

app モジュールの flavor が staging なら api モジュールの staging flavor を使い、
app モジュールの flavor が production なら api モジュールの production flavor を使いたい。

この場合、build.gradle に次のような設定を行う。

api モジュールの build.gradle
  1. apply plugin: 'com.android.library'  
  2.   
  3. configurations {  
  4.     stagingDebugCompile  
  5.     stagingReleaseCompile  
  6.   
  7.     productionDebugCompile  
  8.     productionReleaseCompile  
  9. }  
  10.   
  11. android {  
  12.     ...  
  13.   
  14.     publishNonDefault true  
  15.   
  16.     productFlavors {  
  17.         production {  
  18.             ...  
  19.         }  
  20.         staging {  
  21.             ...  
  22.         }  
  23.     }  
  24. }  


app モジュールの build.gradle
  1. apply plugin: 'com.android.application'  
  2.   
  3. configurations {  
  4.     stagingDebugCompile  
  5.     stagingReleaseCompile  
  6.   
  7.     productionDebugCompile  
  8.     productionReleaseCompile  
  9. }  
  10.   
  11. android {  
  12.     ...  
  13.   
  14.     productFlavors {  
  15.         production {  
  16.             ...  
  17.         }  
  18.         staging {  
  19.             ...  
  20.         }  
  21.     }  
  22. }  
  23.   
  24. dependencies {  
  25.     ...  
  26.   
  27.     stagingDebugCompile project(path: ':api', configuration: 'stagingDebug')  
  28.     stagingReleaseCompile project(path: ':api', configuration: 'stagingRelease')  
  29.   
  30.     productionDebugCompile project(path: ':api', configuration: 'productionDebug')  
  31.     productionReleaseCompile project(path: ':api', configuration: 'productionRelease')  
  32.   
  33. }