Gradle Dependencies Maintenance

This blog is written to keep libraries versions,app version and gradle dependencies at single point

So lets start coding…

1.App Versioning

To keep the proper incremental build and proper version naming conventions we can keep the app version with following code in build.gradle file in app module

def versionMajor = 1
def versionMinor = 0
def versionPatch = 0
def versionBuild = 1 // bump for dogfood builds, public betas, etc
android { 
  defaultConfig { 
     applicationId “com.burhan.rashid.daggermvprx”
     versionCode versionMajor * 10000 + versionMinor * 1000 +  versionPatch * 100 + versionBuild 
    versionName “${versionMajor}.${versionMinor}.${versionPatch}” 
}

2.Build APK file with App name

Now you can provide a file name to APK with app name its version and build date to identify your APK very easily in build folder while releasing your app

def getAppName() {
    def stringsFile = android.sourceSets.main.res.sourceFiles.find { it.name.equals 'strings.xml' }
    String s = new XmlParser().parse(stringsFile).string.find { it.@name.equals 'app_name' }.text();
    def all = s.replaceAll(" ", "_")
    return all.replaceAll(""", "");
}
 android {
   ...
     applicationVariants.all { variant ->
        ;
            variant.outputs.each { output ->
            ;
            def SEP = "_"
            def version = variant.versionName
            def buildType = variant.variantData.variantConfiguration.buildType.name              
            def date = new Date();
            def formattedDate = date.format('ddMMyy')
            def newApkName = appName + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"
            output.outputFile = new File(output.outputFile.parent, newApkName)
        }
    }
 }

https://github.com/burhanrashid52/AndroidDaggerMVPRxArchitecture/blob/master/app/build.gradle

3.Support libraries and SDK version dependencies

To maintain common version for android support libraries and SDK tools with single or multiple module we need to keep ext constant in build.gradle ( Top-level build file)

allprojects {
    repositories {
        jcenter()
    }
}
 ext {
    // sdk and tools
    minSdkVersion = 21
    targetSdkVersion = 25
    compileSdkVersion = 25
    buildToolsVersion = '25.0.0'
    // dependencies versions
    supportLibraryVersion = '25.1.0'
    playServicesVersion = '9.2.1'
}

https://github.com/burhanrashid52/AndroidDaggerMVPRxArchitecture/blob/master/build.gradle

Provide the ext reference in build.gradle(app folder or any other module gradle) like this

android {
   compileSdkVersion rootProject.compileSdkVersion
   buildToolsVersion rootProject.buildToolsVersion
defaultConfig {
    applicationId “com.burhan.rashid.daggermvprx”
    minSdkVersion rootProject.minSdkVersion
    targetSdkVersion rootProject.targetSdkVersion
  }
}
dependencies {
    compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
    compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
    compile "com.android.support:design:$rootProject.supportLibraryVersion"
}

You need to use $rootProject.supportLibraryVersion in dependencies to get the version from top level build.gradle file

Keeping common version in all modules of the app for support libraries and any other play services libraries to keep concurrency throughout the app.In case of any new version just change the version from Project Gradle file and sync it

Thanks for reading my blog

In case of any query please comment on the post i will try to reply ASAP

If you like this article Please like and share with your friends and others developers

Full Project:

https://github.com/burhanrashid52/AndroidDaggerMVPRxArchitecture

Site Footer