简体   繁体   中英

Android Unit Test Succeeds in Android Studio but not in Gradle due to Resource Files

I have some text files with data which I am using in some of my tests. The files live in the 'res' sub-folder under the test folder as follows:

+app
 + src
  + main
  + test
   + res
    - File.txt

I have added the following in my gradle file which allows the me to access File.txt via classloader in Android Studio.

//Copy resource files into build dir
task copyResDirectoryToClasses(type: Copy){
    from "${projectDir}/src/test/res"
    into "${buildDir}/intermediates/classes/test/debug/res"
}
assembleDebug.dependsOn(copyResDirectoryToClasses)

However when I do a gradle build (for example using a cloud CI service) then the file can not be found. I am assuming this is because the final apk does not have the File.txt packaged into it.

Any advice on how to get this into the debug apk would be greatly appreciated.

Kind Regards,

Found the solution. Actually the issue here was that the above copyResDirectoryToClasses closure only copies to the debug target which does get built into the apk. However this does not get copied to other build variants such as Release. The solution was to tweak the above to also copy to Release.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM