Android: PreviewView causes crash in dynamic feature module

1.8k views Asked by At

I am trying to create an on-demand feature module using the androidx.camera package. I am getting a crash when the activity inflates the layout which includes the PreviewView. Note, the module loads correctly and show the correct layout if I just use something simple like a TextView, so I don't think (hopefully anyways) that it's an issue with my dynamic feature setup/download.

Also this all works fine if I include the camera dependencies in the base app module, however it seems like I should be able to do that in the dynamic feature module. I'm not sure what i'm missing.. Anyone come across this before?

feature-module's build.gradle:

apply plugin: 'com.android.dynamic-feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 29

    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':app')

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.71"
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.core:core-ktx:1.3.1'

    def constraintLayoutVersion = "2.0.0-beta3"
    implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"

    // CameraX core library
    def camerax_version = "1.0.0-beta06"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    implementation "androidx.camera:camera-view:1.0.0-alpha13"
    implementation "androidx.camera:camera-extensions:1.0.0-alpha13"

    implementation "io.reactivex.rxjava3:rxandroid:3.0.0"

    // androidx
    implementation "androidx.annotation:annotation:1.0.0"
    implementation "androidx.appcompat:appcompat:1.0.0"
    implementation "androidx.cardview:cardview:1.0.0"
    implementation "androidx.gridlayout:gridlayout:1.0.0"
    implementation "androidx.palette:palette:1.0.0"
    implementation "androidx.recyclerview:recyclerview:1.0.0"

}

activity layout file:

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.camera.view.PreviewView
        android:id="@+id/viewFinder"
        android:layout_width="match_parent"
        android:layout_height="450dp"
        app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Activity in feature module:

class PhotoActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_photo)
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase)
        SplitCompat.installActivity(this)
    }
}

Stacktrace:

2020-10-06 10:53:03.861 27659-27659/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sample.android.debug, PID: 27659
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.android.debug/com.sample.android.image.PhotoActivity}: android.view.InflateException: Binary XML file line #10 in com.sample.android.debug.image:layout/activity_photo: Binary XML file line #10 in com.sample.android.debug.image:layout/activity_photo: Error inflating class androidx.camera.view.PreviewView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        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:930)
     Caused by: android.view.InflateException: Binary XML file line #10 in com.sample.android.debug.image:layout/activity_photo: Binary XML file line #10 in com.sample.android.debug.image:layout/activity_photo: Error inflating class androidx.camera.view.PreviewView
     Caused by: android.view.InflateException: Binary XML file line #10 in com.sample.android.debug.image:layout/activity_photo: Error inflating class androidx.camera.view.PreviewView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:854)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.sample.android.image.PhotoActivity.onCreate(PhotoActivity.kt:18)
        at android.app.Activity.performCreate(Activity.java:7825)
        at android.app.Activity.performCreate(Activity.java:7814)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        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:930)
2020-10-06 10:53:03.861 27659-27659/? E/AndroidRuntime: Caused by: java.lang.NoSuchMethodError: No direct method <init>(Ljava/lang/Object;)V in class Landroidx/lifecycle/MutableLiveData; or its super classes (declaration of 'androidx.lifecycle.MutableLiveData' appears in /data/app/com.sample.android.debug-a4U4CmSx2nFr8mxMFCrakg==/base.apk)
        at androidx.camera.view.PreviewView.<init>(PreviewView.java:80)
3

There are 3 answers

1
heatherrc On

For anyone else that might run across something like this -- My fix was to include the LiveData dependency in my base module:

implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
0
REMIL K R On

My issue was also the same. The error I got was

 Caused by: java.lang.NoSuchMethodError: No direct method <init>(Ljava/lang/Object;)V in class Landroidx/lifecycle/MutableLiveData; or its super classes (declaration of **'androidx.lifecycle.MutableLiveData'** appears 

By adding androidx.lifecycle.MutableLiveData dependency to the base module fixed the issue.

implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
0
dwb On

It might help someone out there, my issue was not including the required

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

in my app's build.grade file. It's mentioned here