@@ -852,6 +852,144 @@ class UnusedDependenciesTest : BasePluginTest() {
852
852
build(" moduleCheckUnusedDependency" ).shouldSucceed()
853
853
}
854
854
855
+ @Test
856
+ fun `module with an auto-generated manifest and a string resource used in subject module should not be unused` () {
857
+
858
+ val appFile = FileSpec .builder(" com.example.app" , " MyApp" )
859
+ .addType(
860
+ TypeSpec .classBuilder(" MyApp" )
861
+ .addProperty(
862
+ PropertySpec .builder(" appNameRes" , Int ::class .asTypeName())
863
+ .getter(
864
+ FunSpec .getterBuilder()
865
+ .addCode(
866
+ """ return %T.string.app_name""" ,
867
+ ClassName .bestGuess(" com.example.app.R" )
868
+ )
869
+ .build()
870
+ )
871
+ .build()
872
+ )
873
+ .build()
874
+ )
875
+ .build()
876
+
877
+ val appProject = ProjectSpec (" app" ) {
878
+ addBuildSpec(
879
+ ProjectBuildSpec {
880
+ addPlugin(""" id("com.android.library")""" )
881
+ addPlugin(" kotlin(\" android\" )" )
882
+ android = true
883
+ addProjectDependency(" api" , jvmSub1)
884
+ }
885
+ )
886
+ addSrcSpec(
887
+ ProjectSrcSpec (Path .of(" src/main/java" )) {
888
+ addFileSpec(appFile)
889
+ }
890
+ )
891
+ addSrcSpec(
892
+ ProjectSrcSpec (Path .of(" src/main" )) {
893
+ addRawFile(
894
+ RawFile (
895
+ " AndroidManifest.xml" ,
896
+ """ <manifest package="com.example.app" />
897
+ """ .trimMargin()
898
+ )
899
+ )
900
+ }
901
+ )
902
+ }
903
+
904
+ val androidSub1 = ProjectSpec (" lib-1" ) {
905
+
906
+ // without this, the standard manifest will be generated and this test won't be testing anything
907
+ disableAutoManifest = true
908
+
909
+ addSrcSpec(
910
+ ProjectSrcSpec (Path .of(" src/main/res/values" )) {
911
+ addRawFile(
912
+ RawFile (
913
+ " strings.xml" ,
914
+ """ <resources>
915
+ | <string name="app_name" translatable="false">MyApp</string>
916
+ |</resources>
917
+ """ .trimMargin()
918
+ )
919
+ )
920
+ }
921
+ )
922
+ addBuildSpec(
923
+ ProjectBuildSpec {
924
+ addPlugin(""" id("com.android.library")""" )
925
+ addPlugin(" kotlin(\" android\" )" )
926
+ android = true
927
+ // This reproduces the behavior of Auto-Manifest:
928
+ // https://github.com/GradleUp/auto-manifest
929
+ // For some reason, that plugin doesn't work with Gradle TestKit. Its task is never
930
+ // registered, and the manifest location is never changed from the default. When I open
931
+ // the generated project dir and execute the task from terminal, it works fine...
932
+ // This does the same thing, but uses a different default directory.
933
+ addBlock(
934
+ """
935
+ |val manifestFile = file("${' $' } buildDir/generated/my-custom-manifest-location/AndroidManifest.xml")
936
+ |
937
+ |android {
938
+ | sourceSets {
939
+ | findByName("main")?.manifest {
940
+ | srcFile(manifestFile.path)
941
+ | }
942
+ | }
943
+ |}
944
+ |
945
+ |val makeFile by tasks.registering {
946
+ |
947
+ | doFirst {
948
+ |
949
+ | manifestFile.parentFile.mkdirs()
950
+ | manifestFile.writeText(
951
+ | ""${' "' } <manifest package="com.example.lib1" /> ""${' "' } .trimMargin()
952
+ | )
953
+ | }
954
+ |}
955
+ |
956
+ |afterEvaluate {
957
+ |
958
+ | tasks.withType(com.android.build.gradle.tasks.GenerateBuildConfig::class.java)
959
+ | .configureEach { dependsOn(makeFile) }
960
+ | tasks.withType(com.android.build.gradle.tasks.MergeResources::class.java)
961
+ | .configureEach { dependsOn(makeFile) }
962
+ | tasks.withType(com.android.build.gradle.tasks.ManifestProcessorTask::class.java)
963
+ | .configureEach { dependsOn(makeFile)}
964
+ |
965
+ |}
966
+ """ .trimMargin()
967
+ )
968
+ }
969
+ )
970
+ }
971
+
972
+ ProjectSpec (" project" ) {
973
+ addSubproject(appProject)
974
+ addSubproject(androidSub1)
975
+ addSettingsSpec(projectSettings.build())
976
+ addBuildSpec(
977
+ projectBuild
978
+ .addBlock(
979
+ """ moduleCheck {
980
+ | autoCorrect = false
981
+ |}
982
+ """ .trimMargin()
983
+ ).build()
984
+ )
985
+ }
986
+ .writeIn(testProjectDir.toPath())
987
+
988
+ build(" moduleCheckUnusedDependency" ).shouldSucceed()
989
+ // one last check to make sure the manifest wasn't generated, since that would invalidate the test
990
+ File (testProjectDir, " /lib1/src/main/AndroidManifest.xml" ).exists() shouldBe false
991
+ }
992
+
855
993
@Test
856
994
fun `module with a declaration used via a class reference with wildcard import should not be unused` () {
857
995
val appProject = ProjectSpec (" app" ) {
0 commit comments