@@ -265,6 +265,85 @@ if [ -z "$GRADLE_PROJECT_DIR" ]; then
265265 exit 1
266266fi
267267
268+ ba_log " Configuring instrumentation test sources in $GRADLE_PROJECT_DIR "
269+ APP_BUILD_GRADLE=" $GRADLE_PROJECT_DIR /app/build.gradle"
270+ if [ -f " $APP_BUILD_GRADLE " ]; then
271+ python3 - " $APP_BUILD_GRADLE " << 'PYTHON '
272+ import pathlib
273+ import re
274+ import sys
275+
276+ path = pathlib.Path(sys.argv[1 ])
277+ text = path.read_text()
278+ modified = False
279+
280+ if " androidx.test.runner.AndroidJUnitRunner" not in text:
281+ def add_runner (match ):
282+ prefix = match.group(0 )
283+ return prefix + " \n testInstrumentationRunner \" androidx.test.runner.AndroidJUnitRunner\" "
284+
285+ new_text, count = re.subn(r " ( defaultConfig\s * \{ ) " , add_runner, text, count = 1 , flags = re.MULTILINE )
286+ if count:
287+ text = new_text
288+ modified = True
289+ else :
290+ raise SystemExit (" defaultConfig block not found while adding instrumentation runner" )
291+
292+ dependencies_to_add = (
293+ ' androidTestImplementation "androidx.test.ext:junit:1.1.5"\n '
294+ ' androidTestImplementation "androidx.test.espresso:espresso-core:3.5.1"'
295+ )
296+
297+ if " androidx.test.ext:junit" not in text and " androidx.test.espresso:espresso-core" not in text:
298+ def add_dependencies (match ):
299+ prefix = match.group(0 )
300+ return prefix + " \n " + dependencies_to_add
301+
302+ new_text, count = re.subn(r " ( dependencies\s * \{ ) " , add_dependencies, text, count = 1 , flags = re.MULTILINE )
303+ if count:
304+ text = new_text
305+ modified = True
306+ else :
307+ raise SystemExit (" dependencies block not found while adding androidTest dependencies" )
308+
309+ if modified:
310+ if not text.endswith(" \n " ):
311+ text += " \n "
312+ path.write_text(text)
313+ PYTHON
314+ ba_log " Ensured instrumentation runner and dependencies are declared"
315+ else
316+ ba_log " Warning: Gradle build file not found at $APP_BUILD_GRADLE ; skipping instrumentation dependency configuration" >&2
317+ fi
318+
319+ TEST_SRC_DIR=" $GRADLE_PROJECT_DIR /app/src/androidTest/java/${PACKAGE_PATH} "
320+ mkdir -p " $TEST_SRC_DIR "
321+ TEST_CLASS=" $TEST_SRC_DIR /HelloCodenameOneInstrumentedTest.java"
322+ cat > " $TEST_CLASS " << EOF
323+ package $PACKAGE_NAME ;
324+
325+ import android.content.Context;
326+
327+ import androidx.test.ext.junit.runners.AndroidJUnit4;
328+ import androidx.test.platform.app.InstrumentationRegistry;
329+
330+ import org.junit.Test;
331+ import org.junit.runner.RunWith;
332+
333+ import static org.junit.Assert.assertEquals;
334+
335+ @RunWith(AndroidJUnit4.class)
336+ public class HelloCodenameOneInstrumentedTest {
337+
338+ @Test
339+ public void useAppContext() {
340+ Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
341+ assertEquals("$PACKAGE_NAME ", appContext.getPackageName());
342+ }
343+ }
344+ EOF
345+ ba_log " Created instrumentation test at $TEST_CLASS "
346+
268347ba_log " Invoking Gradle build in $GRADLE_PROJECT_DIR "
269348chmod +x " $GRADLE_PROJECT_DIR /gradlew"
270349ORIGINAL_JAVA_HOME=" $JAVA_HOME "
@@ -282,4 +361,13 @@ export JAVA_HOME="$ORIGINAL_JAVA_HOME"
282361
283362APK_PATH=$( find " $GRADLE_PROJECT_DIR " -path " */outputs/apk/debug/*.apk" | head -n 1 || true)
284363[ -n " $APK_PATH " ] || { ba_log " Gradle build completed but no APK was found" >&2 ; exit 1; }
285- ba_log " Successfully built Android APK at $APK_PATH "
364+ ba_log " Successfully built Android APK at $APK_PATH "
365+
366+ if [ -n " ${GITHUB_OUTPUT:- } " ]; then
367+ {
368+ echo " gradle_project_dir=$GRADLE_PROJECT_DIR "
369+ echo " apk_path=$APK_PATH "
370+ echo " instrumentation_test_class=$PACKAGE_NAME .HelloCodenameOneInstrumentedTest"
371+ } >> " $GITHUB_OUTPUT "
372+ ba_log " Published GitHub Actions outputs for downstream steps"
373+ fi
0 commit comments