package name clashes with class of same name error in android studio

19,395

Solution 1

This is a late answer, but still, none of the answers provided the true reason for why this error is shown.

You get this error when one of your package names are the same as one of your classes, as shown below:

main 
    java
        com.yourpackage.mycoolapp
             somePackage
                 myclass.java
                 class2.java
             somePackage.java

as you can see above the pack name somePackage and the class name somePackage.java, has the same name.


To fix this you can either change the package name or the name of the class:

Right click on package/class -> Refactor -> Rename

Done, no need to rebuild.

Solution 2

I fixed the same issue by changing applicationId in app\build.gradle and rebuilt the project. My applicationId in build.gradle was the same as package name concatenated with appname from AndroidManifest.xml.

Before the change:

build.gradle

defaultConfig {
    applicationId "com.mypackage.MyAppName"
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackage" >
    <application
        android:name="MyAppName"
    </application>
</manifest>

After the change:

defaultConfig {
    applicationId "com.mypackage.MyApp"
}

I had this error in two places:

  1. BuildConfig.java in MyProjectName/app/build/genereted/source/buildConfig/androidTest/debug/mypackage/MyClass/test
  2. R.java in MyProjectName/app/build/genereted/source/r/androidTest/debug/mypackage/MyClass/test

both contained package com.mypackage.MyClass.test; what was reported had an error.

Solution 3

I try to rebuild the project and solve the problem.

Build->Rebuild Project!

Solution 4

I think you have test.java file in your project files. Rename it to something else.

for android-studio click on test.java and  Go to ReFactor-->Rename

Go-to Build-->Rebuild Project.

Solution 5

After the update, I ran into the same error.

  1. I deleted the Buildconfig.java
  2. Refactor-->Rename, error class.
  3. I ran it again solved.

/AndroidStudioProjects/appName/app/build/generated/source/buildConfig/debug/com/example/gurkan0791/appName/BuildConfig.java
Share:
19,395
Caesar
Author by

Caesar

Updated on July 22, 2022

Comments

  • Caesar
    Caesar almost 2 years

    I was trying out Udacity course on developing android app.enter image description here

    above was the error i ran into when i tried creating an android test case while going through the lesson-4.

    the problem is the test classes under the androidtest folderare packaged under com.sasharma.android.sunshine.testbut whenever i run the app in Androidtest configuration the gradle build the bUildCOnfig File for androidtest with the same package name and gives me the error "Error:(4, 1) error: package com.sasharma.android.sunshine.test clashes with class of same name"

    Also i found that the only difference between the package names used by course instructors and the one used by me is that they are having an extra "app" to there package names like my package name for src\main\java*activity classes is also com.sasharma.android.sunshine but theirs is com.sasharma.android.sunshine .test and same goes for the package name under androidTest directory

    Thanks in Advance!!!