如何在android studio的外部库中添加jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25660166/
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
How to add a jar in External Libraries in android studio
提问by Manoj
I am new to Android Studio. What I need to do is add a few jar files in the External Libraries
below the < JDK > folder.
我是 Android Studio 的新手。我需要做的是External Libraries
在<JDK>文件夹下面添加几个jar文件。
If anyone has knowledge of how to do this, please help me.
如果有人知道如何做到这一点,请帮助我。
回答by capt.swag
A late answer, although I thought of giving an in-depth answer to this question. This method is suitable for Android Studio 1.0.0 and above.
一个迟到的答案,虽然我想对这个问题给出一个深入的答案。此方法适用于Android Studio 1.0.0 及以上版本。
STEPS
脚步
- First switch your folder structure from Android to Project.
- 首先将您的文件夹结构从 Android 切换到Project。
- Now search for the libs folderinside app - build folder.
- 现在在 app - build 文件夹中搜索libs 文件夹。
- Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library. This will take care of adding compile files('libs/library_name.jar') in build.gradle [You don't have to manually enter this in your build file].
- 将 .jar 文件粘贴到 libs 文件夹中后。右键单击 jar 文件,最后单击Add as library。这将负责在 build.gradle 中添加编译文件('libs/library_name.jar')[您不必在构建文件中手动输入它]。
Now you can start using the library in your project.
现在您可以开始在您的项目中使用该库。
回答by Thinsky
Add your jar file to the folder app/libs
. Then right click the jar file and click "add as library".
将您的 jar 文件添加到文件夹中app/libs
。然后右键单击 jar 文件并单击“添加为库”。
If there is no libs
folder you can create it. Click on the combo box that says "Android", and change it to "Project"
如果没有libs
文件夹,您可以创建它。单击显示“Android”的组合框,并将其更改为“项目”
From here, you can right click on "apps" in the directory tree and go to "New" => "Directory"
从这里,您可以右键单击目录树中的“应用程序”并转到“新建”=>“目录”
回答by Ga?tan
Put your JAR in app/libs, and in app/build.gradle add in the dependencies
section:
将您的 JAR 放在 app/libs 中,并在 app/build.gradle 中添加以下dependencies
部分:
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'libs', include: ['*.jar'])
回答by Mohammad Farahi
Create "libs" folder in app directory copy your jar file in libs folder right click on your jar file in Android Studio and Add As library... Then open build.gradle and add this:
在应用程序目录中创建“libs”文件夹将你的jar文件复制到libs文件夹中,右键单击Android Studio中的jar文件并添加为库...然后打开build.gradle并添加以下内容:
dependencies {
implementation files('libs/your jar file.jar')
}
回答by Viral Patel
Step 1:Download any JAR file for your Project.
Step 2:Copy .jar file and past in libs folder.
第 2 步:复制 .jar 文件并粘贴到 libs 文件夹中。
Step 3:Click on File > Project Structure >Select app > Dependencies
第 3 步:单击文件 > 项目结构 > 选择应用程序 > 依赖项
Step 4:
第四步:
Step 5:
第 5 步:
Step 6:After click Ok button then we can see the Dependencies add like this way:
第 6 步:点击 Ok 按钮后,我们可以看到像这样添加依赖项:
回答by Alberto Hdez
Example with Parse jar...
带有解析罐的示例...
Add jars to libs folder from Project view … create lib folder if not exists
从项目视图将 jars 添加到 libs 文件夹……如果不存在则创建 lib 文件夹
Copy all jars there...
复制那里的所有罐子...
Add libs to gradle.... in build.gradle file :
将库添加到 gradle.... 在 build.gradle 文件中:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:percent:23.0.0'
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs', include: 'Parse-*.jar')
}
For add all jars of lib folder... change Parse-*.jar to *.jar
要添加 lib 文件夹的所有 jar... 将 Parse-*.jar 更改为 *.jar
回答by Zulqarnain
回答by Rahul Sharma
A simple way to add Jar file Android Studio Steps:
Android Studio 添加 Jar 文件的简单方法步骤:
- Copy and paste your jar file to libs folder of your project.
- Click File from File menu -> Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ? + ; on Mac OS X).
- Select Modules at the left panel -> Dependencies tab.
- Add... → Project Library → Attach Jar.
- 将 jar 文件复制并粘贴到项目的 libs 文件夹中。
- 单击文件菜单中的文件 -> 项目结构(在 Windows/Linux 上为 CTRL + SHIFT + ALT + S,在 Mac OS X 上为 ? + ;)。
- 在左侧面板中选择 Modules -> Dependencies 选项卡。
- 添加... → 项目库 → 附加 Jar。
回答by adsdf
If anyone is looking for another solution without actually copying the jar file(s) into the project directory, e.g. when using a jar in multiple projects:
Open build.gradleand add
如果有人正在寻找另一种解决方案而不实际将 jar 文件复制到项目目录中,例如在多个项目中使用 jar 时:
打开build.gradle并添加
def myJarFolder = 'C:/Path/To/My/Jars'
[...]
dependencies {
[...]
compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
[...]
}
Note that of course you don't have to use the myJarFoldervariable, I find it useful though. The path can also be relative, e.g. ../../Path/To/My/Jars.
Tested with AndroidStudio 3.0
请注意,当然您不必使用myJarFolder变量,但我发现它很有用。路径也可以是相对的,例如../../Path/To/My/Jars。
使用 AndroidStudio 3.0 测试
Update: For Gradle Plugin > 3.0 use implementationinstead of compile:
更新:对于 Gradle Plugin > 3.0 使用implementation而不是compile:
dependencies {
[...]
implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
[...]
}
回答by thanassis
The GUI based approach would be to add an additional module in your project.
基于 GUI 的方法是在您的项目中添加一个额外的模块。
- From the File menu select Project Structure and click on the green plus icon on the top left.
- The new Module dialog pops
- From the phone and tablet application group select the "Import JAR or AAR package" option and click next.
- Follow the steps to create a new module that contains your JAR file.
- Click on the entry that corresponds to your main project and select the dependencies tab.
- Add a dependency to the module that you created in step 4.
- 从文件菜单中选择项目结构,然后单击左上角的绿色加号图标。
- 弹出新模块对话框
- 从手机和平板电脑应用程序组中选择“导入 JAR 或 AAR 包”选项,然后单击下一步。
- 按照以下步骤创建包含 JAR 文件的新模块。
- 单击与您的主项目相对应的条目,然后选择依赖项选项卡。
- 向您在步骤 4 中创建的模块添加依赖项。
One final piece of advice. Make sure that the JAR file you include is build with at most JDK 1.7. Many problems relating to error message "com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)" root straight to this :0.
最后一条建议。确保您包含的 JAR 文件最多使用 JDK 1.7 构建。许多与错误消息“com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)”相关的问题直接指向这个:0。