@@ -126,7 +126,7 @@ private suspend fun Context.installPackagesInternal(
126
126
} catch (e: Exception ) {
127
127
Log .w(TAG , " Error setting dontKillApp" , e)
128
128
}
129
- val sessionId: Int
129
+ var sessionId: Int
130
130
var session: PackageInstaller .Session ? = null
131
131
try {
132
132
val sessionInfo = packageInstaller.mySessions.firstOrNull{ it.appLabel == key }
@@ -147,7 +147,7 @@ private suspend fun Context.installPackagesInternal(
147
147
148
148
for (component in componentNames) {
149
149
val currentSize: Long = try {
150
- val inputStream = session.openRead(component)
150
+ val inputStream = session!! .openRead(component)
151
151
val totalSize = withContext(Dispatchers .IO ) {
152
152
val buffer = ByteArray (4096 )
153
153
var total = 0L
@@ -162,10 +162,17 @@ private suspend fun Context.installPackagesInternal(
162
162
} catch (e: IOException ) {
163
163
Log .w(TAG , " installPackagesInternal session open read error, ${e.message} " )
164
164
0L
165
+ } catch (e: SecurityException ) {
166
+ Log .w(TAG , " installPackagesInternal session open read error, ${e.message} " )
167
+ // Handling exceptions java.lang.SecurityException: openRead not allowed after commit
168
+ session!! .abandon()
169
+ sessionId = packageInstaller.createSession(params)
170
+ session = packageInstaller.openSession(sessionId)
171
+ 0L
165
172
}
166
173
167
174
Log .d(TAG , " installPackagesInternal component: $component currentSize:$currentSize " )
168
- session.openWrite(component, currentSize, - 1 ).use { outputStream ->
175
+ session!! .openWrite(component, currentSize, - 1 ).use { outputStream ->
169
176
try {
170
177
writeComponent(currentSize, notifyId, component, outputStream)
171
178
session.fsync(outputStream)
@@ -197,7 +204,7 @@ private suspend fun Context.installPackagesInternal(
197
204
)!!
198
205
199
206
emitProgress(notifyId, CommitingSession )
200
- session.commit(pendingIntent.intentSender)
207
+ session!! .commit(pendingIntent.intentSender)
201
208
// don't abandon if `finally` step is reached after this point
202
209
// session.close()
203
210
0 commit comments