Kotlin file changes result in Redeclaration error

Viewed 20100

Every change to a kotlin file results in a Redeclaration error when building. The only way around it is by cleaning the project and then rebuilding. There are no other files in the project with the same name as the files I am editing.

I have tried:

  • Renaming the class I have been working on
  • Upgrading and downgrading the version of kotlin, gradle and kotlin plugin in Android Studio
  • Using beta Android Studio
  • Reordering the plugins in my gradle file

The current build is using:

org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21
com.android.tools.build:gradle:3.2.0

Example code that will cause the error. If I build without the addedThisLine it works as its the first build. Then when I edit the file in anyway it will cause the error below.

package beagle.com

class ErrorFragment {

    val hello = "Hello"

    init {
        var goodbye = "goodbye"
        var addedThisLine = "When this line is added I get error"
    }
}

Error I get

Redeclaration Error

This happens in every kotlin file in my project, the code I posted above is the most basic file. As you can see it points to the name of the class for the error.

10 Answers

just clean project and rebuild your project again

TL;DR: Check your directories have the exact same name as they appear in settings.gradle.

Worth to mention: MacOS is not always case sensitive and that can lead to weird problems.


I think I find a solution: Case sensitive stuff.

In my case I'm using modules, let's just call the modules: moduleA, moduleB

In settings.gradle I have: include ':ModuleA', 'moduleB' but the directories at file system level are: moduleA/ and moduleB/

This seems weird and should not work but the first run always works, then the next ones will fail until project is cleaned.

I decided to keep things consistent and rename the directories as the settings.gradle lists them, even when in AndroidStudio they appear with the name listed in settings.gradle. This also helped to cleanup things because git has a configuration under .git/config which states to ignore the case sensitive stuff.

So, check the case of your directories vs. how they are listed in settings.gradle.

After renaming stuff and cleaning with gradle everything worked as before.

Upgrade kotlin and gradle version then

Try to Run this may this will help

./gradlew clean
rm -rf ~/.gradle/caches/*

or

Try to add the following to your gradle.build file if not added

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

This worked for me

  1. Build > Clean Project
  2. Build > Rebuild Project
  3. Run

1.clean and rebuild

if this don't work its for conflict of classes names !

Just Rename or change path of class , because you have same name of class in one package !

if Clean Project And Rebuild Not working . Try sync project. That works for me.

I had the same issue,

./gradlew clean

was the solution for me

As of jul 2020 you will get this Redeclaration error when there is an already existing class with the same name, so either you have to rename or move to a different package.

Kotlin compiler can fail in MacOS and Linux environments, when there is another Java file with the same name in the path. Renaming file can fix the issue as @sana says

Similar issue on JetBrains issue-tracker

in my case I have created same data class twice in same package was causing the error Redeclaration: Results . try to not create Kotlin class with same name in one package

Related