IllegalStateException camerax is not initialized

2,372 views
Skip to first unread message

agam goyal

unread,
Jun 2, 2020, 3:09:15 AM6/2/20
to Android CameraX Discussion Group
We are using plugin based architecture and don't have access to Application class.
One of the 2 solutions is to implement CameraXConfig.Provider in Application classThis Can't be used in my project

How to apply other solution i.e. a CameraXConfig must be explicitly provided for initialization??

Trevor McGuire

unread,
Jun 2, 2020, 1:31:28 PM6/2/20
to Android CameraX Discussion Group, agam.go...@gmail.com

Hello,

I have a few questions that may help in debugging this issue.
1. Are you including the androidx.camera:camera-camera2 module? The androidx.camera:camera-core module contains code for use cases such as Preview, ImageCapture and ImageAnalysis, but the androidx.camera:camera-camera2 module provides the implementation for interfacing with the camera.
2. If already including androidx.camera:camera-camera2, is your plugin architecture also including the resources that are packaged within the aar of this library? The resources should allow CameraX to initialize properly, but if they are somehow excluded from you app, then you may run into this exception.

It may also be helpful if you could describe in more detail how you are including CameraX as part of a plugin architecture.

Trevor

agam goyal

unread,
Jun 3, 2020, 2:02:54 AM6/3/20
to Android CameraX Discussion Group
Hello,
Thanx for your reply
1. Camera-camera2 is included
2. I am not sure how to check if resources are included or not but I have not excluded them specifically

Our plugin architecture works as follows
Main app is on play store
When app launch and and user click one of the plugin then plugin related apk is downloaded from cloud and files are loaded using dexclassloader.
Just in case this helps

Thank you

Trevor McGuire

unread,
Jun 4, 2020, 10:16:09 PM6/4/20
to Android CameraX Discussion Group, agam.go...@gmail.com
Thank you for the information. Unfortunately DexClassLoader will only load the classes, but does not include resources. We have a change in review at the moment which would allow you to configure the ProcessCameraProvider programmatically with a CameraXConfig before calling ProcessCameraProvider.getInstance() (or before using CameraView). That new API will likely be available within the next 2 releases.

In the mean time, if you are able to add the following string resource to your res/values-v21/strings.xml of your base application, it may solve the issue you are having:
    <string name="androidx_camera_default_config_provider" translatable="false">
        androidx.camera.camera2.Camera2Config$DefaultProvider
    </string>

Agam Goyal

unread,
Jun 30, 2020, 1:24:14 AM6/30/20
to Android CameraX Discussion Group
Hello,
We have applied above mentioned solution but still facing issue as we are using cameraview and while passing lifecycleowner we can only pass current activity context

In this, we are getting class cast exception as we are using plugin context and it requires application context.

Now, I see that 2 new releases have come. Is there any other solution we can work with ?

Trevor McGuire

unread,
Jun 30, 2020, 2:56:33 PM6/30/20
to Android CameraX Discussion Group, goyal....@gmail.com
I don't think using the Activity as a LifecycleOwner is related to the class cast exception you are seeing. Could you post a stack trace so we track down where this ClassCastException is occurring? You mention that the ClassCastException is happening because CameraX is expecting an Application context. Does this mean that your plugin context's Context.getApplicationContext() method does not return an Application object?

Either way, we do have something you can try. The latest release of camera-lifecycle adds ProcessCameraProvider.configureInstance(CameraXConfig). Instead of using the string resource that I mentioned above, you can call this static method immediately after you load your plugin like so:

ProcessCameraProvider.configureInstance(Camera2Config.defaultConfig());

This may skip the code that is causing a ClassCastException, so it might fix your problem. Note that this method is experimental, so it may be changed or removed in future releases. Though if it is removed, we should have a better solution for you to replace it.



Agam Goyal

unread,
Jul 1, 2020, 3:07:29 AM7/1/20
to Android CameraX Discussion Group
Following is the error log -

AndroidRuntime: FATAL EXCEPTION: main
AndroidRuntime: java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.company.android.oneconnect/com.company.android.plugin.steamcloset.DeviceInfoActivity}:android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class androidx.camera.view.CameraView

AndroidRuntime: caused by: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class androidx.camera.view.CameraView

AndroidRuntime: CausedBy: java.lang.reflect.InvocationTargetException

