Troubleshooting Spoon’s RuntimeException When Creating Output Directory on Android Emulators

Troubleshooting Spoon's RuntimeException When Creating Output Directory on Android Emulators

11 April 2024 Stephan Petzl Leave a comment Tech-Help

When automating UI tests for Android applications, developers may encounter issues with taking screenshots using Spoon. A common error that manifests is the RuntimeException: Unable to create output dir, which can be particularly troublesome when transitioning between different API levels of Android emulators.

Understanding the Error

The error message Unable to create output dir: /storage/emulated/0/app_spoon-screenshots indicates that Spoon is unable to create a directory on the device’s external storage. This problem tends to occur when there are permission changes in the Android system, especially when testing on emulators with different API levels.

Solution Overview

To resolve the issue, developers need to adjust the permissions in the application’s manifest file. This involves removing the maxSdkVersion attribute from the WRITE_EXTERNAL_STORAGE permission entry and ensuring that the permissions are correctly granted to the application.

Step-by-Step Solution

  1. Modify the application’s manifest file to remove the maxSdkVersion attribute from the WRITE_EXTERNAL_STORAGE permission:

    <!-- Strip away maxSdkVersion -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                     tools:remove="android:maxSdkVersion"/>
    
    <!-- Add the permission with no maxSdkVersion defined -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  2. After adjusting the manifest, build the application and test APKs, then install them:

    ./gradlew uninstallAll installDebug installDebugAndroidTest
  3. Grant the necessary permissions using ADB commands:

    adb shell pm grant your.package.name android.permission.WRITE_EXTERNAL_STORAGE
    adb shell pm grant your.package.name android.permission.READ_EXTERNAL_STORAGE

    Replace your.package.name with the actual package name of your application.

  4. Verify that the permissions are successfully granted by listing all installed packages:

    adb shell 'pm list packages -f'
  5. Run the tests with Spoon, while disabling GIF generation for performance reasons:

    export APK=build/outputs/apk/debug/debug.apk
    export TEST_APK=build/outputs/apk/androidTest/debug/debug-androidTest.apk
    
    java -jar spoon-runner-2.0.0.jar --debug --disable-gif "$TEST_APK" "$APK"

Important Considerations

  • Ensure that the manifest changes are reflected in the merged AndroidManifest.xml of your application.
  • Check that the ADB commands for granting permissions execute successfully.
  • Remember to replace placeholders with your actual application’s details.

Conclusion

By following the steps outlined above, developers should be able to overcome the RuntimeException encountered when Spoon attempts to create an output directory for screenshots on Android emulators. Managing permissions carefully and ensuring that they are granted as needed is key to successful UI testing with Spoon.

Like this article? there’s more where that came from!