我正在尝试使用新的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。

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


当前回答

我只需要在build.gradle中添加一行就可以了:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
    implementation 'com.google.code.gson:gson:2.3.1'   ----------> I added this one
}

不要忘记点击右上角的“立即同步”。

我使用的是Android Studio 1.0.1。

其他回答

下面是将本地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

在Android Stuido中,我喜欢用Gradle来管理Gson lib。

在构建中添加以下依赖项。gradle文件。

repositories {mavenCentral()}

dependencies {compile 'com.google.code.gson:gson:2.2.4'}

一切都好。

你也可以看到这篇文章。 Android studio中集成第三方库的最佳方式

在Android Studio 2.1中,我遵循这种方式,

Goto app -> src->主->资产文件夹(如果不可用创建它)->放你的JAR文件

在你的构建中。Gradle像这样添加依赖,

implementation files('src/main/assets/jsoup.jar')
implementation files('src/main/assets/org-apache-xmlrpc.jar')
implementation files('src/main/assets/org.apache.commons.httpclient.jar')
implementation files('src/main/assets/ws-commons-util-1.0.2.jar')

现在同步。 现在您的JAR文件可以使用了。

在android Studio 1.1.0中。 我通过以下步骤解决了这个问题:

1:将jar文件放入libs目录。(仪)

2:打开模块设置,进入依赖项,在左下角有一个加号按钮。点击加号按钮,然后选择“文件依赖”。在这里你可以看到你的jar文件。选择它,它就被解析了。

在app级别添加了libs文件夹。 在这个项目中添加了所有的罐子。 接下来,选择所有的罐子,在libs文件夹中, 右键单击所选项目,并选择添加库 然后,您将在项目资源管理器本身中找到jars扩展选项。

我观察到CTRL + ALT + SHIFT + S——>项目结构——>应用程序模块——>依赖项“已经有一个条目作为(dir: 'libs',包括:'*.jar')下编译选项,最初。在按照上面的步骤添加了罐子之后,构建。Gradle获得了新添加的jar本身的条目。