简体   繁体   English

在Android studio 0.3.6中添加外部库

[英]Adding external library in Android studio 0.3.6

So I am fairly new to Java, and I am trying to add this library to my project. 所以我对Java很新,我正在尝试将这个库添加到我的项目中。 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). 问题是Android studio 0.3.6没有这么简单的方法,我搜索的所有答案都引用了旧版本的Android Studio,或描述了如何导入外部项目(源代码,而不是jar文件) 。

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. 阅读一点后,我得出结论,手动添加jar文件将是最好的方式(手动复制/粘贴和gradle编辑),但正如我所说,我对这项技术相当新,不知道在哪里放置文件或我需要添加到gradle文件的行。

Can someone help me? 有人能帮我吗?

UPDATE 1 : 更新1
I finally made the IDE recognize the .jar file (I get autocomplete and class recognition). 我终于让IDE识别出.jar文件(我得到了自动完成和类识别)。 The new problem is that I get the following error when compiling: Gradle: package com.google.gson does not exist . 新问题是我在编译时遇到以下错误: 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) 在主目录中创建一个名为libs的文件夹(src / main / libs应该是结果)
  2. Copy the .jar file in that directory 复制该目录中的.jar文件
  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') . 将以下行添加到项目的build.gradle文件中的dependencies部分: 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') } dependencies {compile'c​​om.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" 右键单击libs文件夹并选择“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. 由于GSON库在MavenCentral中可用,因此可以轻松添加它,以避免必须下载存档文件并将其保存在项目中。

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依赖项

添加Maven依赖项

You'll get a dialog box where you can enter search terms or the fully-qualified Maven coordinate string. 您将看到一个对话框,您可以在其中输入搜索词或完全限定的Maven坐标字符串。 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. 由于GSON是Android开发人员使用的通用库,因此在此对话框中实际给出了它作为示例,具有完全限定名称。 You can type it in: 您可以输入:

添加GSON依赖项

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; 有了这些Maven依赖项,构建系统将自动下载库并缓存它,如果还没有这样做的话; 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. 如果您有一个MavenCentral上没有的库,您可以将存档保存在项目的libs文件夹中,并从该模块依赖项对话框中添加File依赖项而不是Maven依赖项来处理它。

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. 如果您手动编辑build.gradle文件,则需要单击工具栏中的“使用Gradle文件同步项目”按钮以强制Android Studio获取更改并更新项目。 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; Stack Overflow中存在许多与此问题相矛盾的答案,因为在实现必要功能时,此功能不断变化; 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. 这些指令适用于0.3.6,在0.3.7及更高版本中,事情会变得更容易一些。

I had the same issue. 我遇到过同样的问题。 The new version of Android Studio (0.3.6) removed some necessary features to add an existing library to a project using the IDE. 新版本的Android Studio(0.3.6)删除了一些必要的功能,可以使用IDE将现有库添加到项目中。 So you have to do this manually. 所以你必须手动完成这个。

Adding the library into the build folder "<project>\\App\\build\\libs\\" will break the project on "menu > build > clear project / rebuild project". 将库添加到构建文件夹“<project> \\ App \\ build \\ libs \\”将在“菜单>构建>清除项目/重建项目”中中断项目。

Updated solution 更新解决方案
My solution is to generate a new folder inside "<project>\\<app name>\\src\\main\\libs\\" and add the library here. 我的解决方案是在“<project> \\ <app name> \\ src \\ main \\ libs \\”中生成一个新文件夹,并在此处添加库。 Now you have to change your "<project>\\<app name>\\build.gradle" by adding the following (my example shows the value for android-support library: 现在你必须通过添加以下内容来更改“<project> \\ <app name> \\ build.gradle”(我的示例显示了android-support库的值:

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

Now select the library in "project View" by right click and select "Add as library... > level > Global library". 现在,在“项目视图”中右键单击选择库,然后选择“添加为库...>级别>全局库”。 This will fix an import com.google.gson.Gson; 这将修复import com.google.gson.Gson; issue. 问题。

Maybe you still cannot build. 也许你仍然无法建立。 In this case you shall check you project module settings and see if there is an error for Gson dependency. 在这种情况下,您应检查项目模块设置,并查看Gson依赖项是否存在错误。 I let Android Studio fix this issue by hitting a "small red bulb icon > add dependency" in the lower right corner of module settings dialog. 我让Android Studio通过在模块设置对话框的右下角点击“小红灯泡图标>添加依赖项”来解决此问题。 Now it does not show me no errors anymore on build. 现在它不再显示构建上没有错误。

Now we have only one remaining problem: The project does lose the library reference on project close. 现在我们只剩下一个问题:项目确实在项目关闭时丢失了库引用。 So we have to add the library on open again. 所以我们必须再次打开库。 Maybe this is an issue of Android Studio 0.3.6. 也许这是Android Studio 0.3.6的一个问题。 Mario filed a bug report . 马里奥提交了一份错误报告

BTW: I upvoted this question because I searched without success for a working solution in the internet. 顺便说一句:我赞成这个问题是因为我没有成功搜索互联网上的工作解决方案。 I think beginners will always fail to work with the Android developer tutorials of Google when they are forced to deal with the support library. 我认为初学者在被迫处理支持库时,总是无法使用Google的Android开发人员教程。

Update / Recommendation 更新/推荐

Unfortunately I did not get AS 0.3.6 working properly. 不幸的是我没有让AS 0.3.6正常工作。 There are to many issues - at least when adding another module with different namespace. 存在许多问题 - 至少在添加具有不同命名空间的另一个模块时。 So I switched to the origin IDE: IntelliJ IDEA 12 community Edition. 所以我切换到原始IDE:IntelliJ IDEA 12社区版。 It's free and works for me. 它是免费的,适合我。 I did all the stuff in 2 hours which need days using broken Android Studio. 我在2小时内完成了所有这些工作,需要使用破解的Android Studio。 I have no idea what forces Google to build its own IDE based on IntelliJ IDEA without additional benefits / noticeable features when the latter works like a charm. 我不知道是什么迫使谷歌基于IntelliJ IDEA构建自己的IDE而没有额外的好处/明显的功能,当后者像魅力一样。

Running Android Studio 0.4.0 Solved the problem of importing jar by 运行Android Studio 0.4.0解决了导入jar的问题

Project Structure > Modules > Dependencies > Add Files
Browse to the location of jar file and select it

For those like manual editing Open app/build.gradle 对于那些像手动编辑打开app / build.gradle

dependencies {
    compile files('src/main/libs/xxx.jar')
}

I posted the same to importing jar libraries into android-studio putting a duplicate here just in case you stumble into this post instead 我发布相同的导入jar库到android-studio放一个副本,以防万一你偶然发现这个帖子

Click on ProjectName->Libs folder.Paste that jar file into that folder. 单击ProjectName-> Libs文件夹。将该jar文件粘贴到该文件夹​​中。 Just refresh the project.You are done. 只需刷新项目。你完成了。

Using Android Studio 0.8.2, I had to do the following (supposing the library you're trying to add is called MyExternalLib): 使用Android Studio 0.8.2,我必须执行以下操作(假设您尝试添加的库名为MyExternalLib):

  1. In the "app/libs" folder on the hard disk, create a sub-folder "MyExternalLib", and copy the external library into that folder. 在硬盘上的“app / libs”文件夹中,创建一个子文件夹“MyExternalLib”,并将外部库复制到该文件夹​​中。
  2. In the file "app/build.gradle", inside the block named "dependencies", add the line compile project('libs:MyExternalLib') 在文件“app / build.gradle”中,在名为“dependencies”的块内,添加行compile project('libs:MyExternalLib')
  3. In the file "settings.gradle", add the line include ':app:libs:MyExternalLib' 在“settings.gradle”文件中,添加include ':app:libs:MyExternalLib'
  4. Click the button "Sync Project with Gradle Files" 单击“使用Gradle文件同步项目”按钮

Create a new library module 创建一个新的库模块

It is good development practice to group functionality that you may reuse in other apps inside a library module. 将您可以在库模块内的其他应用程序中重用的功能分组是一种很好的开发实践。 To create a library module inside the BuildSystemExample project: 要在BuildSystemExample项目中创建库模块:

Click File and select New Module.
On the window that appears, select Android Library and click Next.
Leave the default module name (lib) unchanged and click Next.
Select Blank Activity and click Next.
Type "LibActivity1" on the Activity Name field and click Finish.

The project now contains two modules, app and lib, with one activity in each module. 该项目现在包含两个模块app和lib,每个模块中有一个活动。 https://developer.android.com/sdk/installing/studio-build.html https://developer.android.com/sdk/installing/studio-build.html

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

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