java 包名称与android studio中的同名类错误冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32162986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:45:57  来源:igfitidea点击:

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

javaandroidandroid-studiopackage

提问by Caesar

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

我正在尝试有关开发 android 应用程序的 Udacity 课程。在此处输入图片说明

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

以上是我在学习第 4 课时尝试创建 android 测试用例时遇到的错误。

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"

问题是 androidtest 文件夹下的测试类打包在 com.saharma.android.sunshine.test 下,但是每当我在 Androidtest 配置中运行应用程序时,gradle 都会使用相同的包名为 androidtest 构建 bUildCOnfig 文件并给我错误“错误: (4, 1) 错误:包 com.sasharma.android.sunshine.test 与同名类冲突”

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

我还发现,课程讲师使用的包名称与我使用的包名称之间的唯一区别是,他们在那里有一个额外的“应用程序”包名称,例如我的 src\main\java*activity 类的包名称也是com.sasharma.android.sunshine 但他们是 com.sasharma.android.sunshine .test 和 androidTest 目录下的包名称相同

Thanks in Advance!!!

提前致谢!!!

回答by ClassA

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:

当您的 package?names 之一与您的类之一相同时,您会收到此错误,如下所示:

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

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

如您所见,包名称somePackage和类名称somePackage.java具有相同的名称。



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.

完成,无需重建。

回答by Grzegorz Bielański

I fixed the same issue by changing applicationIdin app\build.gradleand rebuilt the project. My applicationIdin build.gradlewas the same as packagename concatenated with appnamefrom AndroidManifest.xml.

我通过改变固定同一问题的applicationIDapp\build.gradle和重建项目。我的applicationIdin与与from连接的名称build.gradle相同。packageappnameAndroidManifest.xml

Before the change:

改动前:

build.gradle

构建.gradle

defaultConfig {
    applicationId "com.mypackage.MyAppName"
}

AndroidManifest.xml

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
  1. BuildConfig.java 中 MyProjectName/app/build/genereted/source/buildConfig/androidTest/debug/mypackage/MyClass/test
  2. R.java 中 MyProjectName/app/build/genereted/source/r/androidTest/debug/mypackage/MyClass/test

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

两者都包含package com.mypackage.MyClass.test;报告有错误的内容。

回答by Arjun Nayak

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

我认为您的项目文件中有 test.java 文件。将其重命名为其他名称。

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

Go-to Build-->Rebuild Project.

转到构建--> 重建项目。

回答by Lincoln

I try to rebuild the project and solve the problem.

我尝试重建项目并解决问题。

Build->Rebuild Project!

构建->重建项目!

回答by The Fluffy T Rex

If the Glide library is causing the issue, make sure that the "MyAppGlideModule" class is not an inner class.

如果 Glide 库导致了问题,请确保“MyAppGlideModule”类不是内部类。

回答by Gurkan Yesilyurt

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.
  1. 我删除了 Buildconfig.java
  2. 重构-->重命名,错误类。
  3. 我再次运行它解决了。


/AndroidStudioProjects/appName/app/build/generated/source/buildConfig/debug/com/example/gurkan0791/appName/BuildConfig.java

回答by hrskrs

You can expect this error also if you define testApplicationIdsame with default one as yourApplicationId.test.

如果您将testApplicationId默认值定义为相同,您也可以预期此错误yourApplicationId.test

As doc says here:

正如医生在这里所说

By default, the build tools apply an application ID to your instrumentation test APK using the application ID for the given build variant, appended with .test. For example, a test APK for the com.example.myapp.free build variant has the application ID com.example.myapp.free.test.

默认情况下,构建工具使用给定构建变体的应用程序 ID 将应用程序 ID 应用到您的检测测试 APK,并附加 .test。例如,com.example.myapp.free 构建变体的测试 APK 具有应用程序 ID com.example.myapp.free.test。

Hence you should not use same testApplicationIdas default one by only appending .testto your applicationId, but you can change it to anything else and it will work fine.

因此,您不应testApplicationId仅通过附加.test到您的applicationId,而使用与默认相同的,但您可以将其更改为其他任何内容,它会正常工作。

回答by Mahmoud Elgamal

If you have class of its name is "test" remove it. i had this problem and reslolve it

如果你有类它的名字是“测试”删除它。我遇到了这个问题并解决了它

回答by Radesh

I face this problem when use EventBus.

我在使用EventBus时遇到了这个问题。

Actually i wrote wrong eventBusIndex and it create file with same package name of my application id and it make error

实际上我写了错误的 eventBusIndex 并且它创建了与我的应用程序 ID 相同的包名的文件并且它出错了

Wrong code

错误的代码

kapt {
    arguments {
        arg('eventBusIndex', 'MY_APPLICATION_ID')//E.g. "com.me.myappname"
    }
}

Fixed code

固定码

kapt {
    arguments {
        arg('eventBusIndex', 'MY_APPLICATION_ID.MyEventBus')//E.g. "com.me.myappname.MyEventBus"
    }
}