AndroidRuntime: caused by: java.lang.ClassCastException: com.samsung.android.plugins.PluginApplicationContext cannot be cast to android.app.Application
AndroidRuntime: at androidx.camera.core.CameraX.getOrCreateInstance(CameraX.java:849)
AndroidRuntime: at androidx.camera.lifecycle.ProcessCameraProvider.getInstance(ProcessCameraProvider.java:137)
AndroidRuntime: at androidx.camera.view.CameraXModule.<init>(CameraXModule.java:124)
AndroidRuntime: at androidx.camera.view.CameraView.init(CameraView.java:172)
AndroidRuntime: at androidx.camera.view.CameraView.init(CameraView.java:138)
AndroidRuntime: at androidx.camera.view.CameraView.init(CameraView.java:133)

Trevor McGuire

unread,
Jul 8, 2020, 6:28:19 PM7/8/20
to Android CameraX Discussion Group, goyal....@gmail.com
Thank you for the stack trace. I think I have found where we can make sure this won't happen, but it won't be available until a future version. I've created https://issuetracker.google.com/160817073 to track.

In the mean time, are you able to use the other suggestion I had (using ProcessCameraProvider.configureInstance(CameraXConfig))? I believe it should work around the issue.

Federico Cossetta

unread,
Feb 16, 2022, 9:47:26 AM2/16/22
to Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com
I have a similar situation and found this post.


ProcessCameraProvider.configureInstance(CameraXConfig))

How can we call this method? CameraXConfig is only library_scoped


ProcessCameraProvider.configureInstance(Camera2Config.defaultConfig());

this could achieve the same result?

Trevor McGuire

unread,
Feb 16, 2022, 2:16:32 PM2/16/22
to Android CameraX Discussion Group, federico....@gmail.com, Trevor McGuire, goyal....@gmail.com
Do you mean the constructor for CameraXConfig.Builder is restricted to LIBRARY_GROUP? The CameraXConfig class itself shouldn't be restricted. That said, you should only create an instance from an existing config, such as with Camera2Config.defaultConfig() as that will also inject the required Camera2 implementation as the back-end for CameraX. If you want to customize the config (such as to set your own Executor), you can use: CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig).

Federico Cossetta

unread,
Feb 21, 2022, 5:38:32 AM2/21/22
to Android CameraX Discussion Group, trevor...@google.com, Federico Cossetta, goyal....@gmail.com
Still not working..
I have tried to add in the manifest file

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="androidx_camera_default_config_provider" translatable="false">
androidx.camera.camera2.Camera2Config$DefaultProvider
</string>
</resources>

But still not working in the injected module that use camerax.

In the injected part I do

CameraXConfig.Builder builder = CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig());
ProcessCameraProvider.configureInstance(builder.build());
ListenableFuture<ProcessCameraProvider> instance = ProcessCameraProvider.getInstance(pc.getContext());


But i get the following error 

2022-02-21 11:24:56.443 13007-16008/? E/CameraX: Failed to retrieve default CameraXConfig.Provider from meta-data
    android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{ape.ufogames/androidx.camera.core.impl.MetadataHolderService}
        at android.app.ApplicationPackageManager.getServiceInfo(ApplicationPackageManager.java:543)
        at c2101.u6.<init>(CameraX.java:10)
        at androidx.camera.lifecycle.b.b(ProcessCameraProvider.java:6)
        at c2101.r2.f(Camera.java:7)
        at c2101.og.run(Unknown Source:1)
        at c2101.d8.b(CompletableFromAction.java:5)
        at c2101.c8.a(Completable.java:1)
        at c2101.g8$a.run(CompletableSubscribeOn.java:1)
        at c2101.nk.call(ScheduledDirectTask.java:2)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)
2022-02-21 11:24:56.444 13007-16008/? E/xvoidx: [pool-5-thread-4] [wi] java.lang.IllegalStateException: CameraX is not configured properly. The most likely cause is you did not include a default implementation in your build such as 'camera-camera2'.
        at c2101.u6.<init>(CameraX.java:51)
        at androidx.camera.lifecycle.b.b(ProcessCameraProvider.java:6)
        at c2101.r2.f(Camera.java:7)
        at c2101.og.run(Unknown Source:1)
        at c2101.d8.b(CompletableFromAction.java:5)
        at c2101.c8.a(Completable.java:1)
        at c2101.g8$a.run(CompletableSubscribeOn.java:1)
        at c2101.nk.call(ScheduledDirectTask.java:2)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)
