@@ -23,12 +23,11 @@ import android.util.Log
23
23
import androidx.annotation.RequiresApi
24
24
import androidx.benchmark.DeviceInfo
25
25
import androidx.benchmark.Shell
26
- import androidx.benchmark.macro.MacrobenchmarkScope.Companion.Api24Helper.shaderDir
26
+ import androidx.benchmark.macro.MacrobenchmarkScope.Companion.Api24ContextHelper.createDeviceProtectedStorageContextCompat
27
27
import androidx.benchmark.macro.perfetto.forceTrace
28
28
import androidx.test.platform.app.InstrumentationRegistry
29
29
import androidx.test.uiautomator.UiDevice
30
30
import androidx.tracing.trace
31
- import java.io.File
32
31
33
32
/* *
34
33
* Provides access to common operations in app automation, such as killing the app,
@@ -242,17 +241,30 @@ public class MacrobenchmarkScope(
242
241
public fun dropShaderCache () {
243
242
Log .d(TAG , " Dropping shader cache for $packageName " )
244
243
val dropError = ProfileInstallBroadcast .dropShaderCache(packageName)
245
- if (dropError != null ) {
246
- if (Shell .isSessionRooted()) {
247
- // fall back to root approach
248
- val path = getShaderCachePath(packageName)
249
- Shell .executeScriptSilent(" find $path -type f | xargs rm" )
250
- } else {
244
+ if (dropError != null && ! DeviceInfo .isEmulator) {
245
+ if (! dropShaderCacheRoot()) {
251
246
throw IllegalStateException (dropError)
252
247
}
253
248
}
254
249
}
255
250
251
+ /* *
252
+ * Returns true if rooted, and delete operation succeeded without error.
253
+ *
254
+ * Note that if no files are present in the shader dir, true will still be returned.
255
+ */
256
+ internal fun dropShaderCacheRoot (): Boolean {
257
+ if (Shell .isSessionRooted()) {
258
+ // fall back to root approach
259
+ val path = getShaderCachePath(packageName)
260
+
261
+ // Use -f to allow missing files, since app may not have generated shaders.
262
+ Shell .executeScriptSilent(" find $path -type f | xargs rm -f" )
263
+ return true
264
+ }
265
+ return false
266
+ }
267
+
256
268
/* *
257
269
* Drop caches via setprop added in API 31
258
270
*
@@ -312,21 +324,24 @@ public class MacrobenchmarkScope(
312
324
val context = InstrumentationRegistry .getInstrumentation().context
313
325
314
326
// Shader paths sourced from ActivityThread.java
315
- return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
316
- context.shaderDir
327
+ val shaderDirectory = if (Build .VERSION .SDK_INT >= 34 ) {
328
+ // U switched to cache dir, so it's not deleted on each app update
329
+ context.createDeviceProtectedStorageContextCompat().cacheDir
330
+ } else if (Build .VERSION .SDK_INT >= 24 ) {
331
+ // shaders started using device protected storage context once it was added in N
332
+ context.createDeviceProtectedStorageContextCompat().codeCacheDir
317
333
} else {
318
334
// getCodeCacheDir was added in L, but not used by platform for shaders until M
319
335
// as M is minApi of this library, that's all we support here
320
336
context.codeCacheDir
321
- }.absolutePath.replace(context.packageName, packageName)
337
+ }
338
+ return shaderDirectory.absolutePath.replace(context.packageName, packageName)
322
339
}
323
340
324
341
@RequiresApi(Build .VERSION_CODES .N )
325
- internal object Api24Helper {
326
- val Context .shaderDir: File
327
- get() =
328
- // shaders started using device protected storage context once it was added in N
329
- createDeviceProtectedStorageContext().codeCacheDir
342
+ internal object Api24ContextHelper {
343
+ fun Context.createDeviceProtectedStorageContextCompat (): Context =
344
+ createDeviceProtectedStorageContext()
330
345
}
331
346
}
332
347
}
0 commit comments