2

I Clone a project using Android studio 2.2.2 on windows, when I run the project there is an error

illegal escape character on the BuildConfing.java file

The original development might be used IntelliJ IDEA on linux ubutu. I did clean the project and rebuild it, no changes.

The error is on the following line

public static final String[] TRANSLATION_ARRAY = new String[]  {"G:\Developer\AndroidStudioProjects\gpslogger\gpslogger\src\main\res\values","G:\Developer.......,....,....};

BuildConfig.java is an Auto generated file and should not be edited.

Why is this happen? how BuildConfig.java is generated ?

UPDATE

@Vijai, Thanks a lot. the problem is on the build.gradle file. following task returns a string with illegal escape character.

task buildTranslationArray << {
def foundLocales = new StringBuilder()
foundLocales.append("new String[]{")

fileTree("src/main/res").visit { FileVisitDetails details ->
    if(details.file.path.endsWith("strings.xml")){
        def languageCode = details.file.parent.tokenize('/').last().replaceAll('values-','').replaceAll('-r','-')
        languageCode = (languageCode == "values") ? "en" : languageCode;
        foundLocales.append("\"").append(languageCode).append("\"").append(",")
    }
}
foundLocales.append("}")
//Don't forget to remove the trailing comma
def foundLocalesString = foundLocales.toString().replaceAll(',}','}')
android.defaultConfig.buildConfigField "String[]", "TRANSLATION_ARRAY", foundLocalesString
}
preBuild.dependsOn buildTranslationArray

How can I fix this issue?

Silhan52
  • 41
  • 3

1 Answers1

0

Update

Sorry I did not read that the issue is with BuildConfig. While it is true that its not adviced to edit BuildConfig.java, its contents do come somewhere from build.gradle and you have to escape character there


The charcter \ is a special character and needs to be escaped. replace all instance of \ by \\ like

public static final String[] TRANSLATION_ARRAY = new String[]  {"G:\\Developer\\AndroidStudioProjects\\gpslogger\\gpslogger\\src\\main\\res\\values","G:\\Developer.......,....,....};
Vijai
  • 1,067
  • 2
  • 11
  • 25
  • Yes I know, I have to replace all instance of ' \ ' by ' \\ '. But the problem is BuildConfig.java file is auto **generated**, We should **not edit** the file, and cannot edit it. – Silhan52 Nov 13 '16 at 06:19
  • While it is true you are **not supposed to edit** BuildConfig.java, the `TRANSLATION_ARRAY` comes somewhere from build.gradle. So that would be the location where you have to escape the character. Updated the answer – Vijai Nov 13 '16 at 06:28