54437

Cannot resolve R.java, duplicate class

Question:

I just start using Android Studio for a week and it works great for me, but when I started Android Studio today I get the error: 'error: duplicate class: mypackage.R'. I saw this error before when I used Eclipse so I tried to rebuild the project a few times and restarting Android Studio, this didn't help.

After reading some Stackoverflow questions I tried to deleted R.java and rebuild again, now I don't get any error while rebuilding. The only problem is that Android Studio cannot resolve R, so every line which uses R gets an error (project builds and run).

I also tried to delete all the R.class files but this doesn't help either. I checked if some class imports android.R, but they all don't, they just import 'mypackage.R'. I even deleted all my files and checked out a older version of my project but I still have the same problem.

Edit:

If I don't delete R.java the compiler doesn't give any error. Just when I try to build I get a lot of errors like: 'error: cannot find symbol variable button_login'. If I search the R.java file for 'button_login' I just find it. I did this with multiple errors.

Edit2:

When I deleted all the libaries the project now builds. But the problem is that I need those libaries (HTTPComponents). I added them from maven (File --> Project Structure --> Libaries --> +-sign') and added them to my settings.graddle:

buildscript { repositories { maven { url 'http://repo1.maven.org/maven2' } } dependencies { classpath 'com.android.tools.build:gradle:0.4' } } apply plugin: 'android' dependencies { compile files('libs/android-support-v4.jar') compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.2.5' compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.2.5' //compile 'org.apache.httpcomponents-httpclient:4.2.5' doesn't work either //compile 'org.apache.httpcomponents-httpmime:4.2.5' doesn't work either } android { compileSdkVersion 17 buildToolsVersion "17.0.0" defaultConfig { minSdkVersion 15 targetSdkVersion 16 } }

When I run gradlew.bat --stacktrace --info assemble I get errors that Gradle is unable to find the libaries from the maven repository. I checked the repository but they are available. <a href="http://search.maven.org/#browse%7C1628757896" rel="nofollow">http://search.maven.org/#browse%7C1628757896</a>

Answer1:

Unfortunately I run into this as well, sometimes frequently, mostly happens when I put my computer to sleep and back on while Eclipse is on. Sometimes recompiling and validating the project works.

I did find this solution online somewhere, lost the link and do not claim credits for this:

<ul><li>Disable auto build, and clean the project then</li> <li>Right click your project -> properties -> Java Build Path -> Source -> Add Folder -> add gen and src</li> <li>Then compile</li> </ul>

If that doesn't work, try to delete the gen folder and do that process

Let me know.

Answer2:

Delete the Build folder generated by Android Studio automatically!

Answer3:

Problem is simple usually. You have fresh instal of studio? So you just fogot add SDK support for those version Android in wich try to compile project. Android Studio can`t generate R file. Start SDK manager, install all missing SDK version and rebuld project.

Another problem can be if you use 64bit OS. You ned to install 32 bit lib, because aapt can`t work with 64bit libs.

<ul><li>in Ubuntu install ia32-libs</li> <li><a href="https://ask.fedoraproject.org/question/9556/how-do-i-install-32bit-libraries-on-a-64-bit-fedora/" rel="nofollow">Fedora described here</a></li> <li>in other distr find libs in google (just google:ia32-libs for "mydistr"), i cant describe all of them ;)</li> </ul>

P.S. sorry for my English ;)

Answer4:

Delete all class file in your_package_name(androisTest) folder only keep ApplicationTest file.

Answer5:

If you are using some library from which you are getting this error, just delete the build folder under your app source and rebuild the project. I was using OpenCV library for which I w as getting this error and that got resolved by deleting the build folder and rebuilding the project.

Answer6:

<ol><li>Delete all generated code inside "gen" folder.</li> <li>Uncheck Project->Build Automatically in eclipse.</li> <li>compile android source once again.</li> <li>Error will be removed, worked for me.</li> </ol>

Answer7:

This still happens sometimes in Android Studio, but a simple Build / Clean Project followed by a Build / Rebuild Project takes care of it.

Recommend

  • Event added through Calendar Provider is not showing up on the Android Calendar App
  • Can't access Buildroot staging libraries via ./configure when cross compiling. It fails on test
  • Why JQuery doesn't trigger added eventlistener?
  • Running jar built using Maven causes “java.lang.NoClassDefFoundError: org/rosuda/JRI/Rengine” error
  • How to get the file count for each revision of mercurial using python script
  • maven - Failed to execute cargo:start
  • Tips on using Git with svn in presence of a large number of svn externals
  • Maven: How do I deploy my WAR file to a remote Tomcat server?
  • This gradle plugin requires Android Studio 3.0 minimum
  • jaxb episodic compilation with maven
  • Upgraded to Android Studio 3.0: Unable to resolve dependency for ':app@debug/compileClasspath&#
  • Gradle compile dependencies not included in Jar
  • Import Protobuf generated classes using Gradle in IntelliJ
  • curl - How to escape < in parameter value
  • Android gradle : Error No resource found that matches the given name: attr when refer to an android
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • CSS: How to fix overlapping divs
  • How to force Composer to download a local package?
  • Accessing local repository in offline mode
  • Element.tagName for python not working
  • How can I speed up CURL tasks?
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How to get Eclipse Oxygen to run on Java 9
  • Do I need to reset a Perl hash index?
  • Display images in Django
  • How to render a blob on a canvas element?
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Seeking advice on Jetty HttpClient Hang
  • Cannot connect to cassandra from Spark
  • Why is an OPTIONS request sent to the server?
  • Spring security and special characters
  • Cross-Platform Protobuf Serialization
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • XCode can't find symbols for a specific iOS library/framework project
  • Circular dependency while pushing http interceptor
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery