简体   繁体   English

由于资源文件,Android Studio中的Android单元测试成功,但在Gradle中未成功

[英]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: 这些文件位于测试文件夹下的“ res”子文件夹中,如下所示:

+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. 我在gradle文件中添加了以下内容,该文件允许我通过Android Studio中的类加载器访问File.txt。

//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. 但是,当我执行gradle构建(例如,使用cloud CI服务)时,找不到文件。 I am assuming this is because the final apk does not have the File.txt packaged into it. 我认为这是因为最终apk没有将File.txt打包到其中。

Any advice on how to get this into the debug apk would be greatly appreciated. 任何有关如何将其放入调试apk的建议将不胜感激。

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. 实际上,这里的问题是上述copyResDirectoryToClasses闭包仅复制到确实内置在apk中的调试目标。 However this does not get copied to other build variants such as Release. 但是,这不会复制到其他版本的版本中,例如Release。 The solution was to tweak the above to also copy to Release. 解决方案是对上述内容进行调整以将其复制到Release。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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