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

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


当前回答

所有这些解决方案都过时了。现在在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

在我的例子中,添加的库遗漏了一些依赖项,但不幸的是,Android Studio(0.8.14)对此保密。

不需要手动配置任何东西!我只是添加了缺少的库,并在应用程序构建中使用默认的依赖项配置。Gradle文件,像这样

dependencies {
    compile 'com.google.code.gson:gson:2.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

以上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;

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