forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This CL adds a Java-only gradle application and adds a test to repro the issue seen in #3119. A follow-up CL will fix this issue. RELNOTES=N/A PiperOrigin-RevId: 417294644
- Loading branch information
Showing
6 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
javatests/artifacts/hilt-android/simple/app-java-only/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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 | ||
} | ||
} | ||
|
||
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' | ||
} |
20 changes: 20 additions & 0 deletions
20
javatests/artifacts/hilt-android/simple/app-java-only/src/debug/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!-- | ||
~ Copyright (C) 2020 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"> | ||
</application> | ||
</manifest> |
21 changes: 21 additions & 0 deletions
21
javatests/artifacts/hilt-android/simple/app-java-only/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!-- | ||
~ Copyright (C) 2020 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"> | ||
</application> | ||
</manifest> |
55 changes: 55 additions & 0 deletions
55
...roid/simple/app-java-only/src/main/java/dagger/hilt/android/simple/SimpleApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2020 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 simple, skeletal application that demonstrates a dependency-injected application using the | ||
* utilities in {@code Hilt} in Android. | ||
*/ | ||
@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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters