Gradle无法与“无法找到可选库”同步

时间:2015-11-25 10:52:21

标签: android android-studio gradle build.gradle

我不得不重新安装我的系统,今天当我尝试与gradle同步时,我在Android Studio中收到此错误:

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

我的项目是:

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

  

要继续使用Apache HTTP API,您必须先声明   跟随build.gradle文件中的编译时依赖项:

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.还有来自23.0.1,23.0.0,22.0.1的buildToolsVersion。

9 个答案:

答案 0 :(得分:14)

顶级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 目录并同步项目

答案 1 :(得分:9)

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

答案 2 :(得分:8)

在我的情况下,它没有用,因为我在optional.json目录中缺少<sdk-path>\platforms\android-23\optional\,目录中包含以下内容:

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

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

答案 3 :(得分:2)

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

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

要执行此操作,只需右键单击您的项目并选择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'
    }
}
}

答案 4 :(得分:1)

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

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

还尝试添加codehause repo。

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

答案 5 :(得分:1)

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

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

答案 6 :(得分:1)

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

解决方案1:

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

解决方案2:

下载包含optional.json文件的文件 提取并将optional.json移动到:

  

C:\用户\\应用程序数据\本地\的Android \ SDK \平台\机器人-23 \可选

答案 7 :(得分:0)

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

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

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

答案 8 :(得分:0)

  1. 复制&#34; sdk \ platforms \ android-23 \ optional \ org.apache.http.legacy.jar &#34;到您的app模块libs目录,然后添加为库;
  2. 如果你启用了proguard,请编辑&#34; proguard-rules.pro &#34;保持相关课程或将发生例外。