AndroidXHiltProcessor no longer extends BasicAnnotationProcessor.

BasicAnnotationProcessor forces annotations in runtime classpath be
mixed with compile classpath and this is an issue if the annotations
are in an Android packaged library (aar), see b/153170868.

Since Hilt is now being correctly built in an AAR we can no longer
depend on it on our java-only common artifact, therefore make that
library an android library module and refactor the compiler to not
depend on it.

Test: ./gradlew hilt:hilt-compiler:test
Change-Id: I66aa2c70deda3297b43807d60d713436dd3f57d9
diff --git a/hilt/hilt-common/build.gradle b/hilt/hilt-common/build.gradle
index 1ea21ae..ef394c5 100644
--- a/hilt/hilt-common/build.gradle
+++ b/hilt/hilt-common/build.gradle
@@ -23,13 +23,25 @@
 
 plugins {
     id("AndroidXPlugin")
-    id("java-library")
+    id("com.android.library")
 }
 
 dependencies {
     implementation(HILT_ANDROID)
 }
 
+android.libraryVariants.all { variant ->
+    def name = variant.name
+    def suffix = name.capitalize()
+
+    // Create jar<variant> task for testImplementation in hilt-compiler.
+    project.tasks.register("jar${suffix}", Jar).configure {
+        dependsOn variant.javaCompileProvider
+        from variant.javaCompileProvider.map { task -> task.destinationDir}
+        destinationDir new File(project.buildDir, "libJar")
+    }
+}
+
 androidx {
     name = "AndroidX Hilt Extension Annotations"
     publish = Publish.SNAPSHOT_AND_RELEASE
diff --git a/hilt/hilt-common/src/main/AndroidManifest.xml b/hilt/hilt-common/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..800b74e
--- /dev/null
+++ b/hilt/hilt-common/src/main/AndroidManifest.xml
@@ -0,0 +1,18 @@
+<!--
+  Copyright 2020 The Android Open Source Project
+
+  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 package="androidx.hilt">
+</manifest>
diff --git a/hilt/hilt-compiler/build.gradle b/hilt/hilt-compiler/build.gradle
index d7a2086..4d87625 100644
--- a/hilt/hilt-compiler/build.gradle
+++ b/hilt/hilt-compiler/build.gradle
@@ -32,8 +32,6 @@
 }
 
 dependencies {
-    implementation(project(":hilt:hilt-common"))
-
     implementation(KOTLIN_STDLIB)
     compileOnly(AUTO_SERVICE_ANNOTATIONS)
     kapt(AUTO_SERVICE_PROCESSOR)
@@ -47,18 +45,40 @@
     testImplementation(GOOGLE_COMPILE_TESTING)
     testImplementation(HILT_ANDROID)
     testImplementation fileTree(
+            dir: "${new File(project(":hilt:hilt-common").buildDir, "libJar")}",
+            include : "*.jar")
+    testImplementation fileTree(
             dir: "${new File(project(":hilt:hilt-lifecycle-viewmodel").buildDir, "libJar")}",
             include : "*.jar")
     testImplementation fileTree(
             dir: "${new File(project(":hilt:hilt-work").buildDir, "libJar")}",
             include : "*.jar")
     testImplementation fileTree(
+            dir: "${new File(project.buildDir, "extractedJar")}",
+            include : "*.jar")
+    testImplementation fileTree(
             dir: "${SdkHelperKt.getSdkPath(project)}/platforms/$SupportConfig.COMPILE_SDK_VERSION/",
             include : "android.jar")
 }
 
