How to use external libraries in Android Studio

Adam Sinicki / Android Authority

One of the most important lessons I ever learned as a developer, although it took me a while to get into it, was “don’t reinvent the wheel”. Understanding how to use libraries in Android Studio is a big part of it.

In other words: if you have to perform a common task, you probably don’t need to write the code yourself. Using someone else’s code to resize a bitmap or modify a string may sound like “cheating”, but, in truth, it’s fair. common sense. This will double for independent developers who don’t have a lot of time and resources to devote to each 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’re happy you are using it!

Don’t reinvent the wheel.

This is what the libraries in Android Studio are for. They simply extend the capabilities of Java / Kotlin / Android SDK by allowing you to use classes and methods built by other users. The best libraries offer entirely new functionality and allow you to do some really great things as a developer with minimal code. Unlike copying and pasting code, libraries are also fully portable. This makes it easy to access advanced features with minimal fuss or confusion.

See also: How to use Python modules

That’s why you need to use libraries in Android Studio. The next question is How? ‘Or’ What.

How to use external libraries in Android Studio

There are actually several ways to add external libraries in Android Studio. The best option for a given use case will depend on the nature of the project and your goals. 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 run your Android app and builds them into one package that can run on one device. If you are using an external library then this library code too must enter your project.

The advantage of this method is that you can easily upgrade to a newer update for a given library by modifying 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 will 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, to use TensorFlow Lite, which provides machine learning functionality on the device, you just need to add the following line:

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

Copy 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 appropriate jar file and then drop it into your project’s libs folder. Now right click on that file and choose “Add as Library …” If you are going to check your module’s Gradle build file, you should now see that the dependency has been added.

This method has the advantage of being local and easily modifiable. However, it also means that you will be forced to manually replace files whenever you want to update to a newer version. Keep in mind that some libraries will only be available through one method.

Whichever method you use, you should now be able to reference the classes and methods in that library just like you would any other!

Closing comments

This is how you use external libraries in Android Studio! Of course, the specific methods and strategies you use after this point will depend on the library in question. Going forward, we’ll take a look at the most useful libraries for Android developers. Let us know your favorites in the comments!

Source link

Comments are closed.