Solve Problem: Duplicate attribute ‘{http://schemas.android.com/apk/res/android}textColor'.

I encounter this problem yesterday, but it was caused after I upgrade the Android Studio, so I think it’s the problem os the Android…

I encounter this problem yesterday, but it was caused after I upgrade the Android Studio, so I think it’s the problem os the Android Studio, but actually, it’s the problem on the code.

Solve the problem

Today, I spend sometime to solve this problem, first I downgrade the Android Studio, and find it also has the same problem, so I change code to old commit, and find that the code can compile successfully.

It’s hard to find the problem, because the error message is like below:Caused by: com.ctc.wstx.exc.WstxParsingException: Duplicate attribute '{http://schemas.android.com/apk/res/android}textColor'.
at [row,col {unknown-source}]: [36,53]

So I find all the xml with textColor in line 36, but no problem found, because we have so many xml files in the project.

At last I locate the commit that cause the problem, and this commit has changed a lot of xml files, I try to undo changes for some of the files to find the issue, but Android has another problem, I need to clean the project and build again, the problem will show, If I just build the project after change the source file, it can build and don’t report error. Also build after clean is very slow in my machine.

At last, I find the problem, there are two textColor on the xml file like below:android:textColor="?theme_content_text_color_gray"
           android:textColor="?theme_content_text_color_dark"

Another reason it’s hard to find the problem the error message report the error is on line 36, but the textColor was on 34 and 35 lines.

How to avoid the problem next time?

After I find the problem, I am thinking how to avoid the problem again, or how to solve the problem quick next time?

The code quality

When develop code, the developer should pay attention on the errors on the Android Studio, because this file will report error on the editor.

Run the project before push codes

I think if the developer can run the code, he will find the problem before commit the code to codebase.

The problem panel

On the project errors tab, we can find the problems like below:

So if I check the problem panel earlier, I will find the problem earlier.