re: android/android-test#743 (comment)
The workaround is to write to the app external cache instead:
/**
* Basically [AllureAndroidJUnitRunner] except:
* - [Allure.lifecycle] is set to [BniAllureLifecycle]
* - without [ExternalStoragePermissionsListener] which doesn't work on Android 11
*/
open class AndroidTestRunner : AndroidJUnitRunner() {
override fun onCreate(arguments: Bundle) {
Allure.lifecycle = BniAllureLifecycle
val listenerArg = listOfNotNull(
arguments.getCharSequence("listener"),
AllureJunit4::class.java.name
).joinToString(separator = ",")
arguments.putCharSequence("listener", listenerArg)
super.onCreate(arguments)
}
// ...
}
/**
* Custom [AllureLifecycle] that writes Allure files to app external cache to work around new
* scoped storage limitations introduced in Android 11.
*/
object BniAllureLifecycle : AllureLifecycle(writer = BniAllureResultsWriter)
private object BniAllureResultsWriter : AllureResultsWriter {
private var cachedContext: Context? = null
private var cachedDelegate: AllureResultsWriter? = null
private val delegate: AllureResultsWriter
get() {
val context = targetContext()
return cachedDelegate
?.takeIf { context == cachedContext }
?: FileSystemResultsWriter(context.externalCacheDir!!.resolve("allure-results"))
.also {
cachedContext = context
cachedDelegate = it
}
}
override fun write(testResult: TestResult) {
delegate.write(testResult)
}
override fun write(testResultContainer: TestResultContainer) {
delegate.write(testResultContainer)
}
override fun write(source: String, attachment: InputStream) {
delegate.write(source, attachment)
}
}
Run with the custom AndroidTestRunner
then afterwards pull results from $(adb shell echo \$EXTERNAL_STORAGE)/Android/data/YOUR_PACKAGE/cache/allure-results
.
Hello Larry
I have faced the problem of running Instrumentation Espresso tests for Android 11 using AllureAndroidJUnitRunner.
I tried to use your solution but got a question. How can I import
targetContext()
function? Could I ask you to add used imports to this allure-kotlin-android-11.md file or provide the link to source code (if possible). Thank you in advance!