2022-02-21 11:30:13.236 16824-16824/? E/gle.android.tt: Not starting debugger since process cannot load the jdwp agent.

Scott Nien

unread,
Feb 21, 2022, 10:09:57 PM2/21/22
to Federico Cossetta, Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com
Hi Federico, 

The MetadataHolderService is declared in camera-camera2 AndroidManifest so if you have the camera-camera2 dependency added in your build.gradle,  the error should not happen. Can you double check this ? 
Besides, did you enable proguard (minifyEnabled) ? 

2022-02-21 11:24:56.443 13007-16008/? E/CameraX: Failed to retrieve default CameraXConfig.Provider from meta-data
    android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{ape.ufogames/androidx.camera.core.impl.MetadataHolderService}
 
--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/bcddde3a-9a7e-4031-aa24-b9dbca5b63afn%40android.com.

Federico Cossetta

unread,
Feb 22, 2022, 4:45:44 AM2/22/22
to Android CameraX Discussion Group, Scott Nien, Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com, Federico Cossetta
I am sure i included all the correct dependencies.

// CameraX core library
def camerax_version = '1.1.0-beta01'
implementation "androidx.camera:camera-core:$camerax_version"

// CameraX Camera2 extensions
implementation "androidx.camera:camera-camera2:$camerax_version"

// CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:$camerax_version"


However actually after rebuilding everything error changed and crashes with



2022-02-22 10:40:58.804 6662-7042/? I/xvoidx: [pool-5-thread-2] [d] Init cameraProvider
2022-02-22 10:40:58.837 6662-7069/? I/CameraManagerGlobal: Connecting to camera service
2022-02-22 10:40:58.856 1262-21966/? W/ServiceManager: Permission failure: android.permission.CAMERA_OPEN_CLOSE_LISTENER from uid=10343 pid=6662
2022-02-22 10:40:58.891 6662-7069/? D/CameraRepository: Added camera: 0
2022-02-22 10:40:58.907 6662-7069/? E/AndroidRuntime: FATAL EXCEPTION: CameraX-core_camera_0
    Process: com.tencent.mobileqq, PID: 6662
    java.lang.NoSuchMethodError: No super method postValue(Ljava/lang/Object;)V in class Landroidx/lifecycle/LiveData; or its super classes (declaration of 'androidx.lifecycle.LiveData' appears in /data/app/~~Z1jKjZqOa-vCd9Q3zZOgaw==/com.tencent.mobileqq-fi3KDYCOm0EROvu-uteK8A==/base.apk)
        at androidx.lifecycle.MutableLiveData.postValue(MutableLiveData.java:1)
        at p.u.<init>(Camera2CameraImpl.java:22)
        at p.o.b(Camera2CameraFactory.java:5)
        at w.p.b(CameraRepository.java:5)
        at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2(CameraX.java:22)
        at androidx.camera.core.CameraX.c(Unknown Source:0)
        at q.i.run(Unknown Source:2)

        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)


Seems more an obfuscation problem now. I am using proguard, yes. I cannot use in debug mode, it's not an option. Can you provide me a proguard-rules to apply? thanks

Federico Cossetta

unread,
Feb 23, 2022, 11:33:42 AM2/23/22
to Android CameraX Discussion Group, Federico Cossetta, Scott Nien, Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com
Still facing


 java.lang.NoSuchMethodError: No super method postValue(Ljava/lang/Object;)V in class Landroidx/lifecycle/LiveData; or its super classes (declaration of 'androidx.lifecycle.LiveData' appears in /data/app/~~iucDtEK-3vn0-kA3j59ZAg==/com.tencent.mobileqq-9e2i_XlTi0tgRIocV9MeKw==/base.apk)
        at androidx.lifecycle.MutableLiveData.postValue(MutableLiveData.java:1)
        at androidx.camera.camera2.internal.b.<init>(Camera2CameraImpl.java:22)
        at c2101.g5.b(Camera2CameraFactory.java:5)
        at c2101.b8.b(CameraRepository.java:5)
        at c2101.l6.run(Unknown Source:2

when calling

ListenableFuture<ProcessCameraProvider> instance = ProcessCameraProvider.getInstance(context);

Any idea?

Federico Cossetta

unread,
Feb 24, 2022, 5:17:18 AM2/24/22
to Android CameraX Discussion Group, Federico Cossetta, Scott Nien, Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com
Detalis about crash


022-02-24 11:15:56.371 9486-9486/? E/q.a: SafeAtomicHelper is broken!
    java.lang.ClassCastException
        at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.<init>(AtomicReferenceFieldUpdater.java:352)
        at java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdater.java:109)
        at q.a.<clinit>(AbstractResolvableFuture.java:6)
        at q.b$a.<init>(CallbackToFutureAdapter.java:2)
        at q.b.a(CallbackToFutureAdapter.java:1)
        at androidx.camera.core.CameraX.initInternal(CameraX.java:4)
        at androidx.camera.core.CameraX.<init>(CameraX.java:19)
        at androidx.camera.lifecycle.ProcessCameraProvider.getOrCreateCameraXInstance(ProcessCameraProvider.java:4)
        at androidx.camera.lifecycle.ProcessCameraProvider.getInstance(ProcessCameraProvider.java:2)
        at t0.e.run(PictureTaker.java:9)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
