Provide third party class instance using Hilt

267 Views Asked by At

I'm using Googles mlkit to scan barcodes. I've included some of their sample files. I would like to inject BarcodeScannerProcessor below using Hilt, rather than creating an inline instance.

I've tried flagging it as @ActivityScoped and injecting @ActivityContext context: Context in its constructor, which as far as I know should be eqvivalent to the inline instance below. However, when I do that and supply it to the activity below, I just get a black scanner window. How can I provide BarcodeScannerProcessor using Hilt?

class ScannerActivity : AppCompatActivity(){
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_vision_live_preview)
    
    // some rows omitted for brevity
    val processor = BarcodeScannerProcessor(this)
    cameraSource!!.setMachineLearningFrameProcessor(processor)
  }
}

My attempt to modify the BarcodeScannerProcessor:

@ActivityScoped
class BarcodeScannerProcessor @Inject constructor(@ActivityContext context: Context) 
                                         : VisionProcessorBase<List<Barcode>>(context) {
//....

Injected into the ScannerActivity using:

@Inject lateinit var barcodeScannerProcessor: BarcodeScannerProcessor
0

There are 0 best solutions below