如何在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:37:33  来源:igfitidea点击:

How to add a jar in External Libraries in android studio

androidandroid-studio

提问by Manoj

I am new to Android Studio. What I need to do is add a few jar files in the External Librariesbelow the < JDK > folder.

我是 Android Studio 的新手。我需要做的是External Libraries在<JDK>文件夹下面添加几个jar文件。

enter image description here

在此处输入图片说明

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

脚步



  1. First switch your folder structure from Android to Project.
  1. 首先将您的文件夹结构从 Android 切换到Project

enter image description here

在此处输入图片说明

  1. Now search for the libs folderinside app - build folder.
  1. 现在在 app - build 文件夹中搜索libs 文件夹。

enter image description here

在此处输入图片说明

  1. 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].
  1. 将 .jar 文件粘贴到 libs 文件夹中后。右键单击 jar 文件,最后单击Add as library。这将负责在 build.gradle 中添加编译文件('libs/library_name.jar')[您不必在构建文件中手动输入它]。

enter image description here

在此处输入图片说明

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 libsfolder you can create it. Click on the combo box that says "Android", and change it to "Project"

如果没有libs文件夹,您可以创建它。单击显示“Android”的组合框,并将其更改为“项目”

enter image description here

在此处输入图片说明

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 dependenciessection:

将您的 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.enter image description here

步骤 1:为您的项目下载任何 JAR 文件。在此处输入图片说明

Step 2:Copy .jar file and past in libs folder.

第 2 步:复制 .jar 文件并粘贴到 libs 文件夹中。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Step 3:Click on File > Project Structure >Select app > Dependencies

第 3 步:单击文件 > 项目结构 > 选择应用程序 > 依赖项

enter image description here

在此处输入图片说明

Step 4:

第四步:

enter image description here

在此处输入图片说明

Step 5:

第 5 步:

enter image description here

在此处输入图片说明

Step 6:After click Ok button then we can see the Dependencies add like this way:

第 6 步:点击 Ok 按钮后,我们可以看到像这样添加依赖项:

enter image description here

在此处输入图片说明

回答by Alberto Hdez

Example with Parse jar...

带有解析罐的示例...

Add jars to libs folder from Project view … create lib folder if not exists

从项目视图将 jars 添加到 libs 文件夹……如果不存在则创建 lib 文件夹

enter image description here

在此处输入图片说明

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

This is how you can add .jarfile in Android Studio 2.1.3.

这是.jar在 Android Studio 2.1.3 中添加文件的方法。

  1. Copy the .jar file jar file copy screenshot

  2. paste the file in Libs folder and then right click on .jar file and press Add as libraryenter image description here

  3. open build.gradle enter image description here

  4. add lines under dependencies as shown in screenshot enter image description hereenter image description here

  5. Now press play button and you are done adding .jar file enter image description here

  1. 复制 .jar 文件 jar文件复制截图

  2. 将文件粘贴到 Libs 文件夹中,然后右键单击 .jar 文件并按 Add as library在此处输入图片说明

  3. 打开 build.gradle 在此处输入图片说明

  4. 在依赖项下添加行,如屏幕截图所示 在此处输入图片说明在此处输入图片说明

  5. 现在按下播放按钮,你就完成了添加 .jar 文件 在此处输入图片说明

回答by Rahul Sharma

A simple way to add Jar file Android Studio Steps:

Android Studio 添加 Jar 文件的简单方法步骤:

  1. Copy and paste your jar file to libs folder of your project.
  2. Click File from File menu -> Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ? + ; on Mac OS X).
  3. Select Modules at the left panel -> Dependencies tab.
  4. Add... → Project Library → Attach Jar.
  1. 将 jar 文件复制并粘贴到项目的 libs 文件夹中。
  2. 单击文件菜单中的文件 -> 项目结构(在 Windows/Linux 上为 CTRL + SHIFT + ALT + S,在 Mac OS X 上为 ? + ;)。
  3. 在左侧面板中选择 Modules -> Dependencies 选项卡。
  4. 添加... → 项目库 → 附加 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 的方法是在您的项目中添加一个额外的模块。

  1. From the File menu select Project Structure and click on the green plus icon on the top left.
  2. The new Module dialog pops
  3. From the phone and tablet application group select the "Import JAR or AAR package" option and click next.
  4. Follow the steps to create a new module that contains your JAR file.
  5. Click on the entry that corresponds to your main project and select the dependencies tab.
  6. Add a dependency to the module that you created in step 4.
  1. 从文件菜单中选择项目结构,然后单击左上角的绿色加号图标。
  2. 弹出新模块对话框
  3. 从手机和平板电脑应用程序组中选择“导入 JAR 或 AAR 包”选项,然后单击下一步。
  4. 按照以下步骤创建包含 JAR 文件的新模块。
  5. 单击与您的主项目相对应的条目,然后选择依赖项选项卡。
  6. 向您在步骤 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。