@@ -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+
292309if modified:
293310 if not text.endswith(" \n " ):
294311 text += " \n "
295312 path.write_text(text)
296313PYTHON
297- ba_log " Ensured instrumentation runner is declared"
314+ ba_log " Ensured instrumentation runner and libraries are declared"
298315else
299316 ba_log " Warning: Gradle build file not found at $APP_BUILD_GRADLE ; skipping instrumentation dependency configuration" >&2
300317fi
@@ -318,6 +335,17 @@ public class HelloCodenameOneInstrumentedTest extends InstrumentationTestCase {
318335EOF
319336ba_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+
321349ba_log " Invoking Gradle build in $GRADLE_PROJECT_DIR "
322350chmod +x " $GRADLE_PROJECT_DIR /gradlew"
323351ORIGINAL_JAVA_HOME=" $JAVA_HOME "
0 commit comments