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

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


当前回答

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

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

其他回答

在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文件可以使用了。

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

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

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:我两者都用

在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文档。

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

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

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