I am trying to convert an IntelliJ project to the Gradle system of Android Studio but I am running into errors with Apache HttpClient? Am I missing something, the errors I am getting are as follows:
Error:(10, 30) error: package org.apache.http.client does not exist
Error:(11, 30) error: package org.apache.http.client does not exist
Error:(12, 37) error: package org.apache.http.client.entity does not exist
Error:(13, 38) error: package org.apache.http.client.methods does not exist
Error:(14, 38) error: package org.apache.http.client.methods does not exist
Error:(15, 38) error: package org.apache.http.client.methods does not exist
Error:(16, 35) error: package org.apache.http.impl.client does not exist
Error:(134, 33) error: cannot find symbol class HttpUriRequest
Error:(164, 39) error: cannot find symbol class HttpUriRequest
Error:(106, 17) error: cannot find symbol class HttpGet
Error:(106, 39) error: cannot find symbol class HttpGet
Error:(117, 17) error: cannot find symbol class HttpPost
Error:(117, 40) error: cannot find symbol class HttpPost
Error:(125, 43) error: cannot find symbol class UrlEncodedFormEntity
Error:(135, 9) error: cannot find symbol class HttpClient
Error:(135, 33) error: cannot find symbol class DefaultHttpClient
Error:(155, 18) error: cannot find symbol class ClientProtocolException
Error:(165, 9) error: cannot find symbol class HttpClient
Error:(165, 33) error: cannot find symbol class DefaultHttpClient
Error:(185, 18) error: cannot find symbol class ClientProtocolException
My build.gradle file has the following dependencies:
dependencies {
compile 'com.google.android.gms:play-services:+'
compile 'org.apache.httpcomponents:httpclient:4.2.6'
compile 'org.apache.httpcomponents:httpmime:4.2.6'
compile files('libs/core.jar')
}
It seems a lot of people are getting a similar problem but neither SO or Google have a solution so I am hoping this question will help future searchers.
if you are using target sdk as 23 add below code in your build.gradle
android{
compileSdkVersion 23
buildToolsVersion '23.0.1'
useLibrary 'org.apache.http.legacy'
}
and change your buildscript to
classpath 'com.android.tools.build:gradle:1.3.0'
for more info follow this link
I had this problem and then found these pages:
Here you can see that apache library is deprecated, but it's not removed, so it should work. It doesn't.
See.
And here you can see how to include apache library to your project
See.
I resolved problem by adding following to my build.gradle file as recommended in second link.
android {
useLibrary 'org.apache.http.legacy'
}
However this only works if you are using gradle 1.3.0-beta2 or greater, so you will have to add this to buildscript dependencies if you are on a lower version:
classpath 'com.android.tools.build:gradle:1.3.0-beta2'
Add this library into build.gradle
android {
useLibrary 'org.apache.http.legacy'
}
I suggest you replace the deprecated apache HttpClient with the new HttpURLConnection.
That's a cleaner solution, it's quite easy to migrate, and generally it's better to stick to the latest SDK changes than trying to hack/patch/workaround: you usually regret it later :)
Step 1
HttpGet httpGet = new HttpGet(url);
becomes:
URL urlObj = new URL(url);
Step 2
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpGet, localContext);
InputStream is = response.getEntity().getContent();
becomes:
HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
InputStream is = urlConnection.getInputStream();
Step 2 bis
int status = response.getStatusLine().getStatusCode();
becomes:
int status = urlConnection.getResponseCode();
copy org.apache.http.legacy.jar which is in Android/Sdk/platforms/android-23/optional folder to to app/libs
and also added this line to app.gradle file
compile files('libs/org.apache.http.legacy.jar')
But if you're using more jar libraries, you can use this way
compile fileTree(dir: 'libs', include: ['*.jar'])
Basically all you need to do is add:
useLibrary 'org.apache.http.legacy'
To your build.gradle file.
I ran into similar problems, you might be able to get it to work using a similar method.
First, try this with your current configuration, exclude httpclient from httpmime:
dependencies {
compile 'com.google.android.gms:play-services:+'
compile ('org.apache.httpcomponents:httpmime:4.2.6'){
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient:4.2.6'
}
In my case, I fixed it by using the following jars :
httpclient-android-4.3.5.1.jar
httpmime-4.3.5.jar
Then, in the build.gradle, excluding httpclient from httpmime:
dependencies {
compile 'com.google.android.gms:play-services:+'
compile('org.apache.httpcomponents:httpmime:4.3.5') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
}
I ran into the same issue. Daniel Nugent's answer helped a bit (after following his advice HttpResponse was found - but the HttpClient was still missing).
So here is what fixed it for me:
(if not already done, commend previous import-statements out)
visit http://hc.apache.org/downloads.cgi
get the 4.5.1.zip from the binary section
unzip it and paste httpcore-4.4.3 & httpclient-4.5.1.jar in project/libs folder
right-click the jar and choose Add as library.
Hope it helps.
Perfect Answer by Jinu and Daniel
Adding to this
I solved the Issue by Using This, if your compileSdkVersion is 19(IN MY CASE)
compile ('org.apache.httpcomponents:httpmime:4.3'){
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile ('org.apache.httpcomponents:httpcore:4.4.1'){
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile 'commons-io:commons-io:1.3.2'
else if your compileSdkVersion is 23
then use
android {
useLibrary 'org.apache.http.legacy'
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
This is what I did, and it works for me.
step 1: add this in the build.grade(module: app)
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
step 2: sync the project and done.
After spending days on the subject, I managed to solve it finally. Unfortunately we need it if we want to use expansion files. The solution has been provided by a few people here, but for me the answers were missing a few minor detail that could have saved a lot of time for me. Here is the sequence of events for novices like me to save your precious time.
First I imported the "library" folder from D:\Users\Imre\AppData\Local\Android\sdk1\extras\google\play_licensing to the project by going into File, New, Import Module and navigating to the "library" folder. If you open the SDK Manager and click on "Launch Standalone SDK Manager" in the bottom of the popup screen, you can hover your pointer over the "Extras" folder down in the bottom and the small yellow info will tell you where you will find the package that you need to import.
Once that is done, go down to the "library" section in the left pane with "Project" and within it the "Android" tab open. Open the java section of the library and open the APKExpansionPolicy class.
If you have errors and the import org.apache.http.NameValuePair and the import org.apache.http.client.utils.URLEncodedUtils are pale grey, open the build.gradle(Project:whatever) and make sure that in "buildscript" under "dependancies" you have the "classpath 'com.android.tools.build:gradle:1.5.0" included. The 1.5.0 might be different in your case. I guess it depends on your Studio version. Mine is 1.5.1. If yours is newer, you will be reminded to update the numbers.
After that, go to "build.gradle(Module:library) and include the "useLibrary 'org.apache.http.legacy' into the "android"section. Sync Now (R/H top corner) should be offered. Do it.
If you get further error messages (I wouldn't know it because I did it the other way round), the Apache files might be missing from your project, as apache is not supported anymore. Find them in C/D:Users/Yourname/AppData/Local/Android/Sdk/platforms/android-23/optional and copy/paste them into yourproject/app/libs folder. You should have an optional.json and an org.apache.http.legacy.jar file in there. You might be better off going to apache's website and dowloading the newest version: http://hc.apache.org/downloads.cgi Unzip it anywhere you want, open it, go to "lib", copy the httpclient-4.5.1.jar file and replace the org.apache.http.legacy.jar file with it.
Sync and rebuild/clean your project and it should be good to go.
Just in case, open your terminal (L/H bottom of Android Studio) and type in "gradlew clean". It will install a few things. Once it is done, type in "gradlew assemble". It will take a few minutes to finish, but it will give you the errors if you have any of course.
If you cannot type in anything in the Terminal, launch command prompt (Windows button+R), type in cmd, hit OK, right click on the title of the small black popup screen (C:\WINDOWS\system32\cmd.exe), Properties, Options, in the bottom of the window check that "Use legacy console" is thicked. Restart A.Studio.
Good luck!
The version of the Apache HTTP client provided on stock Android was very very old.
Google Android 1.0 was released with a pre-BETA snapshot of Apache HttpClient. To coincide with the first Android release Apache HttpClient 4.0 APIs had to be frozen prematurely, while many of interfaces and internal structures were still not fully worked out. As Apache HttpClient 4.0 was maturing the project was expecting Google to incorporate the latest code improvements into their code tree. Unfortunately it did not happen.
While you could keep using the old deprecated library via the useLibrary 'org.apache.http.legacy' workaround (suggested by #Jinu and others), you really need to bite the bullet and update to something else, for example the native Android HttpUrlConnection, or if that doesn't meet your needs, you can use the OkHttp library, which is what HttpUrlConnection is internally based upon anyway.
OkHttp actually has a compatibility layer that uses the same API as the Apache client, though they don't implement all of the same features, so your mileage may vary.
While it is possible to import a newer version of the Apache client (as suggested by #MartinPfeffer), it's likely that most of the classes and methods you were using before have been deprecated, and there is a pretty big risk that updating will introduce bugs in your code (for example I found some connections that previously worked from behind a proxy no longer worked), so this isn't a great solution.
I had to post as none of the above answers worked completely for me.
I am using Android Studio
classpath 'com.android.tools.build:gradle:1.5.0'
compileSdkVersion 23
buildToolsVersion "23.0.3"
Step 1: Download lastest jar file (http://www-eu.apache.org/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.2-bin.zip)
Step 2: Copy paste the .jar file to the libs folder (create if does not exist already) in your module (can be app or library)
Step 3: Right click on the jar and "Add as Library". It will automatically add the jar file as a dependency in your module's gradle file
Step 4: Now automatically your problem will get resolved but in case you are using proguard in your app, it will give you warning about duplicate class files and won't let you build. It is a known bug and you need to add following to your proguard-rules
-dontwarn org.apache.commons.**
-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**
Good Luck!
In my case, I updated one of my libraries in my android project.
I'm using Reservoir as my cache storage solution: https://github.com/anupcowkur/Reservoir
I went from:
compile 'com.anupcowkur:reservoir:2.1'
To:
compile 'com.anupcowkur:reservoir:3.1.0'
The library author must have removed the commons-io library from the repo so my app no longer worked.
I had to manually include the commons-io by adding this onto gradle:
compile 'commons-io:commons-io:2.5'
https://mvnrepository.com/artifact/commons-io/commons-io/2.5
I cloned the following: https://github.com/google/play-licensing
Then I imported that into my project.
Related
I'm using an 'aar' library which I created.
In both, my project and the library, there is a dependency implementation of Conceal library (each from its own lib folder).
When I build the project after importing the library and using ProGuard obfuscation, I get this error message:
Error: Program type already present: com.facebook.crypto.cipher.NativeGCMCipher
How can I resolve this problem?
this error say you are importing the dependency which already imported in project.
solution :- remove or exclude this dependency
ex:-
compile ('com.github.ganfra:material-spinner:1.1.1'){
exclude group: 'com.nineoldandroids'
}
according to mavenCentral(), this is the package name (which could be used instead of .jar):
// https://mvnrepository.com/artifact/com.facebook.conceal/conceal
implementation "com.facebook.conceal:conceal:2.0.2"
therefore the exclusion should look about like this:
implementation( project(":libraryproject") ) {
exclude group: "com.facebook.conceal"
}
To my understanding, the error means that I imported a dependency that already imported in the project (once in the project and once in the library).
The suggested solutions of #Mayur Dabhi and #Martin Zeitler had the right approach, but unfortunately, I wasn't able to get the exclude command working.
finally, with the help of #Martin Zeitler, I replaced:
implementation files('libs/conceal_android.jar')
implementation files('libs/libconceal.jar')
with:
implementation "com.facebook.conceal:conceal:2.0.2"
meaning I removed the 'Conceal' jar files from the 'lib' folder and imported the dependency. After that, the error message disappeared and I managed to build the project.
Thanks for anyone who tried to help :)
I want to import org.apache.http.client.utils.URLEncodedUtils in android studio. It have to add library in build gradle I add this code
implementation 'org.apache.httpcomponents:httpclient:4.5.6'
It show error like this.
httpclient defines classes that conflict with classes now provided by
Android. Solutions include finding newer versions or alternative
libraries that don't have the same problem (for example, for
httpclient use HttpUrlConnection or okhttp instead), or repackaging
the library using something like jarjar.
If I remove implementation in build gradle it show error can not resolv symbol URLEncodedUtils.
How to import library URLEncodedUtils android studio ?
Remove implementation 'org.apache.httpcomponents:httpclient:4.5.6'
and add this to your gradle:
android {
useLibrary 'org.apache.http.legacy'
}
You also may try to download and include HttpClient jar directly into your project or use OkHttp instead.
Hope this help!
Add next block to build.gradle located in app module
configurations {
all {
exclude module: 'httpclient'
}
}
After upgrading to Android Studio 3.1, I started to get following error during build. Project uses multidex and DX is enabled by default as you would notice in the error. I tried to check dependency graph to understand what is going on but so far have no clue. Interestingly this only fails on my machine. I cleaned up everything, including reinstall etc but nothing worked.
Anyone had the same issue and how did you solve it? Or any direction that I can take a look?
AGPBI: {
"kind":"error",
"text":"Program type already present: android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat",
"sources":[{}],
"tool":"D8"
}
This is the task that fails:
transformDexArchiveWithExternalLibsDexMergerForDebug
I checked similar issues and it seems random things fixes their problem, I'm not sure what is the real cause.
For my solution (I do not know it will work for you):
Firstly I followed #Orhan Obut's solution:
Search for duplicate classes in your project
I found that there are more than one class files in different libraries.
Then I put the ignore annotation above my support dependency in my project module's build.gradle (app folder):
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
I realized that ignorance is no solution, because the error did not go away, even after clean-rebuilding and clearing/invalidating cache for the project.
See: Infographic: 11 Most Common Android Errors and How to Fix Them
So I explored more, and found out this link:
Android - Understanding and dominating gradle dependencies
It suggests ways to resolve conflicts. Hence I put this on my gradle just above the declarations of dependencies:
configurations.all {exclude group: 'com.android.support', module: 'support-v4'}
Since then when I search for duplicate classes for this one using #Orhan Obut's solution above, I find only single entry in the result. That meant that there were no duplicates.
Also, it will be better if you migrate to AndroidX with latest SDK and build tools. Make sure you don't have older support dependencies anywhere.
Happy Coding :-)
I have my solution by change this:
implementation 'com.android.support:appcompat-v7:27.0.0'
to
implementation 'com.android.support:appcompat-v7:26.0.0'
it works for me.
I managed to determine the root cause by using the following steps. It may be different use case for each issue, therefore this is the way to determine the root cause.
Go to android studio
Navigate -> Class
Check include non-project classes
Copy paste full class path with package name. android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat
You should be able to see where it is used. Most probably you may need to remove it from one of them.
In my case the issue was ViewPagerIndicator library was downloading support library as jar. Removing it solved the issue.
For the easy option just add
configurations.all {exclude group: 'com.android.support', module: 'support-v4'}
before dependencies in build.gradle app module, it should ignore v4 support libraries, and the duplicate error will go away.
Adding the below line in the build.gradle of the app level worked for me
implementation 'com.android.support:support-v4:28.0.0'
As for me this helps to resolve such issues
all support libraries (also included thirdy-part) reduces to specified version
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '28.0.0-beta01'
}
}
}
}
I also faced the same problem just a while ago. In my case the third party library used the older AccessibilityServiceInfoCompat version v4 22 and i already updated to newer one v4 28 so both support library classes clashed
In y case I've resolved issue by
implementation 'com.android.support:appcompat-v7:26.0.0'
to
implementation 'com.android.support:appcompat-v7:27.1.1'
some third-party library may be use different version of support library. you can use ./gradlew :app:dependencies find out it, and then import the current version of the support library.
I have my solution by change this :
android / build.gradle
buildscript {
ext {
supportLibVersion = "27.0.3"
}
}
to
buildscript {
ext {
supportLibVersion = "26.0.0"
}
}
directory android / app / build.gradle
defaultConfig {
multiDexEnabled true
}
In my android library I am using ceddl4j library but when I build my library and try to add it's aar file in another Application, it is throwing error
Error:(75, 44) error: cannot access DigitalData
class file for nl.mirabeau.ceddl4j.DigitalData not found
Adding ceddl4j into my library with
compile group: 'nl.mirabeau', name: 'ceddl4j', version: '1.0.0'
I am surely missing something in build configuration of my library, can someone please guide.
Thanks to Grabriele for providing the information that aar file does not contain the nested dependencies, hence the option left is maven repo (public or private).
I'm trying to use HttpPost as I did in the past in Android. I realize that it has been removed from recent Android versions. Following the solutions on a similar StackOverflow question, I added this to my Gradle dependencies:
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
After a Gradle sync that complained of no errors, I still cannot import the HttpPost program. In fact, the org.apache.http.client.methods package does not resolve at all.
I also tried what the Apache HttpClient website suggested, which was to use this instead in my Gradle dependencies:
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
Once again, the sync went fine, and this time the package resolved, but not the HttpPost program.
I'd be grateful for any suggestions.
Just add this line in your build.gradle inside android plugin :
useLibrary 'org.apache.http.legacy'
The Apache HttpClient 4.3 for Android meant to be an update for the legacy 4.0 alpha version that was included in early Android versions. To avoid naming conflicts some on the updated classes got the postfix HC4, like HttpPostHC4 for example, which one had to use instead.
But now that the old HttpClient was removed form Android, the old classes like HttpPost do not exist anymore.
The official way to get the HttpClient library on newer Android version is to use:
android {
useLibrary 'org.apache.http.legacy'
}
as documented here.