Home / Android / How to use external libraries in Android Studio

How to use external libraries in Android Studio

LIbraries in Android Studio

Credit: Adam Sinicki / Android Authority

One of the most important lessons I ever learned as a developer, although it took some time to sink in, was “don’t reinvent 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 might feel like “cheating” to use someone else’s code to resize a bitmap or modify a string but, in truth, it’s just good sense. That goes double for independent developers that don’t have a ton of time and resources to throw at every project.

Why spend hours banging your head against a wall when you can just copy and paste someone else’s code that does the same thing better? Assuming they are happy for you to use 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 provide entirely new functionality and let you do some seriously awesome stuff as a developer with minimal code. Unlike copying and pasting code, libraries are entirely portable too. This makes it easy to access advanced features with minimal work or confusion.

See also: How to use Python modules

So, that’s why you should use libraries in Android Studio. The next question is how.

How to use external libraries in Android Studio

There are actually multiple ways to add external libraries in Android Studio. The best option for any given use-case will depend on the nature of the project and your goals for it. So, let’s take a look at the two most common methods.

Adding Gradle dependencies

Gradle is the build tool that takes all of the different files used to make your Android app work and builds them into a single package that can run on a device. If you are using an external library, then that library code also needs to go into your project.

There are plenty of Android Studio libraries available through remote repositories. You can use these simply by adding their location and telling Gradle to include them in your code.

See also: Introducing Gradle for new Android developers – The master builder

The great thing about this method is that you can easily switch to a newer update for a given library by changing a single line in your Gradle build file. The downside is that because these libraries aren’t stored locally on your machine, you won’t be able to edit them manually. You’ll also need an internet connection.

To add dependencies this way, you need to find the module-level build file and then add the line to the dependencies block near the bottom. For example, in order to use TensorFlow Lite, which provides on-device machine learning capabilities, you simply add the following line:

Compile ‘org.tensorflow: tensorflow-lite:+’

Copying files manually

Another way to use libraries in Android Studio is to copy your files manually to the libs folder. To do this, simply download the relevant jar file and then drop it into the libs folder of your project. Now right-click that file and choose “Add as Library…” If you go and check inside your module’s Gradle build file, you should now see that the dependency has been added.

This method has the benefit of being local and easy to modify. However, it also means you will be forced to manually replace the files any time you want to update to a newer version. Keep in mind that some libraries will only be available via one method.

Whichever method you use, you should now be able to reference classes and methods from that library as you would any other!

Closing comments

That is how you go about using external libraries in Android Studio! Of course, the specific methods and strategies you use following this point will then 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!


Source link

About admin

Check Also

Facebook is testing pop-up messages telling people to read a link before they share it

Years after popping open a Pandora’s box of bad behavior, social media companies are trying ...

Leave a Reply

Your email address will not be published. Required fields are marked *