Android:More than one file was found with OS independent path ‘res/values/values.xml

More than one file was found with OS independent path ‘res/values/values.xml
More than one file was found with OS independent path ‘res/values/values.xml

analysis question:
1. First, according to the error report diary: this is due to the same name of the resource file is repeated, so the system compilation time can not identify which file to load; 2. Then, follow the train of thought, go to the global search to see if your own resource file or the jar, aar or resource file used for guidance has more than one resource file with diary error; 3. After searching, it was found that multiple files with the same name did exist in the imported aar package:

4. So now that the cause of the problem has been identified, the problem is how to solve it.

According to the advice of most people on the Internet, there are mainly the following kinds:
1. Find the corresponding resource file and change the name (this method depends on the size of the project, the number of conflicting files, or only one or two resource files, find out and change the following, o; However, if there are too many conflicting files, this method is not recommended); 2. Exclude files or directories from APK, act on APK, and do not filter aar and jar contents:

...
android{
    packagingOptions {
        exclude 'res/values/values.xml'
        exclude 'libs/loginsdk1.0.4-release.aar/res/values/values.xml'
    }
}
...

exclude is the path of the file to be filtered. This method is straightforward, simple, and the method most people use.
3. Similar to method 2: packagingOptions USES pickFirst, matches to multiple identical files, and extracts only the first. APK only, files in AAR and JAR cannot be filtered.
for example:

...
android{
    packagingOptions {
//        pickFirst 'res/values/values.xml'
//        pickFirst 'AndroidManifest.xml'
    }
    }
    ...

4. However, when we are referenced by the three parties jar packages or aar resource file, can’t modify repeatedly named resource file, at the same time, although the same name, but the file content is different, several methods obviously cannot solve the problem, therefore, at this time, we can bold guess: since packagingOptions configuration items can filter files, extract, etc., will it be all right if we can be repeated by identifying the complete name of a resource file, and then let the system to extract, or, we can also make duplicate naming files to merge?And, along the way of thinking, I baidu about gradle configuration items packagingOptions instructions
gradle configuration items packagingOptions instructions
from this post, so he found a solution:
the merge, will match the files are added to the APK, contrary to some pickFirst, will merge all files
so, combine the duplicate files:

android{
    packagingOptions {
        merge 'res/values/values.xml'
        merge 'AndroidManifest.xml'
        merge 'res/drawable/login_bg.xml'
        merge 'R.txt'
        merge 'classes.jar'
    }
    }

finally, compile, pass.

:
sometimes encounter problems, according to the online most people encounter similar problems and offer you the train of thought, if not solve, can follow the same train of thought to further try, maybe problem solved, such as: from gradle configuration items packagingOptions use of filtering, extracting, if still can’t solve problems, you can follow the train of thought, guess packagingOptions whether there will be other use method, to try, maybe can solve problem. Of course, I don’t know if using this Merg will cause any other problems. I also welcome your advice. thank you

Read More: