添加外部库在Android的工作室0.3.6 [英] Adding external library in Android studio 0.3.6

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

问题描述

所以,我是相当新的Java和我试图将这个库到我的项目。问题是,Android的工作室0.3.6并没有这样做的一个简单的方法,所有的答案,我搜索参考值的Andr​​oid Studio的旧版本,或描述如何导入外部项目(来源$ C ​​$ C,不是jar文件)。

读了一点后,我到手动添加的jar文件将是最好的方式(手动复制/粘贴和摇篮编辑)的结论,但正如我所说,我是相当新的这个技术,不知道在哪里放置文件,也没有什么线我需要添加到摇篮的文件。

有人能帮助我吗?

更新1
我终于做出了IDE识别.jar文件(我得到自动完成和类识别)。新的问题是,在编译时,我得到了以下错误:摇篮:包com.google.gson不存在。下面是我把导入库中的步骤:

  1. 创造一个称为主目录下libs文件夹中(src /主/库应该是结果)
  2. 复制.jar文件在该目录
  3. 添加下面一行到项目中的build.gradle文件的依赖关系部分:编译文件(库/ GSON-2.2.4.jar')。它看起来应该像现在这样:

    相关性{ 编译com.android.support:support-v13:+ 编译文件(库/ GSON-2.2.4.jar) }

  4. 重新编译项目(不知道是否有必要,但我做到了)
  5. 右键单击库文件夹,选择添加库
解决方案

由于GSON库在MavenCentral可用,有一个简单的方法是避免了下载的存档文件并将其保存在您的项目中添加。

项目结构> 模块> 你的模块名称> 依赖,点击+按钮添加一个新的依赖。选择的Maven依赖从列表:

您会得到一个对话框,您可以输入搜索字词或完全合格的Maven坐标串。由于GSON是Android开发者使用公用库,它实际上此对话框,例如,在给定的,与完全限定域名。您可以键入它:

点击OK就两个对话框,你应该是好去。

使用这些Maven的依赖关系,构建系统会自动下载该库并缓存,如果还没有这样做的话;它需要照顾,对于你。

如果你有一个库,它不具备的MavenCentral,可以为您节省存档在文件夹中的项目,并从该模块依赖关系对话框中,添加一个文件依赖性,而不是的Maven依赖以照顾它。

如果你手工编辑您的build.gradle文件,你需要点击同步工程与摇篮文件按钮,工具栏,迫使机器人工作室拿起变化和更新您的专案。如果你去通过项目结构对话框,这是不必要的。

有很多相互矛盾的答案,这个问题在堆栈溢出,因为这个功能是在不断变化的必要功能的实现;它之前已经被确实打破。这些指令应该正常工作为0.3.6,事情会更容易一些在0.3.7和更高版本。

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).

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.

Can someone help me?

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. Creat a folder called libs in the main directory (src/main/libs should be the result)
  2. Copy the .jar file in that directory
  3. add the following line to the dependencies section in the build.gradle file in your project: compile files('libs/gson-2.2.4.jar'). It should look something like this now:

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

  4. Recompile the project (not sure if necessary, but I did it)
  5. Right click on the libs folder and select "Add as Library"

解决方案

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.

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:

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:

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

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.

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.

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.

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的工作室0.3.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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