Credit: Adam Sinicki / Android Authority
One of the most important lessons I ever learned as a developer, although it took me a while to get to the bottom of it, was “not reinventing the wheel.” Understanding how to use libraries in Android Studio is a big part of that.
In other words, if you need to perform a common task, you probably don’t need to write the code yourself. It may feel like “cheating” to use someone else’s code to resize a bitmap or change a string, but in reality it’s just good feeling. That goes doubly for independent developers who don’t have a lot of time and resources to throw in every project.
Why spend hours banging your head against a wall when you can just copy and paste the code from someone else who does the same better? Assuming they like you using it!
Don’t reinvent the wheel.
This is what libraries in Android Studio are for. They simply extend the capabilities of Java / Kotlin / the Android SDK by letting you use classes and methods built by other users. The best libraries offer all-new functionality and let you do some seriously awesome things as a developer with minimal code. Unlike copying and pasting code, libraries are also completely portable. This makes it easy to access advanced functions with minimal work or confusion.
Also see: How to use Python modules
That’s why you should be using Libraries in Android Studio. The next question is how.
Using external libraries in Android Studio
There are actually multiple ways to add external libraries in Android Studio. The best option for a particular use case depends on the nature of the project and your goals for it. So let’s take a look at the two most commonly used methods.
Add gradle dependencies
Gradle is the build tool that collects and assembles all the different files used to run your Android app into one package that can be run on a device. If you are using an external library, then that library code also should address your project.
There are plenty of Android Studio libraries available from external repositories. You can easily use these by adding their location and telling Gradle to include them in your code.
Also see: Introducing Gradle to New Android Developers – The Master Builder
The great thing about this method is that you can easily switch to a newer update for a particular library by changing a single line in your Gradle build file. The downside is that because these libraries are not stored locally on your computer, you cannot edit them manually. You also need an internet connection.
To add dependencies in this way, find the build file at the module level and then add the line to the dependency block at the bottom. For example, if you want to use TensorFlow Lite, which offers machine learning capabilities on the device, simply add the following line:
Compile ‘org.tensorflow: tensorflow-lite: +’
Copy files manually
Another way to use libraries in Android Studio is to manually copy your files to the libs folder. To do this, simply download the relevant jar file and drop it into the libs folder of your project. Now right click on that file and choose “Add as library …”. If you are going to check the Gradle build file of your module, you should now see that the dependency has been added.
This method has the advantage of being local and easily adaptable. However, it also means that you have to manually replace the files when you want to update to a newer version. Keep in mind that some libraries are only available through one method.
Whichever method you use, you should now be able to reference classes and methods from that library just like any other!
That’s how you use external libraries in Android Studio! The specific methods and strategies you use after this point will of course depend on the library in question. In the future, we’ll take a look at the most useful libraries for Android developers. Let us know your favorites in the comments!