Java 在 Android Studio 0.3.6 中添加外部库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20241765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding external library in Android studio 0.3.6
提问by Mario Stoilov
So I am fairly new to Java, and I am trying to add thislibrary 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).
所以我对 Java 相当陌生,我正在尝试将此库添加到我的项目中。问题是 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:
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:
我终于让 IDE 识别了 .jar 文件(我得到了自动完成和类识别)。新问题是我在编译时出现以下错误:Gradle: package com.google.gson does not exist
. 以下是我导入库所采取的步骤:
- Creat a folder called libs in the main directory (src/main/libs should be the result)
- Copy the .jar file in that directory
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') }
- Recompile the project (not sure if necessary, but I did it)
- Right click on the libs folder and select "Add as Library"
- 在主目录下创建一个名为 libs 的文件夹(src/main/libs 应该是结果)
- 复制该目录中的 .jar 文件
以下行添加到您的项目中的build.gradle文件中的依赖关系部分:
compile files('libs/gson-2.2.4.jar')
。它现在应该是这样的:依赖项 { compile 'com.android.support:support-v13:+' compile files('libs/gson-2.2.4.jar') }
- 重新编译项目(不确定是否有必要,但我做到了)
- 右键单击 libs 文件夹并选择“添加为库”
采纳答案by Scott Barta
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.
由于 MavenCentral 中提供了 GSON 库,因此有一种添加它的简单方法,无需下载存档文件并将其保存在您的项目中。
Go to Project Structure> Modules> Your module name> Dependenciesand click on the + button to add a new dependency. Choose Maven dependencyfrom the list:
转到项目结构>模块>您的模块名称>依赖项,然后单击 + 按钮添加新的依赖项。从列表中选择Maven 依赖项:
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:
您将看到一个对话框,您可以在其中输入搜索词或完全限定的 Maven 坐标字符串。由于 GSON 是 Android 开发者常用的库,所以在本对话框中实际上是作为示例给出的,使用完全限定名称。你可以输入:
Hit OK on both dialogs and you should be good to go.
在两个对话框上都点击 OK,你应该很高兴。
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.
有了这些 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 dependencyinstead of a Maven dependencyto take care of it.
如果您的库在 MavenCentral 上不可用,您可以将存档保存libs
在项目的文件夹中,然后从该模块依赖项对话框中添加文件依赖项而不是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. If you go through the Project Structure dialog, that's unnecessary.
如果您手动编辑 build.gradle 文件,则需要单击工具栏中的“将项目与 Gradle 文件同步”按钮以强制 Android Studio 获取更改并更新您的项目。如果您浏览“项目结构”对话框,则没有必要。
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.
Stack Overflow 中对此问题有很多相互矛盾的答案,因为随着必要功能的实现,此问题的功能不断变化;以前真的坏了。这些说明在 0.3.6 中应该可以正常工作,并且在 0.3.7 和更高版本中事情会变得更容易一些。
回答by Barun
Click on ProjectName->Libs folder.Paste that jar file into that folder. Just refresh the project.You are done.
单击 ProjectName->Libs 文件夹。将该 jar 文件粘贴到该文件夹中。只需刷新项目即可。大功告成。
回答by kirsche40
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. So you have to do this manually.
我遇到过同样的问题。新版本的 Android Studio (0.3.6) 删除了一些必要的功能,可以使用 IDE 将现有库添加到项目中。因此,您必须手动执行此操作。
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. 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>\src\main\libs\”中生成一个新文件夹,并在此处添加库。现在您必须通过添加以下内容来更改您的“<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;
issue.
现在通过右键单击在“项目视图”中选择库,然后选择“添加为库... > 级别 > 全局库”。这将解决一个import com.google.gson.Gson;
问题。
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. 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. Now it does not show me no errors anymore on build.
也许你仍然无法构建。在这种情况下,您应该检查您的项目模块设置并查看 Gson 依赖项是否存在错误。我让 Android Studio 通过点击模块设置对话框右下角的“红色小灯泡图标 > 添加依赖项”来解决这个问题。现在它不再向我显示构建时没有错误。
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. Mario filed a bug report.
现在我们只剩下一个问题:项目在项目关闭时丢失了库引用。所以我们必须再次在打开时添加库。也许这是 Android Studio 0.3.6 的问题。马里奥提交了错误报告。
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. 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. It's free and works for me. I did all the stuff in 2 hours which need days using broken 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.
不幸的是,我没有让 AS 0.3.6 正常工作。有很多问题 - 至少在添加具有不同命名空间的另一个模块时。所以我切换到了起源IDE:IntelliJ IDEA 12 社区版。它是免费的,对我有用。我使用损坏的 Android Studio 在 2 小时内完成了需要数天的所有工作。我不知道是什么迫使谷歌基于 IntelliJ IDEA 构建自己的 IDE,而没有额外的好处/显着的功能,当后者像魅力一样工作时。
回答by ken
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-studioputting a duplicate here just in case you stumble into this post instead
我发布了相同的 将 jar 库导入 android-studio 的帖子,在这里放了一个副本,以防万一你偶然发现这篇文章
回答by Dev
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):
- In the "app/libs" folder on the hard disk, create a sub-folder "MyExternalLib", and copy the external library into that folder.
- In the file "app/build.gradle", inside the block named "dependencies", add the line
compile project('libs:MyExternalLib')
- In the file "settings.gradle", add the line
include ':app:libs:MyExternalLib'
- Click the button "Sync Project with Gradle Files"
- 在硬盘上的“app/libs”文件夹中,创建一个子文件夹“MyExternalLib”,并将外部库复制到该文件夹中。
- 在文件“app/build.gradle”中,在名为“dependencies”的块中,添加一行
compile project('libs:MyExternalLib')
- 在文件“settings.gradle”中,添加一行
include ':app:libs:MyExternalLib'
- 单击“将项目与 Gradle 文件同步”按钮
回答by Ra_mon
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. https://developer.android.com/sdk/installing/studio-build.html
该项目现在包含两个模块 app 和 lib,每个模块中有一个活动。 https://developer.android.com/sdk/installing/studio-build.html