Skip to content

Commit

Permalink
Add gradle test for Dagger Android Ksp
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 584175632
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Nov 21, 2023
1 parent 75a3a0a commit 95f2afb
Show file tree
Hide file tree
Showing 22 changed files with 875 additions and 16 deletions.
16 changes: 14 additions & 2 deletions .github/actions/artifact-android-local-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ inputs:
agp:
description: 'The version of AGP to test with.'
required: true
type: choice
options:
- '7.0.0'
- '7.1.2'
- '8.1.0'
jdk:
description: 'The version of JDK to test with.'
required: true
type: choice
options:
- '11'
- '17'

runs:
using: "composite"
Expand All @@ -25,11 +37,11 @@ runs:
with:
name: local-snapshot
path: ~/.m2/repository/com/google/dagger
- name: 'Install Java ${{ env.USE_JAVA_VERSION }}'
- name: 'Install Java ${{ inputs.jdk }}'
uses: actions/setup-java@v3
with:
distribution: '${{ env.USE_JAVA_DISTRIBUTION }}'
java-version: '${{ env.USE_JAVA_VERSION }}'
java-version: '${{ inputs.jdk }}'
- name: 'Gradle Android local tests (AGP ${{ inputs.agp }})'
run: ./util/run-local-gradle-android-tests.sh "${{ inputs.agp }}"
shell: bash
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,19 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
agp: ['7.0.0', '7.1.2']
include:
- agp: '7.0.0'
jdk: '11'
- agp: '7.1.2'
jdk: '11'
- agp: '8.1.0'
jdk: '17'
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/artifact-android-local-tests
with:
agp: '${{ matrix.agp }}'
jdk: '${{ matrix.jdk }}'
artifact-android-emulator-tests:
name: 'Artifact Android emulator tests (API 30)'
needs: bazel-build
Expand Down
74 changes: 74 additions & 0 deletions javatests/artifacts/dagger-android-ksp/app/build.gradle
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"
}
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>
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
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
}
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<*>
}

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> {}
}
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
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"
}
Loading

0 comments on commit 95f2afb

Please sign in to comment.