So I followed the CameraX documentation. It works on newly created projects. Fresh projects. However, when you try to integrate it onto old Android Studio projects, the same code will crush mysteriously.
The preview, cameraSelector(back) are set as per the document. But when it gets to the bindToLifeCycle(this, cameraSelector, preview) function, it throw an AbstractMethodError
It looks like it has something to do with build.gradle or settings.gradle. I can't figure it out. Maybe one of you can. Anyway, here is the startCamera code
`private fun startCamera() { val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
// Used to bind the lifecycle of cameras to the lifecycle owner
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
// Preview
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(viewBinding.viewFinder.surfaceProvider)
}
// Select back camera as a default
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
// Unbind use cases before rebinding
cameraProvider.unbindAll()
**// Bind use cases to camera where the error occurs(AbstractMethodError)**
cameraProvider
.bindToLifecycle(this, cameraSelector, preview)
} catch(exc: Exception) {
Toast.makeText(applicationContext, exc.message, Toast.LENGTH_LONG).show()
Log.e(TAG, "Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(this))
}`
And here is the logcat/error message itself:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ynot, PID: 3872 java.lang.AbstractMethodError: abstract method "androidx.camera.core.impl.Config androidx.camera.core.impl.UseCaseConfigFactory.getConfig(androidx.camera.core.impl.UseCaseConfigFactory$CaptureType, int)" at androidx.camera.core.Preview.getDefaultConfig(Preview.java:472) at androidx.camera.core.internal.CameraUseCaseAdapter.getConfigs(CameraUseCaseAdapter.java:483) at androidx.camera.core.internal.CameraUseCaseAdapter.addUseCases(CameraUseCaseAdapter.java:208) at androidx.camera.lifecycle.LifecycleCamera.bind(LifecycleCamera.java:206) at androidx.camera.lifecycle.LifecycleCameraRepository.bindToLifecycleCamera(LifecycleCameraRepository.java:278) at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:532) at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:360) at com.example.ynote.Tests.CamTest.startCamera$lambda-2(CamTest.kt:113) at com.example.ynote.Tests.CamTest.$r8$lambda$5ypGB5bKZPoW5lxjdhsS7beQdvA(Unknown Source:0) at com.example.ynote.Tests.CamTest$$ExternalSyntheticLambda0.run(Unknown Source:4) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:264) at android.app.ActivityThread.main(ActivityThread.java:7581) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)