1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import dalvik.system.DexFile; 18*795d594fSAndroid Build Coastguard Worker import dalvik.system.VMRuntime; 19*795d594fSAndroid Build Coastguard Worker import java.io.File; 20*795d594fSAndroid Build Coastguard Worker import java.io.IOException; 21*795d594fSAndroid Build Coastguard Worker 22*795d594fSAndroid Build Coastguard Worker public class Main { main(String[] args)23*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) throws Exception { 24*795d594fSAndroid Build Coastguard Worker System.loadLibrary(args[0]); 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard Worker // Register the dex file so that the runtime can pick up which 27*795d594fSAndroid Build Coastguard Worker // dex file to compile for the image. 28*795d594fSAndroid Build Coastguard Worker File file = null; 29*795d594fSAndroid Build Coastguard Worker try { 30*795d594fSAndroid Build Coastguard Worker file = createTempFile(); 31*795d594fSAndroid Build Coastguard Worker String codePath = DEX_LOCATION + "/846-multidex-data-image.jar"; 32*795d594fSAndroid Build Coastguard Worker VMRuntime.registerAppInfo( 33*795d594fSAndroid Build Coastguard Worker "test.app", 34*795d594fSAndroid Build Coastguard Worker file.getPath(), 35*795d594fSAndroid Build Coastguard Worker file.getPath(), 36*795d594fSAndroid Build Coastguard Worker new String[] {codePath}, 37*795d594fSAndroid Build Coastguard Worker VMRuntime.CODE_PATH_TYPE_PRIMARY_APK); 38*795d594fSAndroid Build Coastguard Worker } finally { 39*795d594fSAndroid Build Coastguard Worker if (file != null) { 40*795d594fSAndroid Build Coastguard Worker file.delete(); 41*795d594fSAndroid Build Coastguard Worker } 42*795d594fSAndroid Build Coastguard Worker } 43*795d594fSAndroid Build Coastguard Worker 44*795d594fSAndroid Build Coastguard Worker if (!hasOatFile() || !hasImage()) { 45*795d594fSAndroid Build Coastguard Worker // We only generate an app image if there is at least a vdex file and a boot image. 46*795d594fSAndroid Build Coastguard Worker return; 47*795d594fSAndroid Build Coastguard Worker } 48*795d594fSAndroid Build Coastguard Worker 49*795d594fSAndroid Build Coastguard Worker if (args.length == 2 && "--second-run".equals(args[1])) { 50*795d594fSAndroid Build Coastguard Worker DexFile.OptimizationInfo info = VMRuntime.getBaseApkOptimizationInfo(); 51*795d594fSAndroid Build Coastguard Worker if (!info.isOptimized() && !isInImageSpace(Main.class)) { 52*795d594fSAndroid Build Coastguard Worker throw new Error("Expected image to be loaded"); 53*795d594fSAndroid Build Coastguard Worker } 54*795d594fSAndroid Build Coastguard Worker } 55*795d594fSAndroid Build Coastguard Worker 56*795d594fSAndroid Build Coastguard Worker VMRuntime runtime = VMRuntime.getRuntime(); 57*795d594fSAndroid Build Coastguard Worker runtime.notifyStartupCompleted(); 58*795d594fSAndroid Build Coastguard Worker 59*795d594fSAndroid Build Coastguard Worker String filter = getCompilerFilter(Main.class); 60*795d594fSAndroid Build Coastguard Worker if ("speed-profile".equals(filter) || "speed".equals(filter)) { 61*795d594fSAndroid Build Coastguard Worker // We only generate an app image for filters that don't compile. 62*795d594fSAndroid Build Coastguard Worker return; 63*795d594fSAndroid Build Coastguard Worker } 64*795d594fSAndroid Build Coastguard Worker 65*795d594fSAndroid Build Coastguard Worker String instructionSet = VMRuntime.getCurrentInstructionSet(); 66*795d594fSAndroid Build Coastguard Worker // Wait for the file to be generated. 67*795d594fSAndroid Build Coastguard Worker File image = new File(DEX_LOCATION + "/" + instructionSet + "/846-multidex-data-image.art"); 68*795d594fSAndroid Build Coastguard Worker while (!image.exists()) { 69*795d594fSAndroid Build Coastguard Worker Thread.yield(); 70*795d594fSAndroid Build Coastguard Worker } 71*795d594fSAndroid Build Coastguard Worker 72*795d594fSAndroid Build Coastguard Worker // Test that we can load a class from the other dex file. We do this after creating the image to 73*795d594fSAndroid Build Coastguard Worker // check that the runtime can deal with a missing dex cache. 74*795d594fSAndroid Build Coastguard Worker Class.forName("Foo"); 75*795d594fSAndroid Build Coastguard Worker } 76*795d594fSAndroid Build Coastguard Worker hasOatFile()77*795d594fSAndroid Build Coastguard Worker private static native boolean hasOatFile(); hasImage()78*795d594fSAndroid Build Coastguard Worker private static native boolean hasImage(); getCompilerFilter(Class<?> cls)79*795d594fSAndroid Build Coastguard Worker private static native String getCompilerFilter(Class<?> cls); isInImageSpace(Class<?> cls)80*795d594fSAndroid Build Coastguard Worker private static native boolean isInImageSpace(Class<?> cls); 81*795d594fSAndroid Build Coastguard Worker 82*795d594fSAndroid Build Coastguard Worker private static final String TEMP_FILE_NAME_PREFIX = "temp"; 83*795d594fSAndroid Build Coastguard Worker private static final String TEMP_FILE_NAME_SUFFIX = "-file"; 84*795d594fSAndroid Build Coastguard Worker private static final String DEX_LOCATION = System.getenv("DEX_LOCATION"); 85*795d594fSAndroid Build Coastguard Worker createTempFile()86*795d594fSAndroid Build Coastguard Worker private static File createTempFile() throws Exception { 87*795d594fSAndroid Build Coastguard Worker try { 88*795d594fSAndroid Build Coastguard Worker return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 89*795d594fSAndroid Build Coastguard Worker } catch (IOException e) { 90*795d594fSAndroid Build Coastguard Worker System.setProperty("java.io.tmpdir", "/data/local/tmp"); 91*795d594fSAndroid Build Coastguard Worker try { 92*795d594fSAndroid Build Coastguard Worker return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 93*795d594fSAndroid Build Coastguard Worker } catch (IOException e2) { 94*795d594fSAndroid Build Coastguard Worker System.setProperty("java.io.tmpdir", "/sdcard"); 95*795d594fSAndroid Build Coastguard Worker return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 96*795d594fSAndroid Build Coastguard Worker } 97*795d594fSAndroid Build Coastguard Worker } 98*795d594fSAndroid Build Coastguard Worker } 99*795d594fSAndroid Build Coastguard Worker } 100