2022-02-24 11:15:56.377 9486-9533/? I/CameraManagerGlobal: Connecting to camera service
2022-02-24 11:15:56.382 1332-1311/? W/ServiceManager: Permission failure: android.permission.CAMERA_OPEN_CLOSE_LISTENER from uid=10345 pid=9486
2022-02-24 11:15:56.413 9486-9533/? D/CameraRepository: Added camera: 0
2022-02-24 11:15:56.434 9486-9533/? E/AndroidRuntime: FATAL EXCEPTION: CameraX-core_camera_0
    Process: com.tencent.mobileqq, PID: 9486

    java.lang.NoSuchMethodError: No super method postValue(Ljava/lang/Object;)V in class Landroidx/lifecycle/LiveData; or its super classes (declaration of 'androidx.lifecycle.LiveData' appears in /data/app/~~iucDtEK-3vn0-kA3j59ZAg==/com.tencent.mobileqq-9e2i_XlTi0tgRIocV9MeKw==/base.apk)
        at androidx.lifecycle.MutableLiveData.postValue(MutableLiveData.java:1)
        at androidx.camera.core.impl.LiveDataObservable.postValue(LiveDataObservable.java:1)
        at androidx.camera.camera2.internal.Camera2CameraImpl.<init>(Camera2CameraImpl.java:17)
        at androidx.camera.camera2.internal.Camera2CameraFactory.getCamera(Camera2CameraFactory.java:5)
        at androidx.camera.core.impl.CameraRepository.init(CameraRepository.java:5)
        at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2(CameraX.java:21)
        at androidx.camera.core.CameraX.c(Unknown Source:0)
        at androidx.camera.core.h.run(Unknown Source:10)

        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

ชนะชัย แซ่ตัน

unread,
Feb 24, 2022, 9:41:30 AM2/24/22
to Federico Cossetta, Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com
Ok

ในวันที่ พ. 16 ก.พ. 2022 21:47 Federico Cossetta <federico....@gmail.com> เขียนว่า:
--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.

Scott Nien

unread,
Feb 24, 2022, 12:02:20 PM2/24/22
to Federico Cossetta, Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com
Hi Federico, 
Can you share your proguard configuration in build.gradle. ?
For example: 
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"))

If you were using proguard-android-optimize.txt,  can you try using proguard-android.txt which will turn off the optimization and see if optimization causes this failure. 

Leo Huang

unread,
Feb 24, 2022, 10:17:45 PM2/24/22
to Scott Nien, Federico Cossetta, Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com
Could you also share all dependencies? including non-camerax dependencies. Thanks

'Scott Nien' via Android CameraX Discussion Group <camerax-d...@android.com> 於 2022年2月25日 週五 上午1:02寫道:

Federico Cossetta

unread,
Feb 28, 2022, 10:31:03 AM2/28/22
to Android CameraX Discussion Group, leoh...@google.com, Federico Cossetta, Android CameraX Discussion Group, trevor...@google.com, goyal....@gmail.com, Scott Nien
Basically  the problems come from androidx dependencies, if you try to execute the code inside a plugin runtime and you have core with obfuscation, it won't find runtime definition of all androidx libraries. I had to repack all camera x libs + androidx dependencies with jar-jar to use a different package name, as all "import androidx.**" will fall in the module.
Reply all
Reply to author
Forward
0 new messages