76

Background

On Android Marshmallow, Google has completely removed the support of Apache HTTP client (link here) because it doesn't have good performance compared to the alternatives.

This might also be the cause for so many apps crashing on Android Marshmallow.

The problem

Google allows you to still use this API, just not as a built in one, by adding this line to the gradle file:

useLibrary 'org.apache.http.legacy'

So, this is what I did:

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
}

And:

android {
    compileSdkVersion 'android-MNC'
    buildToolsVersion "23.0.0 rc3"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.example.user.androidmtest"
        minSdkVersion 'MNC'
        targetSdkVersion 'MNC'
        versionCode 1
        versionName "1.0"
    }

When I tried it, it compiled fine (no errors being shown, and I could run the proof-of-concept app, as it doesn't have any special code), but when I tried using some of the classes that I know that are part of the old API (like "HttpClient" class), I see that it doesn't allow me to do so.

I know it's not recommended to use this solution, but we must have the app ready to work there at least temporarily, till we work 100% on all of the things that should change for Android Marshmallow, and we don't want surprises in the form of crashes.

Here's a screenshot:

enter image description here

The question

Why does it occur? Did I use it correctly?


EDIT: reported about this issue here:

https://code.google.com/p/android/issues/detail?id=181474

4

12 に答える 12

10

useLibrary 'org.apache.http.legacy' は、次のように、Android Studio プロジェクトのメインの build.gradle ファイルで Gradle ツールのバージョンをアップグレードするまで機能しませんでした。

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
}
于 2015-08-24T19:22:26.283 に答える
10

簡単なファイル パス チェックを実行することで、ここで完璧なソリューションを実現できます。実行することによって

   android {
    compileSdkVersion 'android-MNC'
    buildToolsVersion "23.0.0 rc3"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.example.user.androidmtest"
        minSdkVersion 'MNC'
        targetSdkVersion 'MNC'
        versionCode 1
        versionName "1.0"

    }

        getBootClasspath().each{File file ->
           println file.absolutePath
        }
    }
}

以下のようなものが得られます

/Users/"yourname"/Development/android-sdk-macosx/platforms/android-MNC/android.jar /Users/"yourname"/Development/android-sdk-macosx/platforms/android-MNC/optional/org.apache .http.legacy.jar

これで、jar ができました。何らかの理由で、プロジェクトに追加されませんでした。しかし、いつでも手動で追加できると思います。

于 2015-08-05T06:16:12.067 に答える
5

上記の回答は、デバッグ ビルドを実行し、gradle を利用しているビルドをリリースするのに役立ちます。

従来の apache クラスを使用するすべてのプロジェクト インスタンスで、マニフェスト ファイルの application タグ内にこれを挿入します。

<uses-library android:name="org.apache.http.legacy" android:required="false" />

これは、コンパイル中にまだ Eclipse および ant スクリプトを使用しているユーザーにとって役立ちます。

于 2015-11-04T01:40:33.537 に答える
2

にある従来の Apache ライブラリ

[ANDROID_SDK]\platforms\android-23\optional\org.apache.http.legacy.jar 

したがって、プロジェクトライブラリ内にコピーするか、単に使用できます

compile files("${android.getSdkDirectory().getAbsolutePath()}" + File.separator + "platforms" + File.separator + "android-23" + File.separator + "optional" + File.separator + "org.apache.http.legacy.jar")

/app/build.gradle

于 2015-09-17T09:25:03.830 に答える
2

まず、 libs フォルダーでそれを確認する必要があります

Libsフォルダで確認してください apacheライブラリを確認してください

Then add into your gradle file like this 

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.2'

        defaultConfig {
            applicationId "info.tranetech.laundry"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    android {
        useLibrary 'org.apache.http.legacy'
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1
        compile 'com.android.support:design:23.0.1
        testCompile 'junit:junit:4.12'
        compile files('libs/android-async-http-1.4.4.jar')
        compile 'com.google.android.gms:play-services:8.4.0'
    }

これは私のgradleファイルです

外部ライブラリもチェック

于 2016-01-12T10:31:59.657 に答える
2

私はこれがばかげた理由であることを知っていますが、リストで試してみてください...

私は最近この問題を経験しました。パスの長さの制限が原因で、最大 256 文字だと思います。

プロジェクトを再配置すると、ビルドが成功します。うまくいくことを願っています。

于 2015-11-28T09:57:17.133 に答える
0

この問題を解決する簡単な方法は、C:\Users\username\AppData\Local\Android\sdk\platforms です。ここで android-23 を削除し、SDK マネージャーから API 23 を再度更新します。それはあなたの問題を解決します。

于 2015-12-15T10:21:07.653 に答える