When multiple dependencies must have the same version number, users can use variables to declare version numbers and reduce code redundancy. Learn how to declare and use variables in Gradle scripts.
- [Narrator] Gradle build files can use variables to represent strings. This is particularly useful when you're managing software library versions. If you have two or more dependencies, that all depend on a single version, and those versions all move together, you can declare the version once as a variable and then refer to that variable wherever you need it. You can define variables either in the module level gradle file or in the project file but the syntax is different. I'll start with defining a variable right here in the dependencies block in the modules file. Let's say I wanted to manage the gson version. I'll take this version number 2.8.6 and then above my implementation line I'll add a new line and I'll start with, def, for define and then I can make up my own variable name. I'll call this gson_version and then I'll wrap this version in double quotes. Now, I can come down here and change this version to the variable that always starts with a dollar sign, I'll type in the version, and I'll see an error indicator. The error indicator is telling me that if I want to use a variable I have to use double quotes. So I'll change this from single to double quotes, and then I'll re-sync and if everything works okay, it should be fine. My version's now defined in that variable. This syntax is perfect if you're only using a particular dependency in one module. If you're working in a multi-module project though, and if multiple modules are referring to the same library, you might want to define your variables at the project level Here's how you do that. I'll select this line and copy it to the clipboard, and then I'm going to comment it out with a double slash at the beginning of the line. Then I'll go to my project build file, and I'll paste that code in, but instead of using def this time I'll use ext. And this means the same thing, I'm creating the variable but now it's a project wide variable. I'll click sync now again and if everything works as expected the project once again compiles, and the version number is accepted. So that's how you can use gradle variables to manage library versions either locally in the modules version of the gradle build file or globally in the projects version
- Installing Android Studio on macOS and Windows
- Creating your first project
- Creating virtual devices for testing
- Version control with GitHub
- Customizing the Android Studio interface
- Managing dependencies
- Generating and editing Kotlin code
- Refactoring and inspecting code
- Using templates
- Analyzing and debugging code
- Managing bitmap and vector images
Skill Level Beginner
What you should know1m 47s
1. Getting Started
2. Get Ready to Develop Apps
3. Use Software Version Control
Clone a GitHub repository3m 35s
4. Explore the User Interface
5. Manage Project Builds and Dependencies
6. Create and Edit Code Files
7. Analyze and Debug Your Code
8. Manage Image Files
Create vector image assets3m 50s
Next steps1m 11s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.