2

I would like to import a non-Android Java library to an Android project.

Based on my research, there seem to be two main options:

  1. Add the source code of the Java library as a separate module. However, the source code cannot simply be imported (e.g. added as a subrepo) since the Android package names would need to be added to each file.

  2. Add a JAR file of the Java library. I have been following the instructions available here, and the project builds successfully, though I am still unable to import classes from the library. I suspect this may be because the project is a standalone Java library rather than an Android library.

Any advice on how to add the library (either as a JAR or as a copy of the existing library) would be greatly appreciated. Thanks.

M.S.
  • 1,091
  • 1
  • 11
  • 27
  • Possible duplicate of [Android Studio: Add jar as library?](https://stackoverflow.com/questions/16608135/android-studio-add-jar-as-library) – merterpam Jun 11 '17 at 23:00

1 Answers1

1

Add the jar file for the library under app/libs and make sure your app/build.gradle file has the following:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

After you've included the jar file, click on the "Sync Project with Gradle Files" button. After the sync is complete you should be able to use the classes in the library.

Luis
  • 3,451
  • 1
  • 27
  • 41