useLibrary的原理与build.gradle中的compile files('')原理有什么区别? [英] What's the difference between principles of useLibrary and compile files('') in build.gradle?

本文介绍了useLibrary的原理与build.gradle中的compile files('')原理有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布Android 6.0之后,已删除了对 Apache HTTP客户端的支持.如果我们的应用程序正在使用此客户端并且目标为 Android 2.3(API级别9)或更高版本,则建议使用HttpURLConnection类.据说此API效率更高,因为它通过透明压缩和响应缓存减少了网络使用,并最大程度地降低了功耗.如果我们要继续使用 Apache HTTP API ,则必须首先在我们的build.gradl e文件中声明以下编译时依赖项:

After Android 6.0 releases, Support for the Apache HTTP client is removed. If our app is using this client and targets Android 2.3 (API level 9) or higher, HttpURLConnection class is recommended. It's said that this API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. If we want to continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in our build.gradle file:

android {
    useLibrary 'org.apache.http.legacy'
}

旧版jar位于 Android SDK 中,其路径为sdk/platforms/android-23/optional/.因此,它几乎是独立的.同时,此apache旧版jar放入Android SDK的optional/中,那么optional/是什么?是什么意思?

The legacy jar is in Android SDK, whose path is sdk/platforms/android-23/optional/. So, it is nearly independent. Meanwhile, this apache legacy jar is putted into optional/ in Android SDK, so what is optional/? What does that mean?

我们也知道,我们可以将这个jar放入libs中,然后像下面这样在我们的build.gradle文件中声明它:

Also we know, we can put this jar into libs and then declare it in our build.gradle file like this:

dependencies {
    compile files('libs/org.apache.http.legacy.jar')
}

这两种方法在我测试时都能按预期工作.

Both methods worked as expected when I tested.

但是为什么?

build.gradle中的useLibrarycompile files('')有什么区别?仅因为旧版jar文件位于android SDK中,所以我可以在build.gradle中声明useLibrary来使用它?我可以这样使用其他罐子吗?

What's the difference between useLibrary and compile files('') in build.gradle? Only because the legacy jar file is in android SDK so I can declare useLibrary in build.gradleto use it? Could I use other jars in this way?

有人可以提供一些想法吗?

Could somebody provide some ideas about this?

推荐答案

useLibrary在编译时将库添加到类路径中,但不将该库与应用程序捆绑在一起.

useLibrary adds the library to classpath while compiling but does not bundle the library with the application.

compile依赖项在编译时位于类路径中,此外,它们还随您的APK一起提供.

compile dependencies are in classpath at compile time and additionally they get shipped with your APK.

要获得Apache HttpClient支持,请在使用SDK 23+进行编译时使用useLibrary.该库已经在目标平台中了.它只是在编译SDK中不存在.

For the Apache HttpClient support, use useLibrary when compiling with SDK 23+. The library is already there in the target platform. It is just not there in the compile SDK.

这篇关于useLibrary的原理与build.gradle中的compile files('')原理有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