简体   繁体   中英

What's the difference between principles of useLibrary and compile files('') in build.gradle?

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 API s, you must first declare the following compile-time dependency in our build.gradl e file:

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

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?

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.

But why?

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.gradle to use it? Could I use other jars in this way?

Could somebody provide some ideas about this?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM