无法找到可选库:org.apache.http.legacy

Unable to find optional library: org.apache.http.legacy

尝试了 Whosebug 中的所有方法。我在下面列出了我试图纠正此错误的所有内容:(我知道对此有很多答案。它仍然不适合我)

  1. 已检查是否存在 .json 文件(检查)
  2. 将 "useLibrary 'org.apache.http.legacy'" 添加到应用程序模块 gradle 文件(检查)
  3. 将 .jar 文件从 sdk 位置添加到 libs(检查)
  4. 重新安装API 23(检查)
  5. 右键单击 libs 文件夹中的 jar 并添加到库(勾选)
  6. 更改位置以避免 256 个字符。 (检查)
  7. 将 "compile fileTree(dir: 'libs', include: ['*.jar'])" 添加到 gradle 文件依赖项(默认存在)(检查)

这几乎是所有答案的全部内容。谁能提供解决方案?我已经研究了 10 个小时;重新安装和更改脚本。

我的Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.example.name"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:24.0.0-alpha1'
}

对我来说,我使用这个并且它有效:

android {
    useLibrary 'org.apache.http.legacy' // put it in here
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.name"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

我能够解决警告消息

    Unable to find optional library: org.apache.http.legacy

通过升级我的 Android 工作室。这是 1.3.2 。现在是2.2。

首先确保使用此 gradle 插件或更高版本:

classpath 'com.android.tools.build:gradle:1.3.1'

然后检查 Android/Sdk/platforms/android-23/optional 文件夹是否包含 optional.json 文件。

最好使用最新版本:

classpath 'com.android.tools.build:gradle:2.2.0'

如果没有这个版本,您将无法处理 useLibrary 'org.apache.http.legacy'

这与您的问题无关,但还要检查您的依赖项和用于编译的api。

android {

     compileSdkVersion 24
     //....
}
dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:24.2.1'
}