Skip to content

Commit 6316f83

Browse files
wanyingd1996Dagger Team
authored andcommitted
Add a test observing r8 behavior with HiltViewModel.
RELNOTES=n/a PiperOrigin-RevId: 577986909
1 parent ed47d4b commit 6316f83

File tree

19 files changed

+788
-0
lines changed

19 files changed

+788
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2023 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+
plugins {
18+
id 'com.android.application'
19+
id 'com.google.dagger.hilt.android'
20+
}
21+
22+
android {
23+
24+
namespace 'dagger.hilt.viewmodel'
25+
compileSdkVersion 33
26+
defaultConfig {
27+
applicationId 'dagger.hilt.viewmodel'
28+
minSdk 21
29+
targetSdk 33
30+
versionCode 1
31+
versionName "1.0"
32+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
33+
}
34+
35+
buildTypes {
36+
debug {
37+
minifyEnabled true
38+
shrinkResources true
39+
proguardFiles getDefaultProguardFile(
40+
'proguard-android-optimize.txt'),
41+
'proguard-rules.pro'
42+
43+
}
44+
}
45+
46+
compileOptions {
47+
sourceCompatibility JavaVersion.VERSION_11
48+
targetCompatibility JavaVersion.VERSION_11
49+
}
50+
}
51+
52+
dependencies {
53+
implementation 'androidx.appcompat:appcompat:1.2.0'
54+
implementation 'com.google.errorprone:error_prone_annotations:2.15.0'
55+
56+
androidTestImplementation 'androidx.test:core:1.5.0-alpha02'
57+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
58+
androidTestImplementation "androidx.test:runner:1.5.2"
59+
androidTestImplementation "androidx.test:rules:1.5.0"
60+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
61+
62+
// Hilt dependencies
63+
implementation "com.google.dagger:hilt-android:$hilt_version"
64+
annotationProcessor "com.google.dagger:hilt-compiler:$hilt_version"
65+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-dontwarn com.google.errorprone.annotations.MustBeClosed
2+
-keep class kotlin.**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2023 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.viewmodel;
18+
19+
import static androidx.test.espresso.Espresso.onView;
20+
import static androidx.test.espresso.assertion.ViewAssertions.matches;
21+
import static androidx.test.espresso.matcher.ViewMatchers.withResourceName;
22+
import static androidx.test.espresso.matcher.ViewMatchers.withText;
23+
import static org.hamcrest.Matchers.startsWith;
24+
25+
import android.content.Intent;
26+
import androidx.test.core.app.ActivityScenario;
27+
import androidx.test.core.app.ApplicationProvider;
28+
import androidx.test.ext.junit.runners.AndroidJUnit4;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
32+
@RunWith(AndroidJUnit4.class)
33+
public class SimpleApplicationTest {
34+
@Test
35+
public void testHiltViewModelWithR8DoesNotCrash() {
36+
Intent mainIntent =
37+
new Intent(ApplicationProvider.getApplicationContext(), SimpleActivity.class);
38+
try (ActivityScenario<SimpleActivity> scenario = ActivityScenario.launch(mainIntent)) {
39+
onView(withResourceName("greeting"))
40+
.check(matches(withText(startsWith("Hello"))));
41+
}
42+
}
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--
2+
~ Copyright (C) 2023 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+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
17+
18+
<application android:name=".SimpleApplication" android:label="@string/appName">
19+
<activity
20+
android:name=".SimpleActivity"
21+
android:theme="@style/Theme.AppCompat.Light"
22+
android:exported="true">
23+
<intent-filter>
24+
<action android:name="android.intent.action.MAIN" />
25+
<category android:name="android.intent.category.LAUNCHER" />
26+
</intent-filter>
27+
</activity>
28+
</application>
29+
</manifest>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2023 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.viewmodel;
18+
19+
import android.os.Bundle;
20+
import android.widget.TextView;
21+
import androidx.appcompat.app.AppCompatActivity;
22+
import androidx.lifecycle.ViewModelProvider;
23+
import dagger.hilt.android.AndroidEntryPoint;
24+
25+
/** The main activity of the application. */
26+
@AndroidEntryPoint
27+
public class SimpleActivity extends AppCompatActivity {
28+
private static final String TAG = SimpleActivity.class.getSimpleName();
29+
30+
@Override
31+
protected void onCreate(Bundle savedInstanceState) {
32+
super.onCreate(savedInstanceState);
33+
SimpleViewModel viewModel = new ViewModelProvider(this).get(SimpleViewModel.class);
34+
35+
setContentView(R.layout.activity_main);
36+
37+
((TextView) findViewById(R.id.greeting))
38+
.setText(getResources().getString(R.string.welcome, viewModel.userName));
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2023 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.viewmodel;
18+
19+
import android.app.Application;
20+
import dagger.hilt.android.HiltAndroidApp;
21+
22+
/**
23+
* A simple, skeletal application that demonstrates a dependency-injected application using the
24+
* utilities in {@code Hilt} in Android.
25+
*/
26+
@HiltAndroidApp
27+
public class SimpleApplication extends Application {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2023 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.viewmodel;
18+
19+
import androidx.lifecycle.ViewModel;
20+
import dagger.hilt.android.lifecycle.HiltViewModel;
21+
import javax.inject.Inject;
22+
23+
/** The view model requires creation by hilt. */
24+
@HiltViewModel
25+
public final class SimpleViewModel extends ViewModel {
26+
String userName;
27+
28+
@Inject
29+
SimpleViewModel(@UserName String userName) {
30+
this.userName = userName;
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2023 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.viewmodel;
18+
19+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
20+
21+
import java.lang.annotation.Documented;
22+
import java.lang.annotation.Retention;
23+
import javax.inject.Qualifier;
24+
25+
/** Qualifies bindings relating to the user name. */
26+
@Qualifier
27+
@Retention(RUNTIME)
28+
@Documented
29+
@interface UserName {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2023 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.viewmodel;
18+
19+
import dagger.Module;
20+
import dagger.Provides;
21+
import dagger.hilt.InstallIn;
22+
import dagger.hilt.android.components.ViewModelComponent;
23+
import java.util.Random;
24+
25+
@Module
26+
@InstallIn(ViewModelComponent.class)
27+
final class UserNameModule {
28+
@UserName
29+
@Provides
30+
static String provideUserName() {
31+
return "ProdUser-" + new Random().nextInt();
32+
}
33+
34+
private UserNameModule() {}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2023 The Dagger Authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:background="@android:color/background_light">
22+
23+
<TextView
24+
android:id="@+id/greeting"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:layout_alignParentTop="true"
28+
android:textColor="@android:color/primary_text_light"
29+
/>
30+
31+
<Button
32+
android:id="@+id/goto_feature"
33+
android:layout_width="match_parent"
34+
android:layout_height="wrap_content"
35+
android:layout_below="@id/greeting"
36+
android:text="@string/navigateToFeature"
37+
/>
38+
</RelativeLayout>

0 commit comments

Comments
 (0)