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

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


当前回答

在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(),并且应该将库适当地添加到项目中。

有两种方法可以做到这一点。

第一个简单的方法。

复制.jar文件到剪贴板,然后将其添加到libs文件夹。要查看项目中的libs文件夹,请从文件夹上方的组合框中选择项目。

然后右键单击.jar文件,单击“添加为库”,然后选择一个模块,然后确定。 您可以在构建中看到.jar文件。Gradle文件在依赖块。

 dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:21.0.3'
        implementation project(':okhttp-2.0.0')
        implementation 'com.google.code.gson:gson:2.3.1'
    }

第二种方法是:我们可以将.jar文件作为.jar模块导入到模块中,然后将该模块添加到我们想要的任何模块中。

导入模块——>选择你的。jar文件——>而不是导入一个。jar文件——

然后按CTRL+ALT+SHIFT+S——> project sturure——>选择你想要添加一个jar的模块——>Dependencendies——> module Dependency。构建。模块的Gradle会自动更新。

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

文件>新模块…

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

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

我不知道为什么,但我的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