Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

package name clashes with class of same name error in android studio [closed]

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!!!

like image 512
Caesar Avatar asked Aug 23 '15 03:08

Caesar


People also ask

Can we have two classes with same name under the same package?

Yes there is. You would need to implement your own Classloader and play some games to be able to access both during runtime.

Can package name be same as class name?

Yes, it will work.

What is name clashing in Java?

This answer is not useful. Show activity on this post. At runtime, the parameter types are replaced by Object . So saveAll(Collection<?>) and saveAll(Collection<MyClass>) are transformed to saveAll(Collection) . This is a name clash.


4 Answers

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.

like image 160
ClassA Avatar answered Oct 17 '22 14:10

ClassA


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.

like image 35
Arjun Nayak Avatar answered Sep 24 '22 23:09

Arjun Nayak


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.

like image 4
Grzegorz Bielański Avatar answered Oct 17 '22 13:10

Grzegorz Bielański


I try to rebuild the project and solve the problem.

Build->Rebuild Project!

like image 3
Lincoln Avatar answered Oct 17 '22 14:10

Lincoln