简体   繁体   English

将jar库导入android-studio

[英]importing jar libraries into android-studio

android-studio 0.2.7
Fedora 18

Hello, 你好,

I am trying to add the jtwitter jar to my project. 我正在尝试将jtwitter jar添加到我的项目中。

First I tried doing the following: 首先,我尝试执行以下操作:

1) Drag the jtwitter.jar into the root directory of my project explorer, see picture
2) File | project structure
3) Modules | yamba-yamba | dependencies
4) Click the plus sign | jars or directories | and navigate to jtwitter jar | click ok

项目探险家

When I import the jar file I get the following error: 当我导入jar文件时,我收到以下错误:

import winterwell.jtwitter.Twitter;

Cannot resolve symbol winterwell
Gradle: error: package winterwell.jtwitter does not exist

I researched and found that android-studio has some issues and that you have to edit the build.gradle file yourself. 我研究并发现android-studio有一些问题,你必须自己编辑build.gradle文件。

So I tried adding this to my build.gradle file: 所以我尝试将它添加到我的build.gradle文件中:

dependencies {
    compile files('libs/jtwitter.jar')

的build.gradle

And got an error message: cannot resolve symbol dependencies 并收到错误消息: cannot resolve symbol dependencies

Another question, where would the libs folder be. 另一个问题,libs文件夹在哪里。 Does it mean the External Libraries ? 这是指External Libraries吗?

Any help will be most helpfull, 任何帮助都将是最有帮助的,

Try this... 试试这个...

  1. Create libs folder under the application folder. 在应用程序文件夹下创建libs文件夹。
  2. Add .jar files to libs folder. 将.jar文件添加到libs文件夹。
  3. Then add .jar files to app's build.gradle dependency. 然后将.jar文件添加到app的build.gradle依赖项中。
  4. Finally Sync project with Gradle files. 最后使用Gradle文件同步项目。

1.Create libs folder: 1.创建libs文件夹:

在此输入图像描述

2.Add .jar to libs folder: 2.将.jar添加到libs文件夹:

在此输入图像描述

3.Edit app's build.gradle dependency: 3.编辑app的build.gradle依赖:

  • Open app/build.gradle 打开app / build.gradle

在此输入图像描述

4.Sync project with Gradle files: 4.使用Gradle文件同步项目:

  • Finally add .jar files to your application. 最后将.jar文件添加到您的应用程序中。

在此输入图像描述

UPDATE: 更新:

Here I'm going to import org.eclipse.paho.client.mqttv3.jar file to our app module. 在这里,我要将org.eclipse.paho.client.mqttv3.jar文件导入我们的app模块。

  1. Copy your jar file and paste it in directory called libs. 复制jar文件并将其粘贴到名为libs的目录中。

开放的项目结构

  1. Press Ctrl + Alt + Shift + s or just click project structure icon on the toolbar. 按Ctrl + Alt + Shift + s或单击工具栏上的项目结构图标。

项目结构

  1. Then select your module to import .jar file, then select dependencies tab. 然后选择要导入.jar文件的模块,然后选择依赖项选项卡。

加罐子

  1. Click plus icon then select File dependency 单击加号图标,然后选择文件依赖项

文件依赖

  1. Select .jar file path, click OK to build gradle. 选择.jar文件路径,单击“确定”以构建gradle。

jar路径

  1. Finally we're imported .jar file to our module. 最后我们将.jar文件导入到我们的模块中。

导入的jar文件

Happy coding... 快乐的编码......

Updated answer for Android Studio 2 更新了Android Studio 2的答案

The easy and correct way to import a jar/aar into your project is to import it as a module. 将jar / aar导入项目的简单而正确的方法是将其作为模块导入。

New -> Module New - > Module

右键单击主项目和New  - > Module

Select Import .JAR/.AAR Package 选择Import .JAR/.AAR Package

导入.JAR / .AAR包

Select the .JAR/.AAR file and put a module name 选择.JAR / .AAR文件并放置模块名称

选择.JAR / .AAR文件

Add the module as a dependency 将模块添加为依赖项

将模块添加为依赖项

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')
}

