我正在尝试使用新的Android Studio,但我似乎不能让它正常工作。

我正在使用Gson库来序列化/反序列化json对象。但是这个库不知何故没有包含在构建中。

我用MainActivity创建了一个新项目。 复制gson-2.2.3.jar到/libs文件夹中,并将其添加为库依赖项(右键单击->添加为库)。这包括android studio中的jar,因此可以从源文件中引用它。

当我尝试运行项目时,它无法编译,所以我添加:

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

到de .gradle文件中的依赖项。之后,它正确编译,但当运行应用程序时,我得到一个ClassDefNotFoundException。

有人知道我哪里做错了吗?


当前回答

有两种方法可以做到这一点。

第一个简单的方法。

复制.jar文件到剪贴板,然后将其添加到libs文件夹。要查看项目中的libs文件夹,请从文件夹上方的组合框中选择项目。

然后右键单击.jar文件,单击“添加为库”,然后选择一个模块,然后确定。 您可以在构建中看到.jar文件。Gradle文件在依赖块。

 dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:21.0.3'
        implementation project(':okhttp-2.0.0')
        implementation 'com.google.code.gson:gson:2.3.1'
    }

第二种方法是:我们可以将.jar文件作为.jar模块导入到模块中,然后将该模块添加到我们想要的任何模块中。

导入模块——>选择你的。jar文件——>而不是导入一个。jar文件——

然后按CTRL+ALT+SHIFT+S——> project sturure——>选择你想要添加一个jar的模块——>Dependencendies——> module Dependency。构建。模块的Gradle会自动更新。

其他回答

下面是将本地jar文件作为库添加到模块的说明:

Create a 'libs' folder in the top level of the module directory (the same directory that contains the 'src' directory) In the build.gradle file add the following so that your dependencies closure has: dependencies { // ... other dependencies compile files('libs/<your jar's name here>') } Android Studio should have already setup a gradlew wrapper. From the command line, navigate to the top level of your project (the directory that has a gradlew file). Run ./gradlew assemble. This should compile the project with the library. You may need to fix errors in your build.gradle file as necessary. 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: 4.1. Right click on the module in the left hand panel and choose Open Library Settings. 4.2. On the left panel of the dialog, choose Libraries. 4.3. Click the + sign above the panel second from the left -> Java 4.4. Select your local jar and add it to the project. You may need to run the above ./gradlew command one more time

这样做:

在应用程序文件夹下创建libs文件夹。 将.jar文件添加到libs文件夹。 然后添加.jar文件到应用程序的构建。gradle依赖。 最后同步项目与Gradle文件。

在项目中右键单击

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

下面是截图:

1:

2:

3:

你会看到:

我的答案基本上是收集了上面提供的一些正确但不完整的答案。

Open build.gradle Add the following: dependencies { compile 'com.android.support:appcompat-v7:19.+' compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.code.gson:gson:2.3' } This will allow support for two different ways of adding dependencies. The compile fileTree(dir: 'libs', include: ['*.jar']) (as @Binod mentioned) tells the compiler to look under the folder libs for ANY jar. It is a good practice to create such a folder 'libs' which will contain the jar packages that our application needs to use.

但这也将允许对Maven依赖项的支持。编译'com.google.code.gson:gson:2.3'(正如@saneryee所提到的)是另一种推荐的方法,可以在中央远程存储库中添加依赖项,而不是在我们的/libs“本地存储库”中。它基本上是告诉gradle寻找该包的版本,并告诉编译器在编译项目时考虑它(在类路径中)

PS:我两者都用

我发现Android Studio的依赖管理器非常方便和强大的管理第三方依赖(如这里提到的gson)。提供一步一步的指导,这对我来说是有效的(注:这些步骤是针对Android Studio 1.6和Windows平台上的后续版本进行测试的)。

步骤1: 点击“Build > Edit Libraries and Dependencies…”,打开对话框“Project Structure”

步骤2: 选择“app”,然后选择“Dependencies”选项卡。然后选择“Add > 1 Library dependency”

步骤3: 弹出“选择库依赖项”对话框,在搜索中指定“gson”,然后按“搜索按钮”

步骤4: 所需的依赖项将显示在搜索列表中,选择com.google.code.gson:gson:2.7(这是我写答案时的最新版本),按OK

在“项目结构”对话框中按OK。Gradle会相应地更新你的构建脚本。

希望这能有所帮助:)