3

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. http://search.maven.org/#browse%7C1628757896

ObAt
  • 2,337
  • 3
  • 24
  • 44
  • is there an error in one of your xml files preventing .R from being built? – IAmGroot Jun 14 '13 at 08:17
  • I didn't change anything on my xml files, they don't have any error. The project builds and run, the compiler just can't find R – ObAt Jun 14 '13 at 08:18
  • Check if you have one R.java file or not? This happens generally if you have two R.java files – Jainendra Jun 14 '13 at 08:19
  • I just have one R.java file, I tried to delete the file but that doesn't help either – ObAt Jun 14 '13 at 08:20
  • just check your `res` folder and correct errors. – Mihir Jun 14 '13 at 08:29
  • @ObAt Make a search for the R.java manually or using windows search in your project folder you must be having two R.java files. – NetStarter Jun 14 '13 at 11:26
  • @NetStarter, I did but the weird thing there is only one R.java file and one R.class file. I even searched 'C:\Users\{USERNAME}\.AndroidStudioPreview' for R.java (only one file when I build the project) – ObAt Jun 14 '13 at 11:44
  • Did you uncheck the use extern build, (as explained here http://stackoverflow.com/questions/16633956/android-studio-where-is-the-error-output-window/16876993#16876993)? Because unchecking this produces the "duplicate class" errors. – Moberg Jun 14 '13 at 21:46
  • I tried, when I deleted all the libaries it works! But I need those libaries (HTTPComponents from Apache) – ObAt Jun 18 '13 at 07:52

8 Answers8

3

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:

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

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

Let me know.

LuckyMe
  • 3,820
  • 2
  • 27
  • 35
  • 1
    Android Studio does not have a 'gen'-folder, I added the 'src' folder but this doesn't change anything. Android Studio still doesn't generate the R.java file, if I restore it manually I get the duplicate file error – ObAt Jun 14 '13 at 11:23
2

Delete the Build folder generated by Android Studio automatically!

abdelali
  • 29
  • 1
  • Unfortunately this doesn't work, I still have the same issues – ObAt Jun 14 '13 at 11:17
  • This solution works for me. My setup is different from that of @ObAt. It's 3 years later, I'm on Linux and @ObAt appears to be on windows, and my `settings.gradle` is much simpler than the one in question. This solution is much simpler to try than the other one, so hoping the up-vote and this comment help others with the same problem. – Robin like the bird Dec 23 '16 at 22:19
0

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.

  • in Ubuntu install ia32-libs
  • Fedora described here
  • in other distr find libs in google (just google:ia32-libs for "mydistr"), i cant describe all of them ;)

P.S. sorry for my English ;)

Artem Zinoviev
  • 869
  • 12
  • 32
0

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

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
0

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.

Abhinav Tripathi
  • 168
  • 1
  • 2
  • 12
0
  1. Delete all generated code inside "gen" folder.
  2. Uncheck Project->Build Automatically in eclipse.
  3. compile android source once again.
  4. Error will be removed, worked for me.
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25
0

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

Carl
  • 15,445
  • 5
  • 55
  • 53
0

I have not had this problem for a long time until today.

Error:(10, 14) java: duplicate class: com.domain.name.R

After 2 hours of wasting time on the project and getting this error,

Finally I removed the .idea, gen and out folders and also iml file.

Then I closed the project and then re-import it using import project of main menu again.

Its working well right now.

Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36