Android studio gradle version unified management configuration

Preface

With the continuous iteration and update of the project, the project becomes larger and larger. In order to reduce compilation time and development efficiency, most of them adopt modular and component-based development, and the number of moudles will increase. When some dependencies or sdk versions are upgraded, the gradle of each moudle may need to be modified once, so unified management and configuration is more important. There are also several ways to realize it on the Internet. Here is only for the way that I think is better.

I. configuration

Create a file named config.gradle in the root directory of the project. The name can be customized. Here is config.gradle

  1. ext {  
  2.     //app related version control  
  3.     versions = [  
  4.             sdkMinVersion     : 15,  
  5.             sdkTargetVersion  : 26,  
  6.   
  7.             sdkCompileVersion : 26,  
  8.             toolsBuildVersion : "26.0.2",  
  9.   
  10.             appVersionCode    : 520,  
  11.             appVersionName    : "1.0.0"  
  12.     ]  
  13.   
  14.     //Dependent version control  
  15.     depVersion = [  
  16.             appCompatVersion : "26.+",  
  17.             recyclerViewVersion : "26.0.0-alpha1"  
  18.     ]  
  19.   
  20.     //Dependence  
  21.     deps = [  
  22.         suport : [  
  23.                 appcompat   : "com.android.support:appcompat-v7:${depVersion.appCompatVersion}",  
  24.                 recyclerview: "com.android.support:recyclerview-v7:${depVersion.recyclerViewVersion}"  
  25.         ]  
  26.     ]  
  27. }  

Two. Call

01. Add a line of code to build.gradle in the project root directory. Note that without this line of code, it will not work

  1. apply from: 'config/config.gradle'  
02. Call in the corresponding moudle.

  1. android {  
  2.     def versions = rootProject.ext.versions  
  3.     compileSdkVersion versions.sdkCompileVersion  
  4.     buildToolsVersion versions.toolsBuildVersion  
  5.     defaultConfig {  
  6.         applicationId "exrow.zp.zpexrow"  
  7.         minSdkVersion versions.sdkMinVersion  
  8.         targetSdkVersion versions.sdkTargetVersion  
  9.         versionCode versions.appVersionCode  
  10.         versionName versions.appVersionName  
  11.     }  
  12.     buildTypes {  
  13.         release {  
  14.             minifyEnabled false  
  15.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
  16.         }  
  17.     }  
  18. }  
  19.   
  20. dependencies {  
  21.     def dependencies = rootProject.ext.deps  
  22.   
  23.     compile dependencies.suport.appcompat  
  24.     compile dependencies.suport.recyclerview  
  25. }  

Three, remarks

There will be other corresponding configurations in gradle. Other configurations are the same as the above configurations and can be used according to their own situations.

Tags: Gradle Android SDK

Posted on Thu, 02 Apr 2020 14:48:18 -0700 by standalone