Skip to content

Commit

Permalink
Add a Java-only gradle application.
Browse files Browse the repository at this point in the history
This CL adds a Java-only gradle application and adds a test to repro the issue seen in #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
  • Loading branch information
bcorso authored and Dagger Team committed Dec 21, 2021
1 parent 1205c26 commit 5fa8e76
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
69 changes: 69 additions & 0 deletions 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'
}
@@ -0,0 +1,24 @@
<!--
~ 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dagger.hilt.android.simple">

<application
android:name=".SimpleApplication"
android:label="Java-only Hilt Android"
android:taskAffinity="">
</application>
</manifest>
@@ -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();
}
}
@@ -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()));
}
}
1 change: 1 addition & 0 deletions 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'
Expand Down

0 comments on commit 5fa8e76

Please sign in to comment.