Skip to content

Commit 76c55b2

Browse files
committed
Fix Android emulator CI test file permission error
Use internal cache directory instead of external cache directory for Android instrumentation tests. On Android API 34, scoped storage restrictions prevent writing to external storage even for app-specific directories. Internal storage (cacheDir) requires no permissions and works reliably on all API levels. - Change externalCacheDir to cacheDir in TestFileUtils - Remove unnecessary READ_EXTERNAL_STORAGE permission from manifest - Remove GrantPermissionRule from all test files
1 parent 8887cfd commit 76c55b2

File tree

6 files changed

+1
-29
lines changed

6 files changed

+1
-29
lines changed

extension/android/executorch_android/src/androidTest/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
43
<application>
54
</application>
65
<instrumentation

extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/LlmModuleInstrumentationTest.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
*/
88
package org.pytorch.executorch
99

10-
import android.Manifest
1110
import androidx.test.ext.junit.runners.AndroidJUnit4
12-
import androidx.test.rule.GrantPermissionRule
1311
import java.io.File
1412
import java.io.IOException
1513
import java.net.URISyntaxException
@@ -19,7 +17,6 @@ import org.json.JSONObject
1917
import org.junit.Assert.assertEquals
2018
import org.junit.Assert.assertTrue
2119
import org.junit.Before
22-
import org.junit.Rule
2320
import org.junit.Test
2421
import org.junit.runner.RunWith
2522
import org.pytorch.executorch.TestFileUtils.getTestFilePath
@@ -51,10 +48,6 @@ class LlmModuleInstrumentationTest : LlmCallback {
5148
LlmModule(getTestFilePath(TEST_FILE_NAME), getTestFilePath(TOKENIZER_FILE_NAME), 0.0f)
5249
}
5350

54-
@get:Rule
55-
var runtimePermissionRule: GrantPermissionRule =
56-
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
57-
5851
@Test
5952
@Throws(IOException::class, URISyntaxException::class)
6053
fun testGenerate() {

extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/ModuleE2ETest.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
*/
88
package org.pytorch.executorch
99

10-
import android.Manifest
1110
import android.graphics.Bitmap
1211
import android.graphics.BitmapFactory
1312
import androidx.test.ext.junit.runners.AndroidJUnit4
14-
import androidx.test.rule.GrantPermissionRule
1513
import java.io.File
1614
import java.io.IOException
1715
import java.net.URISyntaxException
1816
import org.apache.commons.io.FileUtils
1917
import org.junit.Assert
2018
import org.junit.Assert.assertArrayEquals
21-
import org.junit.Rule
2219
import org.junit.Test
2320
import org.junit.runner.RunWith
2421
import org.pytorch.executorch.TensorImageUtils.bitmapToFloat32Tensor
@@ -27,9 +24,6 @@ import org.pytorch.executorch.TestFileUtils.getTestFilePath
2724
/** Unit tests for [Module]. */
2825
@RunWith(AndroidJUnit4::class)
2926
class ModuleE2ETest {
30-
@get:Rule
31-
var runtimePermissionRule: GrantPermissionRule =
32-
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
3327

3428
@Throws(IOException::class, URISyntaxException::class)
3529
fun testClassification(filePath: String) {

extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/ModuleInstrumentationTest.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
*/
88
package org.pytorch.executorch
99

10-
import android.Manifest
1110
import androidx.test.ext.junit.runners.AndroidJUnit4
12-
import androidx.test.rule.GrantPermissionRule
1311
import java.io.File
1412
import java.io.IOException
1513
import java.net.URISyntaxException
@@ -20,7 +18,6 @@ import org.apache.commons.io.FileUtils
2018
import org.junit.Assert
2119
import org.junit.Before
2220
import org.junit.Ignore
23-
import org.junit.Rule
2421
import org.junit.Test
2522
import org.junit.runner.RunWith
2623
import org.pytorch.executorch.TestFileUtils.getTestFilePath
@@ -43,10 +40,6 @@ class ModuleInstrumentationTest {
4340
inputStream.close()
4441
}
4542

46-
@get:Rule
47-
var runtimePermissionRule: GrantPermissionRule =
48-
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
49-
5043
@Ignore(
5144
"The forward has failure that needs to be fixed before enabling this test: [Executorch Error 0x12] Invalid argument: Execution failed for method: forward "
5245
)

extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/TestFileUtils.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import androidx.test.InstrumentationRegistry
66
object TestFileUtils {
77

88
fun getTestFilePath(fileName: String): String {
9-
return InstrumentationRegistry.getInstrumentation().targetContext.externalCacheDir.toString() +
10-
fileName
9+
return InstrumentationRegistry.getInstrumentation().targetContext.cacheDir.toString() + fileName
1110
}
1211
}

extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/training/TrainingModuleE2ETest.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@
88

99
package org.pytorch.executorch.training
1010

11-
import android.Manifest
1211
import android.util.Log
1312
import androidx.test.ext.junit.runners.AndroidJUnit4
14-
import androidx.test.rule.GrantPermissionRule
1513
import java.io.File
1614
import java.io.IOException
1715
import java.net.URISyntaxException
1816
import kotlin.random.Random
1917
import kotlin.test.assertContains
2018
import org.apache.commons.io.FileUtils
2119
import org.junit.Assert
22-
import org.junit.Rule
2320
import org.junit.Test
2421
import org.junit.runner.RunWith
2522
import org.pytorch.executorch.EValue
@@ -29,9 +26,6 @@ import org.pytorch.executorch.TestFileUtils
2926
/** Unit tests for [TrainingModule]. */
3027
@RunWith(AndroidJUnit4::class)
3128
class TrainingModuleE2ETest {
32-
@get:Rule
33-
var runtimePermissionRule: GrantPermissionRule =
34-
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
3529

3630
@Test
3731
@Throws(IOException::class, URISyntaxException::class)

0 commit comments

Comments
 (0)