Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.pm; |
| 18 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageParser; |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 23 | import android.os.Environment; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 24 | import android.os.PowerManager; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 25 | import android.os.UserHandle; |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 26 | import android.os.WorkSource; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.util.Slog; |
| 29 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 30 | import com.android.internal.os.InstallerConnection.InstallerException; |
| 31 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 32 | import java.io.File; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 33 | import java.io.IOException; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 34 | import java.util.List; |
| 35 | |
| 36 | import dalvik.system.DexFile; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 37 | |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 38 | import static com.android.server.pm.Installer.DEXOPT_BOOTCOMPLETE; |
| 39 | import static com.android.server.pm.Installer.DEXOPT_DEBUGGABLE; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 40 | import static com.android.server.pm.Installer.DEXOPT_PROFILE_GUIDED; |
Todd Kennedy | fa54ab7 | 2015-09-25 07:46:12 -0700 | [diff] [blame] | 41 | import static com.android.server.pm.Installer.DEXOPT_PUBLIC; |
| 42 | import static com.android.server.pm.Installer.DEXOPT_SAFEMODE; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 43 | import static com.android.server.pm.InstructionSets.getAppDexInstructionSets; |
| 44 | import static com.android.server.pm.InstructionSets.getDexCodeInstructionSets; |
Andreas Gampe | 47c170a | 2016-03-30 17:21:19 -0700 | [diff] [blame] | 45 | import static com.android.server.pm.PackageManagerServiceCompilerMapping.getNonProfileGuidedCompilerFilter; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Helper class for running dexopt command on packages. |
| 49 | */ |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 50 | class PackageDexOptimizer { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 51 | private static final String TAG = "PackageManager.DexOptimizer"; |
| 52 | static final String OAT_DIR_NAME = "oat"; |
| 53 | // TODO b/19550105 Remove error codes and use exceptions |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 54 | static final int DEX_OPT_SKIPPED = 0; |
| 55 | static final int DEX_OPT_PERFORMED = 1; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 56 | static final int DEX_OPT_FAILED = -1; |
| 57 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 58 | private final Installer mInstaller; |
| 59 | private final Object mInstallLock; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 60 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 61 | private final PowerManager.WakeLock mDexoptWakeLock; |
| 62 | private volatile boolean mSystemReady; |
| 63 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 64 | PackageDexOptimizer(Installer installer, Object installLock, Context context, |
| 65 | String wakeLockTag) { |
| 66 | this.mInstaller = installer; |
| 67 | this.mInstallLock = installLock; |
| 68 | |
| 69 | PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 70 | mDexoptWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag); |
| 71 | } |
| 72 | |
| 73 | protected PackageDexOptimizer(PackageDexOptimizer from) { |
| 74 | this.mInstaller = from.mInstaller; |
| 75 | this.mInstallLock = from.mInstallLock; |
| 76 | this.mDexoptWakeLock = from.mDexoptWakeLock; |
| 77 | this.mSystemReady = from.mSystemReady; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 80 | static boolean canOptimizePackage(PackageParser.Package pkg) { |
Calin Juravle | 6dfd83d | 2016-01-29 18:23:57 +0000 | [diff] [blame] | 81 | return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0; |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Performs dexopt on all code paths and libraries of the specified package for specified |
| 86 | * instruction sets. |
| 87 | * |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 88 | * <p>Calls to {@link com.android.server.pm.Installer#dexopt} on {@link #mInstaller} are |
| 89 | * synchronized on {@link #mInstallLock}. |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 90 | */ |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame^] | 91 | int performDexOpt(PackageParser.Package pkg, String[] sharedLibraries, |
| 92 | String[] instructionSets, boolean checkProfiles, String targetCompilationFilter) { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 93 | synchronized (mInstallLock) { |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 94 | final boolean useLock = mSystemReady; |
| 95 | if (useLock) { |
| 96 | mDexoptWakeLock.setWorkSource(new WorkSource(pkg.applicationInfo.uid)); |
| 97 | mDexoptWakeLock.acquire(); |
| 98 | } |
| 99 | try { |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame^] | 100 | return performDexOptLI(pkg, sharedLibraries, instructionSets, checkProfiles, |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 101 | targetCompilationFilter); |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 102 | } finally { |
| 103 | if (useLock) { |
| 104 | mDexoptWakeLock.release(); |
| 105 | } |
| 106 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 110 | /** |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 111 | * Adjust the given dexopt-needed value. Can be overridden to influence the decision to |
| 112 | * optimize or not (and in what way). |
| 113 | */ |
| 114 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 115 | return dexoptNeeded; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Adjust the given dexopt flags that will be passed to the installer. |
| 120 | */ |
| 121 | protected int adjustDexoptFlags(int dexoptFlags) { |
| 122 | return dexoptFlags; |
| 123 | } |
| 124 | |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame^] | 125 | private int performDexOptLI(PackageParser.Package pkg, String[] sharedLibraries, |
| 126 | String[] targetInstructionSets, boolean checkProfiles, String targetCompilerFilter) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 127 | final String[] instructionSets = targetInstructionSets != null ? |
| 128 | targetInstructionSets : getAppDexInstructionSets(pkg.applicationInfo); |
| 129 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 130 | if (!canOptimizePackage(pkg)) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 131 | return DEX_OPT_SKIPPED; |
| 132 | } |
| 133 | |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 134 | final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly(); |
| 135 | final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid); |
| 136 | |
| 137 | boolean isProfileGuidedFilter = DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter); |
| 138 | // If any part of the app is used by other apps, we cannot use profile-guided |
| 139 | // compilation. |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 140 | if (isProfileGuidedFilter && isUsedByOtherApps(pkg)) { |
| 141 | checkProfiles = false; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 142 | |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 143 | targetCompilerFilter = getNonProfileGuidedCompilerFilter(targetCompilerFilter); |
| 144 | if (DexFile.isProfileGuidedCompilerFilter(targetCompilerFilter)) { |
| 145 | throw new IllegalStateException(targetCompilerFilter); |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 146 | } |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 147 | isProfileGuidedFilter = false; |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // If we're asked to take profile updates into account, check now. |
| 151 | boolean newProfile = false; |
| 152 | if (checkProfiles && isProfileGuidedFilter) { |
| 153 | // Merge profiles, see if we need to do anything. |
| 154 | try { |
| 155 | newProfile = mInstaller.mergeProfiles(sharedGid, pkg.packageName); |
| 156 | } catch (InstallerException e) { |
| 157 | Slog.w(TAG, "Failed to merge profiles", e); |
| 158 | } |
| 159 | } |
| 160 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 161 | final boolean vmSafeMode = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0; |
| 162 | final boolean debuggable = (pkg.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
| 163 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 164 | boolean performedDexOpt = false; |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 165 | boolean successfulDexOpt = true; |
| 166 | |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 167 | final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets); |
| 168 | for (String dexCodeInstructionSet : dexCodeInstructionSets) { |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 169 | for (String path : paths) { |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 170 | int dexoptNeeded; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 171 | try { |
Calin Juravle | 693f997 | 2016-02-25 15:26:20 +0000 | [diff] [blame] | 172 | dexoptNeeded = DexFile.getDexOptNeeded(path, |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 173 | dexCodeInstructionSet, targetCompilerFilter, newProfile); |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 174 | } catch (IOException ioe) { |
| 175 | Slog.w(TAG, "IOException reading apk: " + path, ioe); |
| 176 | return DEX_OPT_FAILED; |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 177 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 178 | dexoptNeeded = adjustDexoptNeeded(dexoptNeeded); |
Andreas Gampe | 47c170a | 2016-03-30 17:21:19 -0700 | [diff] [blame] | 179 | if (PackageManagerService.DEBUG_DEXOPT) { |
| 180 | Log.i(TAG, "DexoptNeeded for " + path + "@" + targetCompilerFilter + " is " + |
| 181 | dexoptNeeded); |
| 182 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 183 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 184 | final String dexoptType; |
| 185 | String oatDir = null; |
Calin Juravle | 693f997 | 2016-02-25 15:26:20 +0000 | [diff] [blame] | 186 | switch (dexoptNeeded) { |
| 187 | case DexFile.NO_DEXOPT_NEEDED: |
| 188 | continue; |
| 189 | case DexFile.DEX2OAT_NEEDED: |
| 190 | dexoptType = "dex2oat"; |
| 191 | oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet); |
| 192 | break; |
| 193 | case DexFile.PATCHOAT_NEEDED: |
| 194 | dexoptType = "patchoat"; |
| 195 | break; |
| 196 | case DexFile.SELF_PATCHOAT_NEEDED: |
| 197 | dexoptType = "self patchoat"; |
| 198 | break; |
| 199 | default: |
| 200 | throw new IllegalStateException("Invalid dexopt:" + dexoptNeeded); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 201 | } |
Narayan Kamath | 01dcb76 | 2015-05-07 17:48:42 +0100 | [diff] [blame] | 202 | |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame^] | 203 | String sharedLibrariesPath = null; |
| 204 | if (sharedLibraries != null && sharedLibraries.length != 0) { |
| 205 | StringBuilder sb = new StringBuilder(); |
| 206 | for (String lib : sharedLibraries) { |
| 207 | if (sb.length() != 0) { |
| 208 | sb.append(":"); |
| 209 | } |
| 210 | sb.append(lib); |
| 211 | } |
| 212 | sharedLibrariesPath = sb.toString(); |
| 213 | } |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 214 | Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg=" |
| 215 | + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet |
| 216 | + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame^] | 217 | + " target-filter=" + targetCompilerFilter + " oatDir = " + oatDir |
| 218 | + " sharedLibraries=" + sharedLibrariesPath); |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 219 | // Profile guide compiled oat files should not be public. |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 220 | final boolean isPublic = !pkg.isForwardLocked() && !isProfileGuidedFilter; |
| 221 | final int profileFlag = isProfileGuidedFilter ? DEXOPT_PROFILE_GUIDED : 0; |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 222 | final int dexFlags = adjustDexoptFlags( |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 223 | ( isPublic ? DEXOPT_PUBLIC : 0) |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 224 | | (vmSafeMode ? DEXOPT_SAFEMODE : 0) |
| 225 | | (debuggable ? DEXOPT_DEBUGGABLE : 0) |
Andreas Gampe | bdd30d8 | 2016-03-20 11:32:11 -0700 | [diff] [blame] | 226 | | profileFlag |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 227 | | DEXOPT_BOOTCOMPLETE); |
| 228 | |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 229 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 230 | mInstaller.dexopt(path, sharedGid, pkg.packageName, dexCodeInstructionSet, |
Jeff Hao | c7b9482 | 2016-03-16 15:56:07 -0700 | [diff] [blame^] | 231 | dexoptNeeded, oatDir, dexFlags, targetCompilerFilter, pkg.volumeUuid, |
| 232 | sharedLibrariesPath); |
Calin Juravle | db4a79a | 2015-12-23 18:55:08 +0200 | [diff] [blame] | 233 | performedDexOpt = true; |
| 234 | } catch (InstallerException e) { |
| 235 | Slog.w(TAG, "Failed to dexopt", e); |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 236 | successfulDexOpt = false; |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 239 | } |
| 240 | |
David Brazdil | 6a3b2d2 | 2016-04-08 16:09:06 +0100 | [diff] [blame] | 241 | if (successfulDexOpt) { |
| 242 | // If we've gotten here, we're sure that no error occurred. We've either |
| 243 | // dex-opted one or more paths or instruction sets or we've skipped |
| 244 | // all of them because they are up to date. In both cases this package |
| 245 | // doesn't need dexopt any longer. |
| 246 | return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED; |
| 247 | } else { |
| 248 | return DEX_OPT_FAILED; |
| 249 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 252 | /** |
| 253 | * Creates oat dir for the specified package. In certain cases oat directory |
| 254 | * <strong>cannot</strong> be created: |
| 255 | * <ul> |
| 256 | * <li>{@code pkg} is a system app, which is not updated.</li> |
| 257 | * <li>Package location is not a directory, i.e. monolithic install.</li> |
| 258 | * </ul> |
| 259 | * |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 260 | * @return Absolute path to the oat directory or null, if oat directory |
| 261 | * cannot be created. |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 262 | */ |
| 263 | @Nullable |
Todd Kennedy | 27c24fb | 2015-09-17 16:49:25 -0700 | [diff] [blame] | 264 | private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet) { |
Fyodor Kupolov | ebcac16 | 2015-09-09 15:56:45 -0700 | [diff] [blame] | 265 | if (!pkg.canHaveOatDir()) { |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 266 | return null; |
| 267 | } |
| 268 | File codePath = new File(pkg.codePath); |
| 269 | if (codePath.isDirectory()) { |
| 270 | File oatDir = getOatDir(codePath); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 271 | try { |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 272 | mInstaller.createOatDir(oatDir.getAbsolutePath(), dexInstructionSet); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 273 | } catch (InstallerException e) { |
| 274 | Slog.w(TAG, "Failed to create oat dir", e); |
| 275 | return null; |
| 276 | } |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 277 | return oatDir.getAbsolutePath(); |
Fyodor Kupolov | b94c165 | 2015-03-03 12:25:30 -0800 | [diff] [blame] | 278 | } |
| 279 | return null; |
| 280 | } |
| 281 | |
| 282 | static File getOatDir(File codePath) { |
| 283 | return new File(codePath, OAT_DIR_NAME); |
| 284 | } |
| 285 | |
Fyodor Kupolov | a627c09 | 2015-05-05 18:44:39 -0700 | [diff] [blame] | 286 | void systemReady() { |
| 287 | mSystemReady = true; |
| 288 | } |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 289 | |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 290 | /** |
| 291 | * Returns true if the profiling data collected for the given app indicate |
| 292 | * that the apps's APK has been loaded by another app. |
| 293 | * Note that this returns false for all forward-locked apps and apps without |
| 294 | * any collected profiling data. |
| 295 | */ |
| 296 | public static boolean isUsedByOtherApps(PackageParser.Package pkg) { |
| 297 | if (pkg.isForwardLocked()) { |
| 298 | // Skip the check for forward locked packages since they don't share their code. |
| 299 | return false; |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 300 | } |
David Brazdil | 90e2699 | 2016-04-18 14:08:52 +0100 | [diff] [blame] | 301 | |
| 302 | for (String apkPath : pkg.getAllCodePathsExcludingResourceOnly()) { |
| 303 | try { |
| 304 | apkPath = new File(apkPath).getCanonicalPath(); |
| 305 | } catch (IOException e) { |
| 306 | // Log an error but continue without it. |
| 307 | Slog.w(TAG, "Failed to get canonical path", e); |
| 308 | } |
| 309 | String useMarker = apkPath.replace('/', '@'); |
| 310 | final int[] currentUserIds = UserManagerService.getInstance().getUserIds(); |
| 311 | for (int i = 0; i < currentUserIds.length; i++) { |
| 312 | File profileDir = |
| 313 | Environment.getDataProfilesDeForeignDexDirectory(currentUserIds[i]); |
| 314 | File foreignUseMark = new File(profileDir, useMarker); |
| 315 | if (foreignUseMark.exists()) { |
| 316 | return true; |
| 317 | } |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | return false; |
| 321 | } |
| 322 | |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 323 | /** |
| 324 | * A specialized PackageDexOptimizer that overrides already-installed checks, forcing a |
| 325 | * dexopt path. |
| 326 | */ |
| 327 | public static class ForcedUpdatePackageDexOptimizer extends PackageDexOptimizer { |
| 328 | |
| 329 | public ForcedUpdatePackageDexOptimizer(Installer installer, Object installLock, |
| 330 | Context context, String wakeLockTag) { |
| 331 | super(installer, installLock, context, wakeLockTag); |
| 332 | } |
| 333 | |
| 334 | public ForcedUpdatePackageDexOptimizer(PackageDexOptimizer from) { |
| 335 | super(from); |
| 336 | } |
| 337 | |
| 338 | @Override |
Andreas Gampe | a890875 | 2015-11-10 08:58:14 -0800 | [diff] [blame] | 339 | protected int adjustDexoptNeeded(int dexoptNeeded) { |
| 340 | // Ensure compilation, no matter the current state. |
| 341 | // TODO: The return value is wrong when patchoat is needed. |
| 342 | return DexFile.DEX2OAT_NEEDED; |
| 343 | } |
| 344 | } |
Fyodor Kupolov | 7487657 | 2015-02-23 17:14:45 -0800 | [diff] [blame] | 345 | } |