From 5fa8e768bb74ac77b89ea8c6894461ed71207691 Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Sun, 19 Dec 2021 15:06:23 -0800 Subject: [PATCH] Add a Java-only gradle application. This CL adds a Java-only gradle application and adds a test to repro the issue seen in https://github.com/google/dagger/issues/3119. Currently, the regression test asserts that calling the EntryPointAccessors method throws. A follow-up CL will fix this issue. RELNOTES=N/A PiperOrigin-RevId: 417294644 --- .../simple/app-java-only/build.gradle | 69 +++++++++++++++++++ .../src/main/AndroidManifest.xml | 24 +++++++ .../android/simple/SimpleApplication.java | 52 ++++++++++++++ .../dagger/hilt/android/simple/BuildTest.java | 48 +++++++++++++ .../hilt-android/simple/settings.gradle | 1 + 5 files changed, 194 insertions(+) create mode 100644 javatests/artifacts/hilt-android/simple/app-java-only/build.gradle create mode 100644 javatests/artifacts/hilt-android/simple/app-java-only/src/main/AndroidManifest.xml create mode 100644 javatests/artifacts/hilt-android/simple/app-java-only/src/main/java/dagger/hilt/android/simple/SimpleApplication.java create mode 100644 javatests/artifacts/hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java diff --git a/javatests/artifacts/hilt-android/simple/app-java-only/build.gradle b/javatests/artifacts/hilt-android/simple/app-java-only/build.gradle new file mode 100644 index 00000000000..9d17cd917dd --- /dev/null +++ b/javatests/artifacts/hilt-android/simple/app-java-only/build.gradle @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2021 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +apply plugin: 'com.android.application' +apply plugin: 'dagger.hilt.android.plugin' + +android { + compileSdkVersion 30 + buildToolsVersion "30.0.2" + + defaultConfig { + applicationId "dagger.hilt.android.simple" + minSdkVersion 15 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + compileOptions { + sourceCompatibility 1.8 + targetCompatibility 1.8 + } + testOptions { + unitTests.includeAndroidResources = true + } + lintOptions { + checkReleaseBuilds = false + } +} + +hilt { + enableTransformForLocalTests = true +} + +configurations.all { + resolutionStrategy.eachDependency { DependencyResolveDetails details -> + if ("$dagger_version" == 'LOCAL-SNAPSHOT' + && details.requested.group == 'com.google.dagger') { + details.useVersion 'LOCAL-SNAPSHOT' + details.because 'LOCAL-SNAPSHOT should act as latest version.' + } + } +} + +dependencies { + implementation "com.google.dagger:hilt-android:$dagger_version" + annotationProcessor "com.google.dagger:hilt-compiler:$dagger_version" + + testImplementation 'com.google.truth:truth:1.0.1' + testImplementation 'junit:junit:4.13' + testImplementation 'org.robolectric:robolectric:4.5-alpha-3' + testImplementation 'androidx.core:core:1.3.2' + testImplementation 'androidx.test.ext:junit:1.1.2' + testImplementation 'androidx.test:runner:1.3.0' + testImplementation 'androidx.test.espresso:espresso-core:3.3.0' +} diff --git a/javatests/artifacts/hilt-android/simple/app-java-only/src/main/AndroidManifest.xml b/javatests/artifacts/hilt-android/simple/app-java-only/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..cae4e88fa17 --- /dev/null +++ b/javatests/artifacts/hilt-android/simple/app-java-only/src/main/AndroidManifest.xml @@ -0,0 +1,24 @@ + + + + + + diff --git a/javatests/artifacts/hilt-android/simple/app-java-only/src/main/java/dagger/hilt/android/simple/SimpleApplication.java b/javatests/artifacts/hilt-android/simple/app-java-only/src/main/java/dagger/hilt/android/simple/SimpleApplication.java new file mode 100644 index 00000000000..2a0d8be9ae5 --- /dev/null +++ b/javatests/artifacts/hilt-android/simple/app-java-only/src/main/java/dagger/hilt/android/simple/SimpleApplication.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2021 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.hilt.android.simple; + +import android.app.Application; +import dagger.Module; +import dagger.Provides; +import dagger.hilt.EntryPoint; +import dagger.hilt.InstallIn; +import dagger.hilt.android.EntryPointAccessors; +import dagger.hilt.android.HiltAndroidApp; +import dagger.hilt.components.SingletonComponent; +import javax.inject.Inject; + +/** A java-only application that uses Hilt. */ +@HiltAndroidApp +public class SimpleApplication extends Application { + @Module + @InstallIn(SingletonComponent.class) + interface MyModule { + @Provides + static String provideString() { + return "some string"; + } + } + + @EntryPoint + @InstallIn(SingletonComponent.class) + interface MyEntryPoint { + String getString(); + } + + @Inject String str; + + public String getStringEntryPoint() { + return EntryPointAccessors.fromApplication(this, MyEntryPoint.class).getString(); + } +} diff --git a/javatests/artifacts/hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java b/javatests/artifacts/hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java new file mode 100644 index 00000000000..1f2ae9fc317 --- /dev/null +++ b/javatests/artifacts/hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2021 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.hilt.android.simple; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; + +import android.os.Build; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +/** + * Regression test for https://github.com/google/dagger/issues/3119 + */ +@RunWith(AndroidJUnit4.class) +// Robolectric requires Java9 to run API 29 and above, so use API 28 instead +@Config(sdk = Build.VERSION_CODES.P, application = SimpleApplication.class) +public class BuildTest { + @Test + public void useAppContext() { + assertThat((Object) ApplicationProvider.getApplicationContext()) + .isInstanceOf(SimpleApplication.class); + SimpleApplication app = (SimpleApplication) ApplicationProvider.getApplicationContext(); + assertThat(app.str).isNotNull(); + + // TODO(bcorso): This should no longer throw after we fix the issue. + assertThrows( + NoClassDefFoundError.class, + () -> assertThat(app.str).isEqualTo(app.getStringEntryPoint())); + } +} diff --git a/javatests/artifacts/hilt-android/simple/settings.gradle b/javatests/artifacts/hilt-android/simple/settings.gradle index 7ebdbeae700..9dc59db7ebd 100644 --- a/javatests/artifacts/hilt-android/simple/settings.gradle +++ b/javatests/artifacts/hilt-android/simple/settings.gradle @@ -1,5 +1,6 @@ rootProject.name='Simple Hilt Android' include ':app' +include ':app-java-only' include ':feature' include ':lib' include ':deep-android-lib'