• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.dex;
18 
19 import static com.android.server.pm.PackageManagerServiceCompilerMapping.getCompilerFilterForReason;
20 
21 import android.annotation.Nullable;
22 
23 /**
24  * Options used for dexopt invocations.
25  */
26 public final class DexoptOptions {
27     // When set, the profiles will be checked for updates before calling dexopt. If
28     // the apps profiles didn't update in a meaningful way (decided by the compiler), dexopt
29     // will be skipped.
30     // Currently this only affects the optimization of primary apks. Secondary dex files
31     // will always check the profiles for updates.
32     public static final int DEXOPT_CHECK_FOR_PROFILES_UPDATES = 1 << 0;
33 
34     // When set, dexopt will execute unconditionally (even if not needed).
35     public static final int DEXOPT_FORCE = 1 << 1;
36 
37     // Whether or not the invocation of dexopt is done after the boot is completed. This is used
38     // in order to adjust the priority of the compilation thread.
39     public static final int DEXOPT_BOOT_COMPLETE = 1 << 2;
40 
41     // When set, the dexopt invocation will optimize only the secondary dex files. If false, dexopt
42     // will only consider the primary apk.
43     public static final int DEXOPT_ONLY_SECONDARY_DEX = 1 << 3;
44 
45     // When set, dexopt will optimize only dex files that are used by other apps.
46     // Currently, this flag is ignored for primary apks.
47     public static final int DEXOPT_ONLY_SHARED_DEX = 1 << 4;
48 
49     // When set, dexopt will attempt to scale down the optimizations previously applied in order
50     // save disk space.
51     public static final int DEXOPT_DOWNGRADE = 1 << 5;
52 
53     // When set, dexopt will compile the dex file as a shared library even if it is not actually
54     // used by other apps. This is used to force the compilation or shared libraries declared
55     // with in the manifest with ''uses-library' before we have a chance to detect they are
56     // actually shared at runtime.
57     public static final int DEXOPT_AS_SHARED_LIBRARY = 1 << 6;
58 
59     // The name of package to optimize.
60     private final String mPackageName;
61 
62     // The intended target compiler filter. Note that dexopt might adjust the filter before the
63     // execution based on factors like: vmSafeMode and packageUsedByOtherApps.
64     private final String mCompilerFilter;
65 
66     // The set of flags for the dexopt options. It's a mix of the DEXOPT_* flags.
67     private final int mFlags;
68 
69     // When not null, dexopt will optimize only the split identified by this name.
70     // It only applies for primary apk and it's always null if mOnlySecondaryDex is true.
71     private final String mSplitName;
72 
DexoptOptions(String packageName, String compilerFilter, int flags)73     public DexoptOptions(String packageName, String compilerFilter, int flags) {
74         this(packageName, compilerFilter, /*splitName*/ null, flags);
75     }
76 
DexoptOptions(String packageName, int compilerReason, int flags)77     public DexoptOptions(String packageName, int compilerReason, int flags) {
78         this(packageName, getCompilerFilterForReason(compilerReason), flags);
79     }
80 
DexoptOptions(String packageName, String compilerFilter, String splitName, int flags)81     public DexoptOptions(String packageName, String compilerFilter, String splitName, int flags) {
82         int validityMask =
83                 DEXOPT_CHECK_FOR_PROFILES_UPDATES |
84                 DEXOPT_FORCE |
85                 DEXOPT_BOOT_COMPLETE |
86                 DEXOPT_ONLY_SECONDARY_DEX |
87                 DEXOPT_ONLY_SHARED_DEX |
88                 DEXOPT_DOWNGRADE |
89                 DEXOPT_AS_SHARED_LIBRARY;
90         if ((flags & (~validityMask)) != 0) {
91             throw new IllegalArgumentException("Invalid flags : " + Integer.toHexString(flags));
92         }
93 
94         mPackageName = packageName;
95         mCompilerFilter = compilerFilter;
96         mFlags = flags;
97         mSplitName = splitName;
98     }
99 
getPackageName()100     public String getPackageName() {
101         return mPackageName;
102     }
103 
isCheckForProfileUpdates()104     public boolean isCheckForProfileUpdates() {
105         return (mFlags & DEXOPT_CHECK_FOR_PROFILES_UPDATES) != 0;
106     }
107 
getCompilerFilter()108     public String getCompilerFilter() {
109         return mCompilerFilter;
110     }
111 
isForce()112     public boolean isForce() {
113         return (mFlags & DEXOPT_FORCE) != 0;
114     }
115 
isBootComplete()116     public boolean isBootComplete() {
117         return (mFlags & DEXOPT_BOOT_COMPLETE) != 0;
118     }
119 
isDexoptOnlySecondaryDex()120     public boolean isDexoptOnlySecondaryDex() {
121         return (mFlags & DEXOPT_ONLY_SECONDARY_DEX) != 0;
122     }
123 
isDexoptOnlySharedDex()124     public boolean isDexoptOnlySharedDex() {
125         return (mFlags & DEXOPT_ONLY_SHARED_DEX) != 0;
126     }
127 
isDowngrade()128     public boolean isDowngrade() {
129         return (mFlags & DEXOPT_DOWNGRADE) != 0;
130     }
131 
isDexoptAsSharedLibrary()132     public boolean isDexoptAsSharedLibrary() {
133         return (mFlags & DEXOPT_AS_SHARED_LIBRARY) != 0;
134     }
135 
getSplitName()136     public String getSplitName() {
137         return mSplitName;
138     }
139 
getFlags()140     public int getFlags() {
141         return mFlags;
142     }
143 }
144