In the project right click 在项目中右键单击

-> new -> module
-> import jar/AAR package
-> import select the jar file to import
-> click ok -> done

You can follow the screenshots below: 您可以按照以下屏幕截图进行操作:

1: 1:

步骤1

2: 2:

在此输入图像描述

3: 3:

在此输入图像描述

You will see this: 你会看到这个:

在此输入图像描述

Android Studio 1.0.1 doesn't make it any clearer, but it does make it somehow easier. Android Studio 1.0.1并没有让它更清晰,但确实让它变得更加容易。 Here's what worked for me: 这对我有用:

1) Using explorer, create an ' external_libs ' folder (any other name is fine) inside the Project/app/src folder, where 'Project' is the name of your project 1)使用资源管理器,在Project / app / src文件夹中创建一个' external_libs '文件夹(任何其他名称都可以),其中'Project'是项目的名称

2) Copy your jar file into this 'external_libs' folder 2)将您的jar文件复制到此“external_libs”文件夹中

3) In Android Studio, go to File -> Project Structure -> Dependencies -> Add -> File Dependency and navigate to your jar file, which should be under ' src/external_libs ' 3)在Android Studio中,转到文件 - >项目结构 - >依赖项 - >添加 - >文件依赖项并导航到您的jar文件,该文件应位于' src / external_libs '下

3) Select your jar file and click 'Ok' 3)选择您的jar文件并单击“确定”

Now, check your build.gradle (Module.app) script, where you'll see the jar already added under 'dependencies' 现在,检查你的build.gradle(Module.app)脚本,在那里你会看到已经在'dependencies'下添加的jar

This is how you add jar files from external folders 这是您从外部文件夹添加jar文件的方法

1) Click on File and there you click on New and New Module 1)单击文件,然后单击新建和新模块

2) New Window appears ,,There you have to choose the Import .JAR/.AAR package . 2)出现新窗口,您必须选择导入.JAR / .AAR包。

3) Click on the path option at the top right corner of the window ...And give the whole path of the JAR file . 3)单击窗口右上角的路径选项...并给出JAR文件的完整路径。

4)click on finish. 4)点击完成。

Now you have added the Jar file and You need to add it in the dependency for your application project 现在您已添加了Jar文件,您需要将其添加到应用程序项目的依赖项中

1)Right click on app folder and there you have to choose Open Module Settings or F4 1)右键单击app文件夹,您必须选择Open Module Settings或F4

2)Click on dependency at the top right corner of the current window . 2)单击当前窗口右上角的依赖项。

3)Click on '+' symbol and choose 'Module Dependency' and It will show you the existed JAR files which you have included in your project ... 3)单击“+”符号并选择“模块依赖关系”,它将显示您已包含在项目中的现有JAR文件...

Choose the one you want and click 'OK/Finish' 选择您想要的那个,然后单击“确定/完成”

Thank you 谢谢

Android Studio 1.0 makes it easier to add a .jar file library to a project. Android Studio 1.0可以更轻松地将.jar文件库添加到项目中。 Go to File>Project Structure and then Click on Dependencies. 转到文件>项目结构,然后单击依赖项。 Over there you can add .jar files from your computer to the project. 在那里,您可以将计算机中的.jar文件添加到项目中。 You can also search for libraries from maven. 您还可以从maven搜索库。

This is the way I just did on Android Studio version 1.0.2 这就是我在Android Studio 1.0.2上所做的方式

  • I have created a folder libs in [your project dir]\\app\\src 我在[你的项目目录] \\ app \\ src中创建了一个文件夹
  • I have copied the jtwitter.jar (or the yambaclientlib.jar ) into the [your project dir]\\app\\src\\libs directory 我已将jtwitter.jar (或yambaclientlib.jar )复制到[your project dir] \\ app \\ src \\ libs目录中
  • The following the menu path: File -> Project Structure -> Dependencies -> Add -> File Dependency , Android Studio opens a dialog box where you can drag&drop the jar library. 以下菜单路径: 文件 - >项目结构 - >依赖项 - >添加 - >文件依赖项 ,Android Studio打开一个对话框,您可以在其中拖放 jar库。 Then I clicked the OK button. 然后我点击了确定按钮。