-tasks.findByName("compileKotlin").dependsOn(":hilt:hilt-lifecycle-viewmodel:jarDebug")
-tasks.findByName("compileKotlin").dependsOn(":hilt:hilt-work:jarDebug")
+// Extract Hilt classes.jar to be used for compile-testing.
+tasks.register("extractHiltJar", Exec).configure {
+    def aarPath = project.configurations.testRuntimeClasspath.find {
+        it.path.contains("com/google/dagger/hilt-android/") && it.name.endsWith(".aar")
+    }
+    inputs.file(aarPath)
+    outputs.dir(new File(project.buildDir, "extractedJar"))
+    workingDir(project.buildDir)
+    executable("unzip")
+    args("-o", "$aarPath", "classes.jar", "-d", "extractedJar")
+}
+
+tasks.named("compileKotlin").configure {
+    dependsOn(":hilt:hilt-common:jarDebug")
+    dependsOn(":hilt:hilt-lifecycle-viewmodel:jarDebug")
+    dependsOn(":hilt:hilt-work:jarDebug")
+    dependsOn(":hilt:hilt-compiler:extractHiltJar")
+}
 
 androidx {
     name = "AndroidX Hilt Extension Compiler"
diff --git a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/AndroidXHiltProcessor.kt b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/AndroidXHiltProcessor.kt
index 9d989cb..fb305895 100644
--- a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/AndroidXHiltProcessor.kt
+++ b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/AndroidXHiltProcessor.kt
@@ -18,23 +18,49 @@
 
 import androidx.hilt.lifecycle.ViewModelInjectStep
 import androidx.hilt.work.WorkerInjectStep
-import com.google.auto.common.BasicAnnotationProcessor
 import com.google.auto.service.AutoService
 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor
 import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING
+import javax.annotation.processing.AbstractProcessor
 import javax.annotation.processing.Processor
+import javax.annotation.processing.RoundEnvironment
 import javax.lang.model.SourceVersion
+import javax.lang.model.element.Element
+import javax.lang.model.element.TypeElement
 
 /**
  * Annotation processor for the various AndroidX Hilt extensions.
  */
 @AutoService(Processor::class)
 @IncrementalAnnotationProcessor(ISOLATING)
-class AndroidXHiltProcessor : BasicAnnotationProcessor() {
-    override fun initSteps() = listOf(
+class AndroidXHiltProcessor : AbstractProcessor() {
+
+    override fun getSupportedAnnotationTypes() = setOf(
+        ClassNames.VIEW_MODEL_INJECT.canonicalName(),
+        ClassNames.WORKER_INJECT.canonicalName()
+    )
+
+    override fun getSupportedSourceVersion() = SourceVersion.latest()
+
+    override fun process(
+        annotations: MutableSet<out TypeElement>,
+        roundEnv: RoundEnvironment
+    ): Boolean {
+        getSteps().forEach { step ->
+            annotations.firstOrNull { it.qualifiedName.contentEquals(step.annotation()) }?.let {
+                step.process(roundEnv.getElementsAnnotatedWith(it))
+            }
+        }
+        return false
+    }
+
+    private fun getSteps() = listOf(
         ViewModelInjectStep(processingEnv),
         WorkerInjectStep(processingEnv)
     )
 
-    override fun getSupportedSourceVersion() = SourceVersion.latest()
+    interface Step {
+        fun annotation(): String
+        fun process(annotatedElements: Set<Element>)
+    }
 }
diff --git a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/ClassNames.kt b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/ClassNames.kt
index e3f089d..d957cfd 100644
--- a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/ClassNames.kt
+++ b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/ClassNames.kt
@@ -23,6 +23,7 @@
         ClassName.get("dagger.hilt.android.components", "ActivityRetainedComponent")
     val APPLICATION_COMPONENT =
         ClassName.get("dagger.hilt.android.components", "ApplicationComponent")
+    val ASSISTED = ClassName.get("androidx.hilt", "Assisted")
     val BINDS = ClassName.get("dagger", "Binds")
     val CONTEXT = ClassName.get("android.content", "Context")
     val NON_NULL = ClassName.get("androidx.annotation", "NonNull")
@@ -34,9 +35,11 @@
     val VIEW_MODEL_ASSISTED_FACTORY =
         ClassName.get("androidx.hilt.lifecycle", "ViewModelAssistedFactory")
     val VIEW_MODEL = ClassName.get("androidx.lifecycle", "ViewModel")
+    val VIEW_MODEL_INJECT = ClassName.get("androidx.hilt.lifecycle", "ViewModelInject")
     val SAVED_STATE_HANDLE = ClassName.get("androidx.lifecycle", "SavedStateHandle")
     val STRING_KEY = ClassName.get("dagger.multibindings", "StringKey")
     val WORKER = ClassName.get("androidx.work", "Worker")
     val WORKER_ASSISTED_FACTORY = ClassName.get("androidx.hilt.work", "WorkerAssistedFactory")
+    val WORKER_INJECT = ClassName.get("androidx.hilt.work", "WorkerInject")
     val WORKER_PARAMETERS = ClassName.get("androidx.work", "WorkerParameters")
 }
diff --git a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/assisted/DependencyRequest.kt b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/assisted/DependencyRequest.kt
index 1e76c56..eb4d5e6 100644
--- a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/assisted/DependencyRequest.kt
+++ b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/assisted/DependencyRequest.kt
@@ -16,7 +16,6 @@
 
 package androidx.hilt.assisted
 
-import androidx.hilt.Assisted
 import androidx.hilt.ClassNames
 import androidx.hilt.ext.hasAnnotation
 import com.squareup.javapoet.AnnotationSpec
@@ -60,7 +59,7 @@
     return DependencyRequest(
         name = simpleName.toString(),
         type = type,
-        isAssisted = hasAnnotation(Assisted::class) && qualifier == null,
+        isAssisted = hasAnnotation(ClassNames.ASSISTED.canonicalName()) && qualifier == null,
         qualifier = qualifier
     )
 }
\ No newline at end of file
diff --git a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/lifecycle/ViewModelInjectStep.kt b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/lifecycle/ViewModelInjectStep.kt
index 8c1ccc5..ec7bf75 100644
--- a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/lifecycle/ViewModelInjectStep.kt
+++ b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/lifecycle/ViewModelInjectStep.kt
@@ -16,12 +16,10 @@
 
 package androidx.hilt.lifecycle
 
-import androidx.hilt.Assisted
+import androidx.hilt.AndroidXHiltProcessor
 import androidx.hilt.ClassNames
 import androidx.hilt.ext.hasAnnotation
-import com.google.auto.common.BasicAnnotationProcessor
 import com.google.auto.common.MoreElements
-import com.google.common.collect.SetMultimap
 import com.squareup.javapoet.TypeName
 import javax.annotation.processing.ProcessingEnvironment
 import javax.lang.model.element.Element
@@ -37,19 +35,17 @@
  */
 class ViewModelInjectStep(
     private val processingEnv: ProcessingEnvironment
-) : BasicAnnotationProcessor.ProcessingStep {
+) : AndroidXHiltProcessor.Step {
 
     private val elements = processingEnv.elementUtils
     private val types = processingEnv.typeUtils
     private val messager = processingEnv.messager
 
-    override fun annotations() = setOf(ViewModelInject::class.java)
+    override fun annotation() = ClassNames.VIEW_MODEL_INJECT.canonicalName()
 
-    override fun process(
-        elementsByAnnotation: SetMultimap<Class<out Annotation>, Element>
-    ): MutableSet<out Element> {
+    override fun process(annotatedElements: Set<Element>) {
         val parsedElements = mutableSetOf<TypeElement>()
-        elementsByAnnotation[ViewModelInject::class.java].forEach { element ->
+        annotatedElements.forEach { element ->
             val constructorElement = MoreElements.asExecutable(element)
             val typeElement = MoreElements.asType(constructorElement.enclosingElement)
             if (parsedElements.add(typeElement)) {
@@ -61,7 +57,6 @@
                 }
             }
         }
-        return mutableSetOf()
     }
 
     private fun parse(
@@ -84,7 +79,7 @@
         }
 
         ElementFilter.constructorsIn(typeElement.enclosedElements).filter {
-            it.hasAnnotation(ViewModelInject::class)
+            it.hasAnnotation(ClassNames.VIEW_MODEL_INJECT.canonicalName())
         }.let { constructors ->
             if (constructors.size > 1) {
                 error("Multiple @ViewModelInject annotated constructors found.", typeElement)
@@ -113,7 +108,7 @@
                 valid = false
             }
             firstOrNull()?.let {
-                if (!it.hasAnnotation(Assisted::class)) {
+                if (!it.hasAnnotation(ClassNames.ASSISTED.canonicalName())) {
                     error("Missing @Assisted annotation in param '${it.simpleName}'.", it)
                     valid = false
                 }
diff --git a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/work/WorkerInjectStep.kt b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/work/WorkerInjectStep.kt
index 752edf6..c87a492 100644
--- a/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/work/WorkerInjectStep.kt
+++ b/hilt/hilt-compiler/src/main/kotlin/androidx/hilt/work/WorkerInjectStep.kt
@@ -16,12 +16,10 @@
 
 package androidx.hilt.work
 
-import androidx.hilt.Assisted
+import androidx.hilt.AndroidXHiltProcessor
 import androidx.hilt.ClassNames
 import androidx.hilt.ext.hasAnnotation
-import com.google.auto.common.BasicAnnotationProcessor
 import com.google.auto.common.MoreElements
-import com.google.common.collect.SetMultimap
 import com.squareup.javapoet.TypeName
 import javax.annotation.processing.ProcessingEnvironment
 import javax.lang.model.element.Element
@@ -37,19 +35,17 @@
  */
 class WorkerInjectStep(
     private val processingEnv: ProcessingEnvironment
-) : BasicAnnotationProcessor.ProcessingStep {
+) : AndroidXHiltProcessor.Step {
 
     private val elements = processingEnv.elementUtils
     private val types = processingEnv.typeUtils
     private val messager = processingEnv.messager
 
-    override fun annotations() = setOf(WorkerInject::class.java)
+    override fun annotation() = ClassNames.WORKER_INJECT.canonicalName()
 
-    override fun process(
-        elementsByAnnotation: SetMultimap<Class<out Annotation>, Element>
-    ): MutableSet<out Element> {
+    override fun process(annotatedElements: Set<Element>) {
         val parsedElements = mutableSetOf<TypeElement>()
-        elementsByAnnotation[WorkerInject::class.java].forEach { element ->
+        annotatedElements.forEach { element ->
             val constructorElement =
                 MoreElements.asExecutable(element)
             val typeElement =
@@ -63,7 +59,6 @@
                 }
             }
         }
-        return mutableSetOf()
     }
 
     private fun parse(
@@ -86,7 +81,7 @@
         }
 
         ElementFilter.constructorsIn(typeElement.enclosedElements).filter {
-            it.hasAnnotation(WorkerInject::class)
+            it.hasAnnotation(ClassNames.WORKER_INJECT.canonicalName())
         }.let { constructors ->
             if (constructors.size > 1) {
                 error("Multiple @WorkerInject annotated constructors found.", typeElement)
@@ -114,7 +109,7 @@
                 valid = false
             }
             firstOrNull()?.let {
-                if (!it.hasAnnotation(Assisted::class)) {
+                if (!it.hasAnnotation(ClassNames.ASSISTED.canonicalName())) {
                     error("Missing @Assisted annotation in param '${it.simpleName}'.", it)
                     valid = false
                 }
@@ -130,7 +125,7 @@
                 valid = false
             }
             firstOrNull()?.let {
-                if (!it.hasAnnotation(Assisted::class)) {
+                if (!it.hasAnnotation(ClassNames.ASSISTED.canonicalName())) {
                     error("Missing @Assisted annotation in param '${it.simpleName}'.", it)
                     valid = false
                 }
diff --git a/hilt/hilt-lifecycle-viewmodel/build.gradle b/hilt/hilt-lifecycle-viewmodel/build.gradle
index 30c7cff..f38904b 100644
--- a/hilt/hilt-lifecycle-viewmodel/build.gradle
+++ b/hilt/hilt-lifecycle-viewmodel/build.gradle
@@ -37,9 +37,7 @@
     api(project(":hilt:hilt-common"))
     api("androidx.lifecycle:lifecycle-viewmodel-savedstate:2.2.0")
     implementation("androidx.activity:activity:1.1.0")
-    implementation(DAGGER)
-    annotationProcessor(DAGGER_COMPILER)
-    implementation(HILT_ANDROID)
+    api(HILT_ANDROID)
     annotationProcessor(HILT_ANDROID_COMPILER)
 }
 
@@ -47,10 +45,10 @@
     def name = variant.name
     def suffix = name.capitalize()
 
-    // Create jar<variant> task for testImplementation in hilt--compiler.
-    project.tasks.create(name: "jar${suffix}", type: Jar){
-        dependsOn variant.javaCompileProvider.get()
-        from variant.javaCompileProvider.get().destinationDir
+    // Create jar<variant> task for testImplementation in hilt-compiler.
+    project.tasks.register("jar${suffix}", Jar).configure {
+        dependsOn variant.javaCompileProvider
+        from variant.javaCompileProvider.map { task -> task.destinationDir}
         destinationDir new File(project.buildDir, "libJar")
     }
 }
diff --git a/hilt/hilt-work/build.gradle b/hilt/hilt-work/build.gradle
index b46d6e8..025ce0a 100644
--- a/hilt/hilt-work/build.gradle
+++ b/hilt/hilt-work/build.gradle
@@ -36,9 +36,7 @@
     api("androidx.annotation:annotation:1.1.0")
     api(project(":hilt:hilt-common"))
     api("androidx.work:work-runtime:2.3.4")
-    implementation(DAGGER)
-    annotationProcessor(DAGGER_COMPILER)
-    implementation(HILT_ANDROID)
+    api(HILT_ANDROID)
     annotationProcessor(HILT_ANDROID_COMPILER)
 }
 
@@ -46,10 +44,10 @@
     def name = variant.name
     def suffix = name.capitalize()
 
-    // Create jar<variant> task for testImplementation in hilt--compiler.
-    project.tasks.create(name: "jar${suffix}", type: Jar){
-        dependsOn variant.javaCompileProvider.get()
-        from variant.javaCompileProvider.get().destinationDir
+    // Create jar<variant> task for testImplementation in hilt-compiler.
+    project.tasks.register("jar${suffix}", Jar).configure {
+        dependsOn variant.javaCompileProvider
+        from variant.javaCompileProvider.map { task -> task.destinationDir}
         destinationDir new File(project.buildDir, "libJar")
     }
 }
diff --git a/hilt/integration-tests/viewmodelapp/build.gradle b/hilt/integration-tests/viewmodelapp/build.gradle
index 6036a69..4fac43a 100644
--- a/hilt/integration-tests/viewmodelapp/build.gradle
+++ b/hilt/integration-tests/viewmodelapp/build.gradle
@@ -39,8 +39,6 @@
     implementation(project(":hilt:hilt-lifecycle-viewmodel"))
     kapt(project(":hilt:hilt-compiler"))
     implementation(KOTLIN_STDLIB)
-    implementation(DAGGER)
-    kapt(DAGGER_COMPILER)
     implementation(HILT_ANDROID)
     kapt(HILT_ANDROID_COMPILER)
 }
\ No newline at end of file
diff --git a/hilt/integration-tests/workerapp/build.gradle b/hilt/integration-tests/workerapp/build.gradle
index c423bb1..78eacbf 100644
--- a/hilt/integration-tests/workerapp/build.gradle
+++ b/hilt/integration-tests/workerapp/build.gradle
@@ -39,8 +39,6 @@
     implementation(project(":hilt:hilt-work"))
     kapt(project(":hilt:hilt-compiler"))
     implementation(KOTLIN_STDLIB)
-    implementation(DAGGER)
-    kapt(DAGGER_COMPILER)
     implementation(HILT_ANDROID)
     kapt(HILT_ANDROID_COMPILER)
 }
\ No newline at end of file