3939 "androidx.test.uiautomator:uiautomator:2.2.0" ,
4040)
4141
42+ HELPER_SNIPPET = (
43+ " if (!project.ext.has('cn1AddAndroidTestDependency')) {\n "
44+ " project.ext.cn1AddAndroidTestDependency = { String notation ->\n "
45+ " def parts = notation.split(':')\n "
46+ " def coordinate = parts.size() >= 2 ? parts[0] + ':' + parts[1] : notation\n "
47+ " def targetNames = ['androidTestImplementation', 'androidTestCompile'].findAll { cfgName ->\n "
48+ " configurations.findByName(cfgName) != null\n "
49+ " }\n "
50+ " if (targetNames.isEmpty()) {\n "
51+ " targetNames = ['androidTestImplementation']\n "
52+ " }\n "
53+ " targetNames.each { cfgName ->\n "
54+ " def cfg = configurations.maybeCreate(cfgName)\n "
55+ " def exists = cfg.dependencies.any { dep ->\n "
56+ " dep.group != null && dep.name != null && (dep.group + ':' + dep.name) == coordinate\n "
57+ " }\n "
58+ " if (!exists) {\n "
59+ " project.dependencies.add(cfgName, notation)\n "
60+ " }\n "
61+ " }\n "
62+ " }\n "
63+ " }\n "
64+ )
65+
4266TEST_DEPENDENCIES_TO_REMOVE = [
4367 re .compile (r"\s+testImplementation 'org\.robolectric:robolectric:[^']*'\n" ),
4468 re .compile (r"\s+testImplementation 'androidx\.test:core:[^']*'\n" ),
@@ -117,22 +141,6 @@ def ensure_instrumentation_runner(self) -> None:
117141 + self .content [insert_position :]
118142 )
119143
120- def _determine_android_test_configuration (self , block_body : str ) -> str :
121- if (
122- "androidTestImplementation" in block_body
123- or " implementation " in block_body
124- or "implementation(" in block_body
125- ):
126- return "androidTestImplementation"
127- if (
128- "androidTestCompile" in block_body
129- or " compile " in block_body
130- or "compile(" in block_body
131- ):
132- return "androidTestCompile"
133- # Default to new-style configuration when undetermined.
134- return "androidTestImplementation"
135-
136144 def ensure_dependencies (self ) -> None :
137145 for pattern in TEST_DEPENDENCIES_TO_REMOVE :
138146 self .content = pattern .sub ('\n ' , self .content )
@@ -141,10 +149,23 @@ def ensure_dependencies(self) -> None:
141149 raise SystemExit ("Unable to locate dependencies block in Gradle file" )
142150 block_body = self .content [block [0 ]:block [1 ]]
143151 insertion_point = block [1 ] - 1
144- configuration = self ._determine_android_test_configuration (block_body )
152+ if "cn1AddAndroidTestDependency" not in block_body :
153+ block_text = self .content [block [0 ]:block [1 ]]
154+ newline_offset = block_text .find ('\n ' )
155+ if newline_offset < 0 :
156+ newline_offset = len (block_text )
157+ helper_insert = block [0 ] + newline_offset + 1
158+ self .content = (
159+ self .content [:helper_insert ]
160+ + HELPER_SNIPPET
161+ + self .content [helper_insert :]
162+ )
163+ block = self .find_block ("dependencies" )
164+ block_body = self .content [block [0 ]:block [1 ]]
165+ insertion_point = block [1 ] - 1
145166 for dependency in ANDROID_TEST_DEPENDENCIES :
146- declaration = f" { configuration } '{ dependency } '\n "
147- if declaration . strip () in block_body :
167+ declaration = f" project.ext.cn1AddAndroidTestDependency( '{ dependency } ') \n "
168+ if dependency in block_body :
148169 continue
149170 self .content = (
150171 self .content [:insertion_point ]
0 commit comments