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

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


当前回答

的编译文件…“以前为我工作,但现在不是了。在经历了很多痛苦之后,我发现用这个方法是有效的:

编译fileTree(dir: 'libs',包括:'*.jar')

我不知道为什么会有不同,但是,至少这该死的东西现在开始工作了。

其他回答

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

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

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

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

以上3个步骤我已经做到了,它的工作魅力对我来说。

(我使用Android Studio 2.1.2)

步骤1

将你的jar包名称(例如编译'com.squareup.okhttp3:okhttp:3.3.1')添加到build.gradle(Module:app)下的gradle构建脚本中。

步骤2: 右键单击app文件夹->新建->模块

步骤3:单击“导入JAR/”。然后浏览您的包。例如:OkHttp.jar

Android Studio 3+:

你只需要简单地将jar文件复制到app文件夹下的libs文件夹即可。

...myproject \ app \ libs myfile . jar

然后从“项目”窗口的下拉菜单中选择“项目文件”,右键单击该项目,选择“同步”以在“项目文件”中查看该文件。它会自动在gradle文件(Module:app)中添加依赖项。

dependencies {
...
    implementation files('libs/myfile.jar')

这里有另一个解决方案:

转到Project Files视图(从下拉菜单中选择Project Files)。

选择新…在app下面创建一个名为libs的文件夹。

打开文件资源管理器,复制并粘贴jar文件到libs文件夹中。

在Android Studio中,右键单击jar文件,并选择Add as a Library…从弹出式菜单。

你应该在gradle文件的依赖项列表中看到这个文件:

dependencies {
...
    implementation files('libs/myfile.jar')
}

打开你的java文件,在那里添加import语句:

import com.xxx.xxx;