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

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


当前回答

menu File -> project struct -> module select "app" -> dependencies tab -> + button 
-> File dependency -> PATH/myfile.jar

其他回答

1)在Project/app/src文件夹中创建一个your_libs文件夹。

2)复制jar文件到your_libs文件夹

3)在Android Studio中,点击文件->项目结构->依赖项->添加->文件依赖项,然后导航到你的jar文件,它应该在src/your_libs下

3)选择jar文件,点击“确定”

然后你可以看到你的构建。Gradle是这样的:编译 文件(src / your_libs / your.jar)

所有这些解决方案都过时了。现在在Android Studio中非常简单:

文件>新模块…

下一个屏幕看起来很奇怪,就像你选择了一些小部件或其他东西,但保留了它 在第一张图片和下面滚动,找到“Import JAR or .AAR Package”

然后从文件菜单中选择项目结构。从打开的窗口中选择app,然后选择依赖项,然后按绿色加号按钮,选择模块依赖项,然后选择导入的模块,然后按OK

下面是将本地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文件放到Android项目的libs文件夹中。

然后在应用程序的gradle文件中添加这行代码:

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

对于Android gradle插件3.0及以后版本,最好使用这个:

    implementation fileTree(dir: 'libs', include: ['*.jar'])

1. 将jar(在我的例子中是gson-2.2.4.jar)放入libs文件夹。 2. 确保构建中有编译文件(libs/gson-2.2.4.jar)。gradle文件。 3.现在点击“同步项目与Gradle文件”(顶部栏左侧到AVD管理器按钮)。

在我完成以上三项之后,它开始正常工作。