我正在尝试使用新的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 1.0.2,以下内容已经在您的构建中。gradle文件

implementation fileTree(include: ['*.jar'], dir: 'libs')

将库jar添加到你的libs文件夹->右键单击库->单击添加为库->它会要求你为项目添加它->选择你的项目->单击确定 下面的行会自动添加到build.gradle中

implementation files('libs/android-query.jar')

这对我很有帮助。没有别的要求了。我已经为android演示了另一个第三方库。

其他回答

1)在Project/app/src文件夹中创建一个your_libs文件夹。

2)复制jar文件到your_libs文件夹

3)在Android Studio中,点击文件->项目结构->依赖项->添加->文件依赖项,然后导航到你的jar文件,它应该在src/your_libs下

3)选择jar文件,点击“确定”

然后你可以看到你的构建。Gradle是这样的:编译 文件(src / your_libs / your.jar)

将.jar文件放到Android项目的libs文件夹中。

然后在应用程序的gradle文件中添加这行代码:

    compile fileTree(dir: 'libs', include: ['*.jar'])

对于Android gradle插件3.0及以后版本,最好使用这个:

    implementation fileTree(dir: 'libs', include: ['*.jar'])

我只需要在build.gradle中添加一行就可以了:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
    implementation 'com.google.code.gson:gson:2.3.1'   ----------> I added this one
}

不要忘记点击右上角的“立即同步”。

我使用的是Android Studio 1.0.1。

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

在项目中右键单击

-> new -> module
-> import jar/AAR package
-> import select the jar file to import
-> click ok -> done

下面是截图:

1:

2:

3:

你会看到: