{
"$type": "site.standard.document",
"canonicalUrl": "https://segunfamisa.com/posts/android-gradle-extra-properties",
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreic62tfq5f7j5wjuphzxaeqabbx4x5pk46ovlhbu3ge2hyztih6g2q"
},
"mimeType": "image/png",
"size": 26525
},
"description": "Android tip on how to use gradle extra properties to manage dependency versioning",
"path": "/posts/android-gradle-extra-properties",
"publishedAt": "2016-07-29T07:05:30.000Z",
"site": "at://did:plc:a5mekodp4afxadlpr4hp2wci/site.standard.publication/3mm2oa7vz5327",
"tags": [
"android",
"tip",
"android studio",
"gradle"
],
"textContent": "This is yet another tip you can use in improving your Android development experience\nand speed.\n\nWe all love dependencies right? Yes we do!\n\nA typical Android studio project (you can stop reading now if you still use Eclipse 😑 seriously)\nhas a project level build.gradle file and as many module-level build.gradle as there are modules.\n\nDependencies are usually managed at the app-module level, and your app-module build.gradle file\ncan quickly get messy from dependencies. It gets even worse, when you have other modules you reference\nin your app-module, each with its own dependencies.\n\nIn this post, I'll show a quick way of making things look neat, and easy to maintain.\n\nExternalize hardcoded values.\n\nLet's say our project's app-module build.gradle looks like this:\n\nYou can see that we've repeated quite a number of versions, including the\nandroid support libraries.\n\nWhat we want to do is to externalize hardcoded values in our build.gradle file by\nleveraging gradle's extra properties.\n\nWe can extract these hardcoded into an ext block. Our build.gradle file will now look like this:\n\n<p align=\"center\">\n\t<img src=\"/images/wait-what-meme.jpg\">\n</p>\n\nWait...what changed!?\n\nIf you look closely, you'll notice that\n\ncompile 'com.android.support:appcompat-v7:23.4.0' changed to:\n\ncompile \"com.android.support:appcompat-v7:$supportLibraryVersion\"\n\nNotice the change from single quotes to double quotes. Also note that the use of $ here is simply use of String interpolation in Groovy\n\nEasy peasy!\n\nWhat to do for multiple modules?\n\nSo, besides you app-module, let's say you also have another awesome-library module you have written and is used in this project. This awesome-library, uses some dependencies also declared in your app-module.\n\nHow do we fix this? You might be tempted to have an ext block in both modules. That will work,\nbut if you need to upgrade the support library version, you will need to modify the version, in both modules.\n\nThe fix is simple, we move the ext tag to our root project build.gradle file.\n\nOur root build.gradle file then looks like this:\n\nMy module-level build.gradle files then look like this:\n\nExtra?\n\nI also use this technique to manage minSdkVersion, targetSdkVersion, compileSdkVersion and buildToolsVersion in my projects.\n\nCheck out this gist for a fuller example of how I do this.\n\nIf you have any comments/suggestions or corrections, I'd love to hear them. Please do not\nhesitate to drop a comment below or tweet at me.\n\nPlease share if you found this tip useful 🙈😁\n\nCheers :)",
"title": "Manage Android dependencies versions using gradle extra properties."
}