Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin file changes result in Redeclaration error

Tags:

android

kotlin

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.

like image 261
ganlaw Avatar asked Jun 27 '19 16:06

ganlaw


2 Answers

just clean project and rebuild your project again

like image 72
doema Avatar answered Sep 28 '22 11:09

doema


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.

like image 42
Sierisimo Avatar answered Sep 28 '22 10:09

Sierisimo