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.
Add gradle test for Dagger Android Ksp
RELNOTES=n/a PiperOrigin-RevId: 584175632
- Loading branch information
1 parent
75a3a0a
commit 95f2afb
Showing
22 changed files
with
875 additions
and
16 deletions.
There are no files selected for viewing
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
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
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,74 @@ | ||
/* | ||
* Copyright (C) 2023 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. | ||
*/ | ||
|
||
plugins { | ||
id 'com.android.application' | ||
id 'org.jetbrains.kotlin.android' | ||
id 'com.google.devtools.ksp' | ||
} | ||
|
||
android { | ||
namespace 'dagger.android.ksp' | ||
compileSdkVersion 33 | ||
defaultConfig { | ||
applicationId 'dagger.android.ksp' | ||
minSdk 21 | ||
targetSdk 33 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_17 | ||
targetCompatibility JavaVersion.VERSION_17 | ||
} | ||
testOptions { | ||
unitTests.includeAndroidResources = true | ||
} | ||
sourceSets { | ||
String sharedTestDir = 'src/sharedTest/java' | ||
test { | ||
java.srcDirs += sharedTestDir | ||
} | ||
androidTest { | ||
java.srcDirs += sharedTestDir | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
|
||
testImplementation 'com.google.truth:truth:1.0.1' | ||
testImplementation 'org.robolectric:robolectric:4.11.1' | ||
testImplementation 'androidx.core:core:1.3.2' | ||
testImplementation 'androidx.test.ext:junit:1.1.5' | ||
testImplementation 'androidx.test:runner:1.5.2' | ||
testImplementation 'androidx.test.espresso:espresso-core:3.5.1' | ||
|
||
androidTestImplementation 'com.google.truth:truth:1.0.1' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.5' | ||
androidTestImplementation 'androidx.test:runner:1.5.2' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' | ||
|
||
// Dagger Android dependencies | ||
implementation 'com.google.dagger:dagger:LOCAL-SNAPSHOT' | ||
implementation 'com.google.dagger:dagger-android-support:LOCAL-SNAPSHOT' | ||
implementation 'com.google.dagger:dagger-android:LOCAL-SNAPSHOT' | ||
ksp 'com.google.dagger:dagger-android-processor:LOCAL-SNAPSHOT' | ||
ksp "com.google.dagger:dagger-compiler:LOCAL-SNAPSHOT" | ||
} |
30 changes: 30 additions & 0 deletions
30
javatests/artifacts/dagger-android-ksp/app/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,30 @@ | ||
<!-- | ||
~ Copyright (C) 2023 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.android.ksp"> | ||
|
||
<application | ||
android:name=".SimpleApplication" | ||
android:label="@string/appName" | ||
android:theme="@style/Theme.AppCompat.Light"> | ||
<activity android:name=".SimpleActivity" android:exported="true"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
</manifest> |
22 changes: 22 additions & 0 deletions
22
javatests/artifacts/dagger-android-ksp/app/src/main/kotlin/dagger/android/ksp/Model .kt
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,22 @@ | ||
/* | ||
* Copyright (C) 2023 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.android.ksp | ||
|
||
import javax.inject.Qualifier | ||
|
||
/** Qualifies bindings relating to [android.os.Build.MODEL]. */ | ||
@Qualifier @Retention(AnnotationRetention.RUNTIME) internal annotation class Model |
26 changes: 26 additions & 0 deletions
26
javatests/artifacts/dagger-android-ksp/app/src/main/kotlin/dagger/android/ksp/ModelModule.kt
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,26 @@ | ||
/* | ||
* Copyright (C) 2023 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.android.ksp | ||
|
||
import android.os.Build.MODEL | ||
import dagger.Module | ||
import dagger.Provides | ||
|
||
@Module | ||
object ModelModule { | ||
@Provides @Model fun provideModel(): String = MODEL | ||
} |
68 changes: 68 additions & 0 deletions
68
...sts/artifacts/dagger-android-ksp/app/src/main/kotlin/dagger/android/ksp/SimpleActivity.kt
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,68 @@ | ||
/* | ||
* Copyright (C) 2023 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.android.ksp | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.TextView | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.Subcomponent | ||
import dagger.android.AndroidInjector | ||
import dagger.android.support.DaggerAppCompatActivity | ||
import dagger.multibindings.ClassKey | ||
import dagger.multibindings.IntoMap | ||
import javax.inject.Inject | ||
|
||
/** | ||
* The main activity of the application. | ||
* | ||
* <p>It can be injected with any binding from both {@link SimpleActivityComponent} and {@link | ||
* SimpleApplication.SimpleComponent}. | ||
*/ | ||
class SimpleActivity : DaggerAppCompatActivity() { | ||
private val TAG: String = SimpleActivity::class.java.getSimpleName() | ||
|
||
@Inject @UserName lateinit var userName: String | ||
@Inject @Model lateinit var model: String | ||
|
||
override protected fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
Log.i(TAG, "Injected with userName and model: " + userName + ", " + model) | ||
|
||
setContentView(R.layout.activity_main) | ||
|
||
val greeting = findViewById(R.id.greeting) as TextView | ||
val text = getResources().getString(R.string.welcome, userName, model) | ||
greeting.setText(text) | ||
} | ||
} | ||
|
||
@Subcomponent | ||
interface SimpleActivityComponent : AndroidInjector<SimpleActivity> { | ||
|
||
@Subcomponent.Factory interface Factory : AndroidInjector.Factory<SimpleActivity> {} | ||
} | ||
|
||
@Module(subcomponents = [SimpleActivityComponent::class], includes = [UserNameModule::class]) | ||
interface InjectorModule { | ||
@Binds | ||
@IntoMap | ||
@ClassKey(SimpleActivity::class) | ||
fun bind(factory: SimpleActivityComponent.Factory): AndroidInjector.Factory<*> | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
.../artifacts/dagger-android-ksp/app/src/main/kotlin/dagger/android/ksp/SimpleApplication.kt
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,53 @@ | ||
/* | ||
* Copyright (C) 2023 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.android.ksp | ||
|
||
import android.util.Log | ||
import dagger.Component | ||
import dagger.android.AndroidInjectionModule | ||
import dagger.android.AndroidInjector | ||
import dagger.android.DaggerApplication | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
/** | ||
* A simple, skeletal application that demonstrates a dependency-injected application using the | ||
* utilities in {@code dagger.android}. | ||
*/ | ||
class SimpleApplication : DaggerApplication() { | ||
private val TAG = SimpleApplication::class.java.getSimpleName() | ||
|
||
@Inject @Model lateinit var model: String | ||
|
||
override public fun onCreate() { | ||
super.onCreate() | ||
Log.i(TAG, "Injected with model: " + model) | ||
} | ||
|
||
override protected fun applicationInjector(): AndroidInjector<SimpleApplication> { | ||
return DaggerSimpleComponent.factory().create(this) | ||
} | ||
} | ||
|
||
@Singleton | ||
@Component( | ||
modules = | ||
[AndroidInjectionModule::class, InjectorModule::class, ModelModule::class] | ||
) | ||
interface SimpleComponent : AndroidInjector<SimpleApplication> { | ||
@Component.Factory interface Factory : AndroidInjector.Factory<SimpleApplication> {} | ||
} |
22 changes: 22 additions & 0 deletions
22
javatests/artifacts/dagger-android-ksp/app/src/main/kotlin/dagger/android/ksp/UserName.kt
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,22 @@ | ||
/* | ||
* Copyright (C) 2023 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.android.ksp | ||
|
||
import javax.inject.Qualifier | ||
|
||
/** Qualifies bindings relating to the user name. */ | ||
@Qualifier @Retention(AnnotationRetention.RUNTIME) internal annotation class UserName |
25 changes: 25 additions & 0 deletions
25
...sts/artifacts/dagger-android-ksp/app/src/main/kotlin/dagger/android/ksp/UserNameModule.kt
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,25 @@ | ||
/* | ||
* Copyright (C) 2023 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.android.ksp | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
|
||
@Module | ||
object UserNameModule { | ||
@UserName @Provides fun provideUserName(): String = "ProdUser" | ||
} |
Oops, something went wrong.