Skip to content

Commit 7307c02

Browse files
committed
Use legacy instrumentation runner without androidX dependencies
1 parent d3283ce commit 7307c02

File tree

1 file changed

+7
-47
lines changed

1 file changed

+7
-47
lines changed

scripts/build-android-app.sh

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ path = pathlib.Path(sys.argv[1])
277277
text = path.read_text()
278278
modified = False
279279
280-
if "androidx.test.runner.AndroidJUnitRunner" not in text:
280+
if "android.test.InstrumentationTestRunner" not in text:
281281
def add_runner(match):
282282
prefix = match.group(0)
283-
return prefix + "\n testInstrumentationRunner \"androidx.test.runner.AndroidJUnitRunner\""
283+
return prefix + "\n testInstrumentationRunner \"android.test.InstrumentationTestRunner\""
284284
285285
new_text, count = re.subn(r"(defaultConfig\s*\{)", add_runner, text, count=1, flags=re.MULTILINE)
286286
if count:
@@ -289,43 +289,12 @@ if "androidx.test.runner.AndroidJUnitRunner" not in text:
289289
else:
290290
raise SystemExit("defaultConfig block not found while adding instrumentation runner")
291291
292-
config_block = "\nconfigurations {\n maybeCreate(\"androidTestImplementation\")\n}\n"
293-
294-
if 'maybeCreate("androidTestImplementation")' not in text:
295-
new_text, count = re.subn(r"(android\s*\{)", config_block + r"\1", text, count=1, flags=re.MULTILINE)
296-
if count:
297-
text = new_text
298-
modified = True
299-
else:
300-
text = text.rstrip() + config_block
301-
modified = True
302-
303-
dependency_lines = []
304-
if "androidx.test.ext:junit" not in text:
305-
dependency_lines.append(' androidTestImplementation "androidx.test.ext:junit:1.1.5"')
306-
if "androidx.test.espresso:espresso-core" not in text:
307-
dependency_lines.append(' androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"')
308-
309-
if dependency_lines:
310-
dependencies_to_add = "\n" + "\n".join(dependency_lines)
311-
312-
def add_dependencies(match):
313-
prefix = match.group(0)
314-
return prefix + dependencies_to_add
315-
316-
new_text, count = re.subn(r"(dependencies\s*\{)", add_dependencies, text, count=1, flags=re.MULTILINE)
317-
if count:
318-
text = new_text
319-
modified = True
320-
else:
321-
raise SystemExit("dependencies block not found while adding androidTest dependencies")
322-
323292
if modified:
324293
if not text.endswith("\n"):
325294
text += "\n"
326295
path.write_text(text)
327296
PYTHON
328-
ba_log "Ensured instrumentation runner and dependencies are declared"
297+
ba_log "Ensured instrumentation runner is declared"
329298
else
330299
ba_log "Warning: Gradle build file not found at $APP_BUILD_GRADLE; skipping instrumentation dependency configuration" >&2
331300
fi
@@ -337,21 +306,12 @@ cat >"$TEST_CLASS" <<EOF
337306
package $PACKAGE_NAME;
338307
339308
import android.content.Context;
309+
import android.test.InstrumentationTestCase;
340310
341-
import androidx.test.ext.junit.runners.AndroidJUnit4;
342-
import androidx.test.platform.app.InstrumentationRegistry;
343-
344-
import org.junit.Test;
345-
import org.junit.runner.RunWith;
346-
347-
import static org.junit.Assert.assertEquals;
348-
349-
@RunWith(AndroidJUnit4.class)
350-
public class HelloCodenameOneInstrumentedTest {
311+
public class HelloCodenameOneInstrumentedTest extends InstrumentationTestCase {
351312
352-
@Test
353-
public void useAppContext() {
354-
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
313+
public void testUseAppContext() {
314+
Context appContext = getInstrumentation().getTargetContext();
355315
assertEquals("$PACKAGE_NAME", appContext.getPackageName());
356316
}
357317
}

0 commit comments

Comments
 (0)