Android Studio:将jar添加为库?

本文翻译自:Android Studio: Add jar as library?

I'm trying to use the new Android Studio but I can't seem to get it working correctly. 我正在尝试使用新的Android Studio,但我似乎无法让它正常工作。

I'm using the Gson library to serialize/deserialize JSON-objects. 我正在使用Gson库来序列化/反序列化JSON对象。 But the library somehow isn't included in the build. 但是库不知何故不包含在构建中。

I had created a new project with just a MainActivity . 我创建了一个只有MainActivity的新项目。 Copied gson-2.2.3.jar in the /libs folder and added it as a library dependancy(right click->Add as library). 在/ libs文件夹中复制gson-2.2.3.jar并将其添加为库依赖项(右键单击 - >添加为库)。 This includes the jar in android studio so it can be referenced from the source files. 这包括android studio中的jar,因此可以从源文件中引用它。

When I try to run the project it cannot compile so I added: 当我尝试运行该项目时,它无法编译,所以我添加:

compile files('libs/gson-2.2.3.jar')

to the dependencies in de .gradle file. 到de .gradle文件中的依赖项。 After that it compiles correctly but when running the application I get a ClassDefNotFoundException . 之后它正确编译,但在运行应用程序时,我得到一个ClassDefNotFoundException

Does anyone know what I'm doing wrong? 有谁知道我做错了什么?


#1楼

参考:https://stackoom.com/question/17gX9/Android-Studio-将jar添加为库


#2楼

IIRC, simply using "Add as library" isn't enough for it to compile with the project. IIRC,仅仅使用“添加为库”还不足以使用该项目进行编译。

Check Intellij's help about adding libraries to a project 检查Intellij有关向项目添加库的帮助

The part that should interest you the most is this: 你最感兴趣的部分是:

(In File > Project Structure ) Open the module settings and select the Dependencies tab. (在“ File > Project Structure )打开模块设置,然后选择“依赖关系”选项卡。

On the Dependencies tab, click add and select Library. 在“依赖关系”选项卡上,单击“添加”并选择“库”。

In the Choose Libraries dialog, select one or more libraries and click Add Selected. 在“选择库”对话框中,选择一个或多个库,然后单击“添加所选项”。

If the library doesn't show up in the dialog, add it in the Libraries settings, right below Modules. 如果库未显示在对话框中,请将其添加到“库”设置中,位于“模块”下方。

You shouldn't need to add compile files() anymore, and the library should be properly added to your project. 您不应再需要添加compile files() ,并且应该将库正确添加到项目中。


#3楼

I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less. 我已经在同样的事情上苦苦挣扎了好几个小时,试图让Gson jar继续工作。 I finally cracked it – here are the steps I took: 我终于破解了它 - 这是我采取的步骤:

  1. Put the Gson jar (in my case, gson-2.2.4.jar ) into the libs folder 把Gson jar(在我的例子中, gson-2.2.4.jar )放到libs文件夹中
  2. Right click it and hit 'Add as library' 右键单击它并点击“添加为库”
  3. Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files) 如果你使用的是很多jar文件,请确保compile files('libs/gson-2.2.4.jar')build.gradle文件中(或者compile fileTree(dir: 'libs', include: '*.jar')

    Edit : Use implementation files('libs/gson-2.2.4.jar') (or implementation fileTree(dir: 'libs', include: '*.jar') ) in Android Studio 3.0+ 编辑:在Android Studio 3.0+中使用implementation files('libs/gson-2.2.4.jar') (或implementation fileTree(dir: 'libs', include: '*.jar')

  4. Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean . I'm on Mac OS X, the command might be different on your system 做一个干净的构建(你可以在Android Studio中做到这一点,但要确保我在终端导航到我的应用程序的根文件夹并键入gradlew clean 。我在Mac OS X上,命令可能不同你的系统

After I did the above four, it started working fine. 在我完成上述四个之后,它开始正常工作。 I think the 'Add as library' step was the one I'd previously missed, and it didn't work until I cleaned it either. 我认为'添加为库'步骤是我之前错过的那个步骤,直到我清理它之前它才行。

[Edit - added the build.gradle step which is also necessary as others have pointed out] [编辑 - 添加了build.gradle步骤,这也是必要的,正如其他人指出的那样]


#4楼

On Mac OS X: 在Mac OS X上:

  1. Add jar as library (drag jar to libs, right click add as lib) 添加jar作为库(将jar拖到libs,右键单击add as lib)

  2. Add compile statement to build.grade 将compile语句添加到build.grade

  3. Install gradle v1.6 (use homebrew) 安装gradle v1.6 (使用自制程序)

    • brew install gradle brew安装gradle
    • gradle -v gradle -v
    • if not v1.6, upgrade homebrew 如果不是v1.6,升级自制程序
  4. gradle clean (rebuild android did not work) gradle clean(重建android没有用)

This sorted me out. 这把我排除在外。


#5楼

Here are the instructions for adding a local jar file as a library to a module: 以下是将本地jar文件作为库添加到模块的说明:

  1. Create a 'libs' folder in the top level of the module directory (the same directory that contains the 'src' directory) 在模块目录的顶层创建一个'libs'文件夹(包含'src'目录的同一目录)

  2. In the build.gradle file add the following so that your dependencies closure has: build.gradle file添加以下内容,以便您的依赖项闭包具有:

     dependencies { // ... other dependencies compile files('libs/<your jar's name here>') } 
  3. Android Studio should have already setup a gradlew wrapper. Android Studio应该已经设置了gradlew包装器。 From the command line, navigate to the top level of your project (the directory that has a gradlew file). 从命令行,导航到项目的顶级(具有gradlew文件的目录)。

    Run ./gradlew assemble . 运行./gradlew assemble This should compile the project with the library. 这应该用库来编译项目。 You may need to fix errors in your build.gradle file as necessary. 您可能需要根据需要修复build.gradle文件中的错误。

  4. In order to have Android Studio recognize the local jar files as libraries for support while coding in the IDE, you need to take a few more steps: 为了让Android Studio在IDE中进行编码时将本地jar文件识别为支持库,您需要采取以下几个步骤:

    4.1. 4.1。 Right click on the module in the left hand panel and choose Open Library Settings . 右键单击左侧面板中的模块,然后选择“ Open Library Settings

    4.2. 4.2。 On the left panel of the dialog, choose Libraries . 在对话框的左侧面板中,选择“ Libraries

    4.3. 4.3。 Click the + sign above the panel second from the left -> Java 单击左侧第二个面板上方的+号 - > Java

    菜单

    4.4. 4.4。 Select your local jar and add it to the project. 选择本地jar并将其添加到项目中。

  5. You may need to run the above ./gradlew command one more time 您可能需要再次运行上面的./gradlew命令


#6楼

'compile files...' used to work for me, but not any more. '编译文件......'曾经为我工作,但不再是。 after much pain, I found that using this instead works: 经过多次痛苦,我发现使用它可以起作用:

compile fileTree(dir: 'libs', include: '*.jar')

I have no idea why that made a difference, but, at least the damn thing is working now. 我不知道为什么会有所作为,但是,至少该死的东西现在正在发挥作用。

发布了0 篇原创文章 · 获赞 73 · 访问量 55万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105309374