17

I had to reinstall my system and today I get this error in Android Studio when I try to sync with gradle:

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

My project gradle is:

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

allprojects {
    repositories {
        mavenCentral()
    }
}

And my module 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'
}

I tried the suggestions mention in this thread but they don't work. Same result with android studio 1.5 and 2 preview.

How can I fix this?

Edit: Things I've tried so far:

  1. Change gradle classpath versions to 1.3.0, 1.3.1, 1.5.0.

  2. Change the compileSdkVersion and targetSdkVersion to 22. Also the buildToolsVersion from 23.0.1, 23.0.0, 22.0.1.

Community
  • 1
  • 1
Daniel
  • 1,075
  • 2
  • 14
  • 26
  • `buildToolsVersion '23.0.1'` use this ,For testing case – IntelliJ Amiya Nov 25 '15 at 11:07
  • 1
    @IntelliJAmiya same error with that version of the `buildToolsVersion`. – Daniel Nov 25 '15 at 11:15
  • The only solution that worked for me is from this thread [Why Warning:Unable to find optional library: org.apache.http.legacy occurs](http://stackoverflow.com/questions/33898857/why-warningunable-to-find-optional-library-org-apache-http-legacy-occurs) – Aleksandar Pavić Feb 25 '16 at 08:39
  • I got the same error message with double/multiple declaration of `org.apache.http.legacy` library. – Ibrahim.H Dec 08 '20 at 17:34

9 Answers9

16

Top level build.gradle - /build.gradle

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

Module specific build.gradle - /app/build.gradle

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

Add org.apache.http.legacy.jar which is in Android/Sdk/platforms/android-23/optional folder to app/libs directory and sync your project

johnrao07
  • 6,690
  • 4
  • 32
  • 55
9

After a lot of working this solutions work for me . ** Studio\Android\sdk\platforms** here delete your android-23 and from sdk manager update api 23 again.** . it will solve your issue.

Naeem Ibrahim
  • 3,375
  • 1
  • 21
  • 21
  • I wonder how it is not the accepted answer, it is really the correct solution, I tried a lot to find solution for this problem and I used all methods written in the internet, this is the only solution that solved my same problem – amal50 Mar 25 '16 at 01:10
8

In my case, it didn't work because I was missing optional.json in <sdk-path>\platforms\android-23\optional\, directory with the following content:

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

Creating a new JSON file with with above content solved the problem for me.

ben_joseph
  • 1,660
  • 1
  • 19
  • 24
  • In my case, I had the folder android-23 with all the content, and the folder ...\optional\ had the jar, but not the file 'optional.json' . So as @notsopopularguy said, I just created it, and refreshed AS. Worked perfect. – Shudy Mar 29 '16 at 10:20
2

You need to add org.apache.http.legacy.jar jar file in your Android Stuido project's app/libs folder.

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

To do this, just right click on your project and select Show in Explorer then go to ...\app\libs and paste above jar file and Sync your Project with Gradle File

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'
    }
}
}
Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
1

Update your project gradle file to use gradle version 1.3.1.

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

Also try adding codehause repo.

repositories {
        mavenCentral()
        maven{
            url 'http://repository.codehaus.org'
        }
    }
Adhikari Bishwash
  • 2,770
  • 28
  • 32
  • Already tried it, does not work. I also tried the latest version which is `1.5.0` I believe and it still doesn't work. – Daniel Nov 25 '15 at 11:04
1

In my case the problem was actually occurring because I didn't have the correct SDK installed on that computer. Navigating to .../Android/Sdk/platforms/android-23 I could see that folder was empty. Adding SDK 23 via the SDK manager fixed the problem and allowed me to compile.

You should manually check that you have the same version of the SDK installed that is specified under compileSdkVersion in your build.gradle file.

Tim Rae
  • 3,167
  • 2
  • 28
  • 35
1

To save time 2 solutions are best and works for me.

Solution 1 :

open your android sdk manager and reinstall API 23 (remove and install again).

Solution 2 :

Download this file which contain optional.json file extract and move optional.json to :

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

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65
0

if above solution is not work then try this way

I experienced this problem recently, and it is caused by the path length restriction I think it´s 256 characters maximum.

Relocate your Project and the build will succeed.

Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
0
  1. Copy "sdk\platforms\android-23\optional\org.apache.http.legacy.jar" to your app module libs directory, then add as library;
  2. If you enable the proguard, please edit "proguard-rules.pro" to keep related classes or exception will occurred.
yuiopt
  • 127
  • 1
  • 1
  • 9