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

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


当前回答

从网站下载图书馆文件 从窗口复制 从项目资源管理器粘贴到lib文件夹 Ctrl+Alt+Shift+S打开项目结构 选择Dependencies选项卡,使用+添加文件 工具栏通过按钮将项目与gradle文件同步

这解决了我的问题。试试,如果有人想知道更多细节,请告诉我。

其他回答

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

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

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

Easy steps to add external library in Android Studio If you are in Android View in project explorer, change it to Project view as below Right click the desired module where you would like to add the external library, then select New > Directroy and name it as 'libs' Now copy the blah_blah.jar into the 'libs' folder Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradle as compile files('libs/blah_blah.jar') and sync the gradle. And you are done Please Note : If you are using 3rd party libraries then it is better to use dependencies where Gradle script automatically downloads the JAR and the dependency JAR when gradle script run. Ex : compile 'com.google.android.gms:play-services-ads:9.4.0' Read more about Gradle Dependency Mangement

在Kotlin DSL (build.gradle.kts)中,有多种方法可以做到这一点。 我们假设库文件在project/app/libs/目录下。

Method 1 implementation( files( "libs/library-1.jar", "libs/library-2.jar", "$rootDir/foo/my-common-library.jar" ) ) Method 2 implementation( fileTree("libs/") { // You can add as many include or exclude calls as you want include("*.jar") include("another-library.aar") // aar is similar to jar exclude("bad-library.jar") } ) Method 3 implementation( fileTree( // Here, instead of repeating include or exclude, assign a list "dir" to "libs/", "include" to "*.jar", "exclude" to listOf("bad-library-1.jar", "bad-library-2.jar") ) ) Method 4 repositories { flatDir { dirs("libs/", "lib/") } } dependencies { // Could also write implementation(":jsoup:1.4.13") implementation("org.jsoup:jsoup:1.4.13") // NOTE: Add @aar suffix for AAR files implementation("ir.mahozad.android:pie-chart:0.7.0@aar") }

您可以在包含和排除调用中使用Ant模式,如上所示。

有关这方面的更多信息,请参阅Gradle文档。

感谢这篇文章和这篇文章提供了有用的答案。

IIRC,简单地使用“Add as library”是不足以让它与项目一起编译的。

查看Intellij关于向项目中添加库的帮助

你应该最感兴趣的部分是:

(在文件>项目结构中)打开模块设置并选择Dependencies选项卡。 在Dependencies选项卡上,单击add并选择Library。 在“选择库”对话框中,选择一个或多个库,然后单击“添加选定”。

如果库没有显示在对话框中,将其添加到Libraries设置中,就在Modules的正下方。

您不应该再需要添加compile files(),并且应该将库适当地添加到项目中。

我已经为同样的事情挣扎了好几个小时,试图让Gson罐子工作。我终于破解了——下面是我采取的步骤:

Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder Right click it and hit 'Add as library' 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) Edit : Use implementation files('libs/gson-2.2.4.jar') (or implementation fileTree(dir: 'libs', include: '*.jar')) in Android Studio 3.0+ 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

在我完成以上四项之后,它开始正常工作。我认为“添加为库”步骤是我之前错过的一个步骤,直到我清理它它才开始工作。

[编辑-添加构建Gradle步骤也是必要的,正如其他人指出的那样]