/* Ċ Ġ* ĠCopyright Ġ( C ) Ġ 2 0 1 8 ĠThe ĠAndroid ĠOpen ĠSource ĠProject Ċ Ġ* Ċ Ġ* ĠLicensed Ġunder Ġthe ĠApache ĠLicense , ĠVersion Ġ 2 . 0 Ġ( the Ġ" License "); Ċ Ġ* Ġyou Ġmay Ġnot Ġuse Ġthis Ġfile Ġexcept Ġin Ġcompliance Ġwith Ġthe ĠLicense . Ċ Ġ* ĠYou Ġmay Ġobtain Ġa Ġcopy Ġof Ġthe ĠLicense Ġat Ċ Ġ* Ċ Ġ* ĠĠĠĠĠ Ġhttp :// www . apache . org / licenses / LICENSE - 2 . 0 Ċ Ġ* Ċ Ġ* ĠUnless Ġrequired Ġby Ġapplicable Ġlaw Ġor Ġagreed Ġto Ġin Ġwriting , Ġsoftware Ċ Ġ* Ġdistributed Ġunder Ġthe ĠLicense Ġis Ġdistributed Ġon Ġan Ġ" AS ĠIS " ĠBASIS , Ċ Ġ* ĠWITHOUT ĠWARRANTIES ĠOR ĠCONDITIONS ĠOF ĠANY ĠKIND , Ġeither Ġexpress Ġor Ġimplied . Ċ Ġ* ĠSee Ġthe ĠLicense Ġfor Ġthe Ġspecific Ġlanguage Ġgoverning Ġpermissions Ġand Ċ Ġ* Ġlimitations Ġunder Ġthe ĠLicense . Ċ Ġ*/ Ċ Ċ package Ġcom . android . build . gradle . integration . resources Ċ Ċ import Ġcom . android . build . gradle . integration . common . fixture . Gradle Test Project Ċ import Ġcom . android . build . gradle . integration . common . fixture . app . HelloWorld App Ċ import Ġcom . android . build . gradle . options . Boolean Option Ċ import Ġcom . android . test utils . truth . File Subject . assertThat Ċ import Ġcom . android . utils . FileUtils Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġjava . io . File Ċ Ċ class ĠConditional Keep Rules Test Ġ{ ĊĠĠĠ Ġ@ Jvm Field ĊĠĠĠ Ġ@ Rule ĊĠĠĠ Ġvar Ġproject Ġ= ĊĠĠĠĠĠĠĠ ĠGradle Test Project . builder () ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. from Test App ( HelloWorld App . for Plugin (" com . android . application ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. create () ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcheck Conditional Rules Gener ations () Ġ{ ĊĠĠĠĠĠĠĠ Ġproject . build File . append Text (""" Ċ android Ġ{ ĊĠ Ġbuild Types Ġ{ ĊĠĠĠ Ġdebug Ġ{ ĊĠĠĠĠĠ Ġmin ify Enabled Ġtrue ĊĠĠĠĠĠ Ġpro guard Files ĠgetDefault Pro guard File (' pro guard - android . txt '),' pro guard - rules . pro ' ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } """. trim Margin ()) ĊĊĠĠĠĠĠĠĠ Ġproject . test Dir . resolve (" pro guard - rules . pro "). print Writer (). use Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġit . write ("- keep Ġclass Ġ*** ") ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġlayouts Ġ= ĠFileUtils . join ( project . main Src Dir . parent File , Ġ" res ", Ġ" layout ") ĊĠĠĠĠĠĠĠ ĠFileUtils . write ToFile ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFile ( layouts , Ġ" layout one . xml "), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ""" <? xml Ġversion =" 1 . 0 " Ġencoding =" utf - 8 "?> Ċ < com . custom . My View 1 ĊĠĠĠ Ġxmlns : android =" http :// schemas . android . com / apk / res / android " Ġandroid : layout _ width =" match _ parent " ĊĠĠĠ Ġandroid : layout _ height =" match _ parent "> Ċ Ċ </ com . custom . My View 1 > """) ĊĠĠĠĠĠĠĠ ĠFileUtils . write ToFile ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠFile ( layouts , Ġ" layout two . xml "), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ""" <? xml Ġversion =" 1 . 0 " Ġencoding =" utf - 8 "?> Ċ < com . custom . My View 2 ĊĠĠĠ Ġxmlns : android =" http :// schemas . android . com / apk / res / android " Ġandroid : layout _ width =" match _ parent " ĊĠĠĠ Ġandroid : layout _ height =" match _ parent "> ĊĊĠĠĠ Ġ< include Ġlayout ="@ layout / layout one "/> Ċ Ċ </ com . custom . My View 2 > """ ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġ// ĠFirst Ġmake Ġsure Ġthe Ġconditional Ġrules Ġwere Ġgenerated Ġif Ġthe Ġflag Ġis Ġused . ĊĠĠĠĠĠĠĠ Ġproject . executor (). with ( Boolean Option . CONDITION AL _ KEEP _ R ULES , Ġtrue ). run (" assemble Debug ") ĊĠĠĠĠĠĠĠ Ġval Ġrules Ġ= Ġproject . get Intermediate File (" a apt _ pro guard _ file ", Ġ" debug ", Ġ" a apt _ rules . txt ") ĊĠĠĠĠĠĠĠ ĠassertThat ( rules ). exists () ĊĠĠĠĠĠĠĠ ĠassertThat ( rules ). contains ("- if Ġclass Ġ** . R \$ layout Ġ{ Ġint Ġlayout two ; Ġ} ") ĊĠĠĠĠĠĠĠ ĠassertThat ( rules ). contains ("- keep Ġclass Ġcom . custom . My View 2 ") ĊĊĠĠĠĠĠĠĠ Ġ// ĠAnd Ġalso Ġcheck Ġthat Ġconditional Ġrules Ġare Ġnot Ġgenerated Ġwhen Ġthe Ġflag Ġis Ġturned Ġoff . ĊĠĠĠĠĠĠĠ Ġproject . executor (). with ( Boolean Option . CONDITION AL _ KEEP _ R ULES , Ġfalse ). run (" clean ", Ġ" assemble Debug ") ĊĠĠĠĠĠĠĠ ĠassertThat ( rules ). exists () ĊĠĠĠĠĠĠĠ ĠassertThat ( rules ). does Not Contain ("- if Ġclass Ġ** . R \$ layout Ġ{ Ġint Ġlayout two ; Ġ} ") ĊĠĠĠ Ġ} Ċ }