Gradle 无法与 'unable to find optional library' 同步

Gradle failed to sync with 'unable to find optional library'

我不得不重新安装我的系统,今天我在 Android Studio 中尝试与 gradle:

同步时出现此错误
Warning: Unable to find optional library: org.apache.http.legacy

我的项目gradle是:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

我的模块 gradle:

apply plugin: 'android'

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

    compileSdkVersion 23
    buildToolsVersion '23.0.2'
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
    }
    buildTypes {
        release {
        }
    }
    productFlavors {
    }
}

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

From the google docs:

To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:

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

我尝试了 this thread 中提到的建议,但它们不起作用。 android Studio 1.5 和 2 预览版的结果相同。

我该如何解决这个问题?

编辑:到目前为止我尝试过的事情:

  1. 将 gradle 类路径版本更改为 1.3.0、1.3.1、1.5.0。

  2. compileSdkVersiontargetSdkVersion 更改为 22。还有 buildToolsVersion 从 23.0.1、23.0.0、22.0.1。

顶级build.gradle - /build.gradle

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

模块特定 build.gradle - /app/build.gradle

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

Android/Sdk/platforms/android-23/optional文件夹中的org.apache.http.legacy.jar添加到app/libs目录并同步你的项目

更新项目 gradle 文件以使用 gradle 版本 1.3.1。

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

也尝试添加 codehause 存储库。

repositories {
        mavenCentral()
        maven{
            url 'http://repository.codehaus.org'
        }
    }

如果上述解决方案不起作用,请尝试这种方式

我最近遇到了这个问题,是路径长度限制导致的,我认为最多 256 个字符。

重新定位您的项目,构建将成功。

在我的例子中,问题实际上是因为我没有在该计算机上安装正确的 SDK。导航到 .../Android/Sdk/platforms/android-23 我可以看到该文件夹​​是空的。通过 SDK 管理器添加 SDK 23 解决了问题并允许我编译。

您应该手动检查您安装的 SDK 版本是否与 build.gradle 文件中 compileSdkVersion 下指定的版本相同。

在我的例子中,它不起作用,因为我在 <sdk-path>\platforms\android-23\optional\ 中缺少 optional.json,目录具有以下内容:

[
  {
    "name": "org.apache.http.legacy",
    "jar": "org.apache.http.legacy.jar",
    "manifest": false
  }
]

创建一个包含上述内容的新 JSON 文件解决了我的问题。

  1. 将“sdk\platforms\android-23\optional\org.apache.http.legacy.jar”复制到您的应用程序模块库目录,然后添加为库;
  2. 如果启用proguard,请编辑“proguard-rules.pro”以保持相关类,否则会发生异常。

经过大量工作,这个解决方案对我有用 . ** Studio\Android\sdk\platforms** 在这里删除你的 android-23 并再次从 sdk 管理器更新 api 23。** . 它会解决你的问题。

为了节省时间,2 个解决方案是最好的并且对我有用。

解决方案 1:

打开您的 android sdk 管理器并重新安装 API 23(删除并重新安装)。

方案二:

下载此文件,其中包含 optional.json 个文件 提取并移动 optional.json 到 :

C:\Users\AppData\Local\Android\sdk\platforms\android-23\optional

您需要在 Android Stuido 项目的 app/libs 中添加 org.apache.http.legacy.jar jar 文件文件夹。

  Jar Location - `<SDK LOCATION>\android-sdk\platforms\android-23\optional`  

为此,只需右键单击您的项目和 select Show in Explorer,然后转到 ...\app\libs 并粘贴到 jar 文件上方,然后将您的项目与 Gradle 文件同步

Module:app

android {
compileSdkVersion 'Google Inc.:Google APIs:23'
buildToolsVersion "24.0.0"
defaultConfig {
    applicationId "<ur_app_id>"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}