Skip to content

Commit 49cca52

Browse files
committed
Ensure instrumentation libraries available in generated Android project
1 parent 7307c02 commit 49cca52

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

scripts/build-android-app.sh

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,29 @@ if "android.test.InstrumentationTestRunner" not in text:
289289
else:
290290
raise SystemExit("defaultConfig block not found while adding instrumentation runner")
291291
292+
libraries = [
293+
"useLibrary 'android.test.base'",
294+
"useLibrary 'android.test.mock'",
295+
"useLibrary 'android.test.runner'",
296+
]
297+
298+
missing_libraries = [lib for lib in libraries if lib not in text]
299+
if missing_libraries:
300+
match = re.search(r"^(\s*android\s*\{)", text, flags=re.MULTILINE)
301+
if not match:
302+
raise SystemExit("android block not found while adding instrumentation libraries")
303+
line = match.group(1)
304+
indent = re.match(r"^(\s*)", line).group(1)
305+
insertion = "".join(f"\n{indent} {lib}" for lib in missing_libraries)
306+
text = text[: match.end()] + insertion + text[match.end():]
307+
modified = True
308+
292309
if modified:
293310
if not text.endswith("\n"):
294311
text += "\n"
295312
path.write_text(text)
296313
PYTHON
297-
ba_log "Ensured instrumentation runner is declared"
314+
ba_log "Ensured instrumentation runner and libraries are declared"
298315
else
299316
ba_log "Warning: Gradle build file not found at $APP_BUILD_GRADLE; skipping instrumentation dependency configuration" >&2
300317
fi
@@ -318,6 +335,17 @@ public class HelloCodenameOneInstrumentedTest extends InstrumentationTestCase {
318335
EOF
319336
ba_log "Created instrumentation test at $TEST_CLASS"
320337

338+
DEFAULT_ANDROID_TEST="$GRADLE_PROJECT_DIR/app/src/androidTest/java/com/example/myapplication2/ExampleInstrumentedTest.java"
339+
if [ -f "$DEFAULT_ANDROID_TEST" ]; then
340+
rm -f "$DEFAULT_ANDROID_TEST"
341+
ba_log "Removed default instrumentation stub at $DEFAULT_ANDROID_TEST"
342+
DEFAULT_ANDROID_TEST_DIR="$(dirname "$DEFAULT_ANDROID_TEST")"
343+
DEFAULT_ANDROID_TEST_PARENT="$(dirname "$DEFAULT_ANDROID_TEST_DIR")"
344+
rmdir "$DEFAULT_ANDROID_TEST_DIR" 2>/dev/null || true
345+
rmdir "$DEFAULT_ANDROID_TEST_PARENT" 2>/dev/null || true
346+
rmdir "$(dirname "$DEFAULT_ANDROID_TEST_PARENT")" 2>/dev/null || true
347+
fi
348+
321349
ba_log "Invoking Gradle build in $GRADLE_PROJECT_DIR"
322350
chmod +x "$GRADLE_PROJECT_DIR/gradlew"
323351
ORIGINAL_JAVA_HOME="$JAVA_HOME"

0 commit comments

Comments
 (0)