o7planning

How to add external libraries to Android Project in Android Studio?

  1. The ways to use external libraries
  2. Way 1 - Copy external library to libs folder
  3. Way 2 - Using the module library
  4. Way 3 - Using remote library

1. The ways to use external libraries

You are developing an Android app on Android Studio, sometimes you want to use an external library for your project, such as a jar file.
Common langs is an java library with open source code which is provided by the Apache, it has utility methods for working with String, numbers, concurrency ...
Suppose that you want to use this library for your Android project. And using one of its utility methods to test whether a String is a number or not.
import org.apache.commons.lang3.StringUtils;
 
public class CheckNumeric {
 
   public void test() {
       String text1 = "0123a4";
       String text2 = "01234";
        
       boolean result1 = StringUtils.isNumeric(text1);
       boolean result2 = StringUtils.isNumeric(text2);
 
       System.out.println(text1 + " is a numeric? " + result1);
       System.out.println(text2 + " is a numeric? " + result2);
 
   }
 
}
In this document I will guide you 3 ways to use the external library:
  • Add your jar files to libs folder of the project and declare it as a library to use.
  • Create a Android module and copy your jar file to this module, and then declare your project using the newly created module.
  • Declare and use a remote library.

2. Way 1 - Copy external library to libs folder

Create a project named AddLibsDemo:
OK, your project has been created.
Change to Project tab:
Copy jar file to libs folder:
Right click jar files, select "Add as Library..":
Turn back to "Android" Tab, you can see that your library has been already declared in build.grade (Module: app)
OK, now your library is being already used.

3. Way 2 - Using the module library

Suppose you have a AddLibsDemo2 project, you want to use common-lang3-3.4.jar library for this project.
Create a Android Module:
Point to the location of the library.
Module library has been created:
Next, you need to declare the dependency of the main project into the newly created module.
  • File/Project Structure
Add module dependencies:
You can check your module which has been declared in build.grade (Module: app).

4. Way 3 - Using remote library

Android Studio can use the library on a network, it is located in some repository on the Internet. Suppose you have AddLibDemo3 project, and you need to add the remote library to your project.
Search the library:
OK, the library has been added to the project, you can see on build.grade (Module: app).

Android Programming Tutorials

Show More