1 /* 2 * Copyright (C) 2020 The Dagger Authors. 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 dagger.hilt.android.testing; 18 19 import android.content.Context; 20 import androidx.multidex.MultiDexApplication; 21 import dagger.hilt.android.internal.testing.TestApplicationComponentManager; 22 import dagger.hilt.android.internal.testing.TestApplicationComponentManagerHolder; 23 import dagger.hilt.internal.GeneratedComponentManager; 24 25 /** 26 * An application that can be used for Android instrumentation or Robolectric tests using Hilt. 27 */ 28 public final class HiltTestApplication extends MultiDexApplication 29 implements GeneratedComponentManager<Object>, TestApplicationComponentManagerHolder { 30 31 // This field is initialized in attachBaseContext to avoid pulling the generated component into 32 // the main dex. We could possibly avoid this by class loading TestComponentDataSupplier lazily 33 // rather than in the TestApplicationComponentManager constructor. 34 private TestApplicationComponentManager componentManager; 35 36 @Override attachBaseContext(Context base)37 protected final void attachBaseContext(Context base) { 38 super.attachBaseContext(base); 39 componentManager = new TestApplicationComponentManager(this); 40 } 41 42 @Override componentManager()43 public final Object componentManager() { 44 return componentManager; 45 } 46 47 @Override generatedComponent()48 public final Object generatedComponent() { 49 return componentManager.generatedComponent(); 50 } 51 } 52