Repository: autonomousapps/dependency-analysis-android-gradle-plugin
Issue: ComputeUsagesTask fails with "items must not be empty"

**Plugin version**
1.13.1

**Gradle version**
7.4.2

**(Optional) Android Gradle Plugin (AGP) version**
n.a.

**Describe the bug**
After moving to the latest plugin version (1.13.1) analysis fails with the following error;

```
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.autonomousapps.tasks.ComputeUsagesTask$ComputeUsagesAction	
at org.gradle.workers.internal.DefaultWorkerExecutor$WorkItemExecution.waitForCompletion(DefaultWorkerExecutor.java:339)	
...
Caused by: java.lang.IllegalStateException: items must not be empty	
at com.autonomousapps.model.intermediates.ReasonKt.buildReason(Reason.kt:233)	
at com.autonomousapps.model.intermediates.ReasonKt.access$buildReason(Reason.kt:1)	
at com.autonomousapps.model.intermediates.Reason$ServiceLoader.<init>(Reason.kt:205)	
at com.autonomousapps.tasks.GraphVisitor.visit(ComputeUsagesTask.kt:172)	
at com.autonomousapps.visitor.GraphViewReader.accept(GraphViewReader.kt:18)	
at com.autonomousapps.tasks.ComputeUsagesTask$ComputeUsagesAction.execute(ComputeUsagesTask.kt:92)	
at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)	
at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:66)	
at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:62)	
at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:97)	
at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:62)	
at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44)	
at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41)	
at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)	
at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)	
at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)	
at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)	
at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)	
at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)	
at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)	
at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)	
at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)	
at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:59)	
at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:205)	
at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:187)	
at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.access$700(DefaultConditionalExecutionQueue.java:120)	
at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner$1.run(DefaultConditionalExecutionQueue.java:162)	
at org.gradle.internal.Factories$1.create(Factories.java:31)
```

**To Reproduce**
Steps to reproduce the behavior:
1. Use plugin version > 1.8.0
2. Execute task "projectHealth" for any sub-project in the build
3. Analysis fails in 'ComputeUsagesTask'

**Expected behavior**
analysis should not fail

**Additional context**
Dependency Analysis Plugin is used in a large multi-module Gradle build. The error only occurs with any plugin version > 1.8.0.


Changed files:
src/main/kotlin/com/autonomousapps/subplugin/ProjectPlugin.kt, src/main/kotlin/com/autonomousapps/tasks/FindServiceLoadersTask.kt

diff --git a/src/main/kotlin/com/autonomousapps/subplugin/ProjectPlugin.kt b/src/main/kotlin/com/autonomousapps/subplugin/ProjectPlugin.kt
index 9234e82b..2dba7193 100644
--- a/src/main/kotlin/com/autonomousapps/subplugin/ProjectPlugin.kt
+++ b/src/main/kotlin/com/autonomousapps/subplugin/ProjectPlugin.kt
@@ -583,8 +583,7 @@ internal class ProjectPlugin(private val project: Project) {
     // A report of service loaders.
     val findServiceLoadersTask = tasks.register<FindServiceLoadersTask>("serviceLoader$taskNameSuffix") {
       setCompileClasspath(
-        configurations[dependencyAnalyzer.compileConfigurationName]
-          .artifactsFor(dependencyAnalyzer.attributeValueJar)
+        configurations[dependencyAnalyzer.compileConfigurationName].artifactsFor(dependencyAnalyzer.attributeValueJar)
       )
       output.set(outputPaths.serviceLoaderDependenciesPath)
     }
diff --git a/src/main/kotlin/com/autonomousapps/tasks/FindServiceLoadersTask.kt b/src/main/kotlin/com/autonomousapps/tasks/FindServiceLoadersTask.kt
index ac8ff27b..910015a9 100644
--- a/src/main/kotlin/com/autonomousapps/tasks/FindServiceLoadersTask.kt
+++ b/src/main/kotlin/com/autonomousapps/tasks/FindServiceLoadersTask.kt
@@ -3,14 +3,21 @@ package com.autonomousapps.tasks
 import com.autonomousapps.TASK_GROUP_DEP_INTERNAL
 import com.autonomousapps.internal.ANNOTATION_PROCESSOR_PATH
 import com.autonomousapps.internal.SERVICE_LOADER_PATH
-import com.autonomousapps.internal.utils.*
+import com.autonomousapps.internal.utils.filterNonGradle
+import com.autonomousapps.internal.utils.filterToSet
+import com.autonomousapps.internal.utils.flatMapToSet
+import com.autonomousapps.internal.utils.getAndDelete
+import com.autonomousapps.internal.utils.toJson
 import com.autonomousapps.model.intermediates.ServiceLoaderDependency
 import org.gradle.api.DefaultTask
 import org.gradle.api.artifacts.ArtifactCollection
 import org.gradle.api.artifacts.result.ResolvedArtifactResult
 import org.gradle.api.file.FileCollection
 import org.gradle.api.file.RegularFileProperty
-import org.gradle.api.tasks.*
+import org.gradle.api.tasks.CacheableTask
+import org.gradle.api.tasks.Classpath
+import org.gradle.api.tasks.OutputFile
+import org.gradle.api.tasks.TaskAction
 import java.io.BufferedReader
 import java.util.zip.ZipFile
 
@@ -57,11 +64,11 @@ abstract class FindServiceLoadersTask : DefaultTask() {
   private fun findServiceLoaders(artifact: ResolvedArtifactResult): Set<ServiceLoaderDependency> {
     val zip = ZipFile(artifact.file)
 
-    return zip.entries().toList()
+    return zip.entries().asSequence()
       .filter { it.name.startsWith(SERVICE_LOADER_PATH) }
       .filterNot { it.name.startsWith(ANNOTATION_PROCESSOR_PATH) }
       .filterNot { it.isDirectory }
-      .mapToOrderedSet { serviceFile ->
+      .mapNotNull { serviceFile ->
         val providerClasses = zip.getInputStream(serviceFile)
           .bufferedReader().use(BufferedReader::readLines)
           // remove whitespace
@@ -69,11 +76,17 @@ abstract class FindServiceLoadersTask : DefaultTask() {
           // Ignore comments
           .filterToSet { !it.startsWith("#") }
 
-        ServiceLoaderDependency(
-          providerFile = serviceFile.name.removePrefix(SERVICE_LOADER_PATH),
-          providerClasses = providerClasses,
-          artifact = artifact
-        )
-      }
+        // Unclear why this would ever be empty.
+        // See https://github.com/autonomousapps/dependency-analysis-android-gradle-plugin/issues/780
+        if (providerClasses.isNotEmpty()) {
+          ServiceLoaderDependency(
+            providerFile = serviceFile.name.removePrefix(SERVICE_LOADER_PATH),
+            providerClasses = providerClasses,
+            artifact = artifact
+          )
+        } else {
+          null
+        }
+      }.toSortedSet()
   }
 }