简体   繁体   中英

Importing non-Android Java Library into Android Studio Project

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 (eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM