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

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


当前回答

在项目中右键单击

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

下面是截图:

1:

2:

3:

你会看到:

其他回答

第一步:现在在你的app文件夹下,你应该看到libs,如果你没有看到它,然后创建它。

第二步:在这里拖放。jar文件,你可能会得到一个提示“这个文件不属于项目”,只需点击确定按钮。

步骤3:现在你应该在libs文件夹下看到jar文件,右键单击jar文件并选择“添加为库”,点击确定提示“创建库”

步骤4:现在已经添加了这个罐子。

这样做:

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

我不知道为什么,但我的Android Studio 0.8.14疯狂时,我试图实现这些解决方案使用Gradle。我承认我对这个伟大的构建工具知之甚少,但Studio为什么会破坏我的项目?我设法让它以这种方式工作:将android-support-v13.jar放入“libs”目录,然后在我的项目上F4,并在我指向android-support-v13.jar的地方添加文件依赖。

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

下载并复制你的。jar文件到libs文件夹,然后添加这些行到build.gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.code.gson:gson:2.3.1'
}

别忘了按“立即同步”