在 Android Studio 0.3.6 中添加外部库 [英] Adding external library in Android studio 0.3.6

查看:31
本文介绍了在 Android Studio 0.3.6 中添加外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对 Java 还很陌生,我正在尝试将 this 库添加到我的项目.问题是 Android studio 0.3.6 没有这样做的简单方法,我搜索的所有答案要么引用旧版本的 Android Studio,要么描述如何导入外部项目(源代码,而不是 jar 文件).

So I am fairly new to Java, and I am trying to add this library to my project. The problem is that Android studio 0.3.6 doesn't have a simple way of doing that and all the answers I searched either reference an older version of Android Studio, or describe how to import an external project (source code, not jar file).

阅读了一些之后,我得出的结论是,手动添加 jar 文件将是最好的方法(手动复制/粘贴和 gradle 编辑),但正如我所说,我对这项技术还很陌生,不要知道将文件放在哪里,也知道我需要将哪些行添加到 gradle 文件中.

After reading a little, I got to the conclusion that manually adding the jar file would be the best way (manual copy/paste and gradle edits) but as I said, I'm fairly new to this technology and don't know where to place the file nor what lines I need to add to the gradle files.

有人可以帮我吗?

更新 1:
我终于让 IDE 识别了 .jar 文件(我得到了自动完成和类识别).新的问题是编译时出现如下错误:Gradle: package com.google.gson does not exist.以下是我导入库的步骤:

UPDATE 1:
I finally made the IDE recognize the .jar file (I get autocomplete and class recognition). The new problem is that I get the following error when compiling: Gradle: package com.google.gson does not exist. Here are the steps I took to import the library:

  1. 在主目录下创建一个名为libs的文件夹(结果应该是src/main/libs)
  2. 复制该目录下的 .jar 文件
  3. 将以下行添加到项目中 build.gradle 文件的依赖项部分:compile files('libs/gson-2.2.4.jar').现在应该是这样的:

依赖项{编译'com.android.support:support-v13:+'编译文件('libs/gson-2.2.4.jar')}

dependencies { compile 'com.android.support:support-v13:+' compile files('libs/gson-2.2.4.jar') }

推荐答案

由于 GSON 库在 MavenCentral 中可用,因此添加它的方法很简单,无需下载存档文件并将其保存在您的项目中.

Since the GSON library is available in MavenCentral, there's an easy way to add it that avoids having to download an archive file and save it in your project.

>

转到项目结构 > 模块 > 你的模块名称 > 依赖项并点击+按钮添加新的依赖项.从列表中选择 Maven 依赖项:

Go to Project Structure > Modules > Your module name > Dependencies and click on the + button to add a new dependency. Choose Maven dependency from the list:

您将看到一个对话框,您可以在其中输入搜索词或完全限定的 Maven 坐标字符串.由于 GSON 是 Android 开发者常用的库,所以在本对话框中实际上是作为示例给出的,使用完全限定名称.你可以输入:

You'll get a dialog box where you can enter search terms or the fully-qualified Maven coordinate string. Since GSON is a common library for Android developers to use, it's actually given in this dialog as an example, with the fully-qualified name. You can type it in:

在两个对话框上都点击 OK,你应该很高兴.

Hit OK on both dialogs and you should be good to go.

有了这些 Maven 依赖项,构建系统将自动下载库并缓存它(如果还没有这样做的话);它会为您解决这个问题.

With these Maven dependencies, the build system will automatically download the library and cache it if hasn't done so already; it takes care of that for you.

如果您有一个在 MavenCentral 上不可用的库,您可以将存档保存在项目的 libs 文件夹中,然后从该模块依赖项对话框中添加一个 文件依赖项 而不是 Maven 依赖来处理它.

If you had a library that wasn't available on MavenCentral, you could save the archive in a libs folder in your project, and from that module dependencies dialog, add a File dependency instead of a Maven dependency to take care of it.

如果您手动编辑 build.gradle 文件,则需要单击工具栏中的将项目与 Gradle 文件同步"按钮以强制 Android Studio 获取更改并更新您的项目.如果您查看项目结构"对话框,则没有必要.

If you edit your build.gradle file by hand, you need to click on the "Sync Project with Gradle Files" button in the toolbar to force Android Studio to pick up the changes and update your project. If you go through the Project Structure dialog, that's unnecessary.

在 Stack Overflow 中对这个问题有很多相互矛盾的答案,因为随着必要功能的实现,此功能不断变化;以前真的坏过.这些说明在 0.3.6 中应该可以正常工作,并且在 0.3.7 及更高版本中会变得更容易一些.

There are lots of conflicting answers to this issue in Stack Overflow because the functionality for this is in flux as the necessary features are implemented; it has been really broken before. These instructions should work properly for 0.3.6, and things will get a little easier in 0.3.7 and later.

这篇关于在 Android Studio 0.3.6 中添加外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