简体   繁体   English

如何在Android Studio项目中包含.jar库

[英]How Do I Include A .jar Library In Android Studio Project

Assume that I have no idea what I'm doing. 假设我不知道自己在做什么。 I'm trying to include a java library that I created in Eclipse with no relation to Android in my Android application so that I can use the classes in said library. 我试图在我的Android应用程序中包含我在Eclipse中创建的与Java无关的java库,以便我可以使用所述库中的类。 I tried creating a folder called "libs" and including ":app:libs" as a dependency in my Project Structure screen under Modules->app->Dependencies. 我尝试创建一个名为“libs”的文件夹,并在Modules-> app-> Dependencies下的Project Structure屏幕中包含“:app:libs”作为依赖项。 When I try to build, it gives me the message "Error:A problem occurred configuring project ':app'. 当我尝试构建时,它向我显示消息“错误:配置项目时出现问题':app'。

Cannot evaluate module libs : Configuration with name 'default' not found." I looked around and saw that the problem appears to be that I haven't set up a default build configuration for libs. What I can't figure out is how to do this. How do I set up a default build configuration for the module libs? 无法评估模块库:未找到名称为“default”的配置。“我环顾四周,看到问题似乎是我没有为libs设置默认的构建配置。我无法弄清楚的是如何这样做。如何为模块库设置默认的构建配置?

You can do it in different ways. 你可以用不同的方式做到这一点。 There are three standard approaches for importing a JAR file into Android studio. 将JAR文件导入Android studio有三种标准方法 The first one is traditional way , the second one is standard way , and the last one is remote library . 第一种是传统方式 ,第二种是标准方式 ,最后一种是远程库 I explained these approaches step by step with screenshots in this link: 我通过此链接中的屏幕截图逐步解释了这些方法:

https://stackoverflow.com/a/35369267/5475941 . https://stackoverflow.com/a/35369267/5475941

I hope it helps. 我希望它有所帮助。

One solution is to compile your library into a JAR file and then place it in the libs (Eclipse) or app/libs (Android Studio) folder in your Android project. 一种解决方案是将库编译为JAR文件,然后将其放在Android项目的libs (Eclipse)或app/libs (Android Studio)文件夹中。

Alternatively, if you are using Eclipse to develop your Android app, you can add the library project as a dependency to your Android project. 或者,如果您使用Eclipse开发Android应用程序,则可以将库项目添加为Android项目的依赖项。 On the other hand, if you are using Android Studio, you can import the code for your library as a module and add it as a dependency to the module for your Android app. 另一方面,如果您使用的是Android Studio,则可以将库的代码作为模块导入,并将其作为依赖项添加到Android应用的模块中。

It sounds like you're trying to add a jar dependency to a Gradle build of an Android app. 听起来您正在尝试将jar依赖项添加到Android应用程序的Gradle构建中。 You can create a folder called libs in your app project, but to tell Gradle to include jars from that folder, you'll have to put a line like this in your dependencies block: 您可以在应用程序项目中创建一个名为libs的文件夹,但要告诉Gradle包含该文件夹中的jar文件,您必须在依赖项块中添加这样的行:

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

If you create a new Android project from scratch in Android studio, this line is actually present for you in the template it uses. 如果您在Android studio中从头开始创建一个新的Android项目,则该行实际上会在您使用的模板中出现。

The syntax you were using for ':app:libs' is actually telling gradle you want to add a dependency on subproject of 'app' called 'libs'. 您用于':app:libs'的语法实际上是在告诉gradle您想要在'app'子项目中添加一个名为'libs'的依赖项。 Time to hit the gradle docs... 是时候点击gradle了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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