java.lang.BootstrapMethodError: Exception from cal

2020-08-13 05:36发布

I am trying to use CameraX API to fetch photo, but after I updated to new API version 1.0.0-alpha06 it started to crash on

CameraX.bindToLifecycle(...)

method.

The log of error is:

Process: com.aximetria.aximetria.debug, PID: 2629
    java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method
        at androidx.camera.core.Preview.updateListener(Preview.java:368)
        at androidx.camera.core.Preview.updateOutput(Preview.java:586)
        at androidx.camera.core.Preview.updateConfigAndOutput(Preview.java:363)
        at androidx.camera.core.Preview.onSuggestedResolutionUpdated(Preview.java:524)
        at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:408)
        at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:684)
        at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:195)
        at com.aximetria.aximetria.ui.main.profile.avatar.TakeAvatarPhotoFragment.initCamera(TakeAvatarPhotoFragment.kt:169)
        at com.aximetria.aximetria.ui.main.profile.avatar.TakeAvatarPhotoFragment.initCamera$default(TakeAvatarPhotoFragment.kt:117)
        at com.aximetria.aximetria.ui.main.profile.avatar.TakeAvatarPhotoFragment$onViewCreated$1.onPermissionsChecked(TakeAvatarPhotoFragment.kt:55)
    ...

My code is:

    private fun initCamera(reverseFacing: Boolean = false) {
        ...

        val previewConfig = PreviewConfig.Builder()
                .setTargetAspectRatio(AspectRatio.RATIO_4_3)
                .setLensFacing(lensFacing)
                .build()

        val preview = Preview(previewConfig)
        preview.setOnPreviewOutputUpdateListener { previewOutput ->
            val parent = binding.preview.parent as ViewGroup
            parent.removeView(binding.preview)
            parent.addView(binding.preview, 0)
            binding.preview.surfaceTexture = previewOutput.surfaceTexture
        }

        val captureConfig = ImageCaptureConfig.Builder()
                .setFlashMode(flashMode)
                .setLensFacing(lensFacing)
                .build()

        val capture = ImageCapture(captureConfig)

        binding.takeAPhoto.setOnClickListener {
            ...
            val imageFile = createTempFile(System.currentTimeMillis().toString(), ".jpg")
            capture.takePicture(imageFile, this, object : ImageCapture.OnImageSavedListener {
                override fun onImageSaved(file: File) {
                    ...
                }

                override fun onError(imageCaptureError: ImageCapture.ImageCaptureError, message: String, cause: Throwable?) {
                    ...
                }
            })
        }

        CameraX.bindToLifecycle(this, capture, preview)
    }

Is anyone know what caused this crash? I cannot see any related information in documentation (which by this time is out of date for CameraX)

4条回答
放我归山
2楼-- · 2020-08-13 05:58

Add the following lines into build.gradle(:app) file :

android{

  compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }    
}
查看更多
We Are One
3楼-- · 2020-08-13 05:59

I have fixed this issue by adding Java8 to app build.gradle

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}
查看更多
甜甜的少女心
4楼-- · 2020-08-13 06:06

Add this in gradle.

CameraX needs some methods that are part of Java 8, so we need to set our compile options accordingly. At the end of the android block, right after buildTypes, add the following:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
查看更多
【Aperson】
5楼-- · 2020-08-13 06:19

As @a-local-nobody, @stéphane-péchard and @yoonhok rightly said the bug in alpha-6 version of library. Roll back to alpha-05 also removed crashes in my case.

查看更多
登录 后发表回答