At this point Gradle will rebuild the project importing the library and resolving the dependencies. 此时, Gradle将重建导入库并解析依赖项的项目。

I see so many complicated answer. 我看到这么多复杂的答案。

All this confused me while I was adding my Aquery jar file in the new version of Android Studio. 当我在新版本的Android Studio中添加我的Aquery jar文件时,这一切让我很困惑。

This is what I did : 这就是我做的:

Copy pasted the jar file in the libs folder which is visible under Project view. 复制将jar文件粘贴到项目视图下可见的libs文件夹中。

And in the build.gradle file just added this line : compile files('libs/android-query.jar') 在build.gradle文件中添加了这一行: compile files('libs/android-query.jar')

PS : Once downloading the jar file please change its name. PS:下载jar文件后请更改其名称。 I changed the name to android-query.jar 我将名称更改为android-query.jar

There are three standard approaches for importing a JAR file into Android studio. 将JAR文件导入Android studio有三种标准方法 The first one is traditional way , the second one is standard way , and the last one is remote library . 第一种是传统方式 ,第二种是标准方式 ,最后一种是远程库 I explained these approaches step by step with screenshots in this link: 我通过此链接中的屏幕截图逐步解释了这些方法:

https://stackoverflow.com/a/35369267/5475941 . https://stackoverflow.com/a/35369267/5475941

I hope it helps. 我希望它有所帮助。

If the code for your jar library is on GitHub then importing into Android Studio is easy with JitPack . 如果jar库的代码在GitHub上,那么使用JitPack可以轻松导入到Android Studio中。

Your will just need to add the repository to build.gradle: 您只需要将存储库添加到build.gradle:

allprojects{
 repositories {
    jcenter()
    maven { url "https://jitpack.io" }
 }
}

and then the library's GitHub repository as a dependency: 然后将库的GitHub存储库作为依赖项:

dependencies {
    // ...
    compile 'com.github.YourUsername:LibraryRepo:ReleaseTag'
}

JitPack acts as a maven repository and can be used like Maven Central. JitPack充当maven存储库,可以像Maven Central一样使用。 The nice thing is that you don't have to upload the jar manually. 好消息是您不必手动上传jar。 Behind the scenes JitPack will check out the code from GitHub and compile it. 在幕后,JitPack将检查GitHub中的代码并进行编译。 Therefore it works only if the repo has a build file in it (build.gradle). 因此,只有当repo中有一个构建文件(build.gradle)时,它才有效。

There is also a guide on how to prepare an Android project. 还有一个关于如何准备Android项目的指南。

Avoid redundancy. 避免冗余。 If you have your jars under /libs in your app build.gradle by default you will have 如果你的应用程序build.gradle中的/ libs下面的jar你默认拥有

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ...
}

that is enough to add all the jars you have under /libs 这足以添加/ libs下的所有jar

this is not necessary 这不是必要的

//    compile files('libs/activation.jar')
//    compile files('libs/additional.jar')
//    compile files('libs/mail.jar')

I also faced same obstacle but not able to find out solution from given answers. 我也面临同样的障碍,但无法从给定的答案中找到解决方案。 Might be it's happening due to project path which is having special characters & space etc... So please try to add this line in your build.gradle . 由于具有特殊字符和空格等的项目路径,可能会发生这种情况......所以请尝试在build.gradle添加此行。

compile files('../app/libs/jtwitter.jar')// pass your .jar file name

".." (Double dot) will find your root directory of your project. “..” (Double dot)将找到项目的根目录。

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

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