Problems migrating from Cap 4 -> 5

Following the migration guide:

We run into two problems:

First, we are unable to finish building because of duplicate classes:

Duplicate class android.support.v4.media.session.PlaybackStateCompat$CustomAction$1 found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompat$CustomAction$Builder found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompat$ErrorCode found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompat$MediaKeyAction found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompat$RepeatMode found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompat$ShuffleMode found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompat$State found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompatApi21 found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompatApi21$CustomAction found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.media.session.PlaybackStateCompatApi22 found in modules media-1.0.0-runtime (androidx.media:media:1.0.0) and support-media-compat-27.1.1-runtime (com.android.support:support-media-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.10.0-runtime (androidx.core:core:1.10.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)

Go to the documentation to learn how to Fix dependency resolution errors.

If we re-enable andorid.enableJetifier=true then we get past the duplicate classes error and can finish building but then we get a runtime error of:

Blockquote
2023-05-22 13:03:19.772 10981-10981 AndroidRuntime pid-10981 E FATAL EXCEPTION: main
Process: com.tigoenergy.smart, PID: 10981
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tigoenergy.smart/com.tigoenergy.smart.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4325)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
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:2574)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8757)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:846)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
at androidx.appcompat.app.AppCompatDelegateImpl.initWindowDecorActionBar(AppCompatDelegateImpl.java:550)
at androidx.appcompat.app.AppCompatDelegateImpl.getSupportActionBar(AppCompatDelegateImpl.java:537)
at androidx.appcompat.app.AppCompatDelegateImpl.invalidateOptionsMenu(AppCompatDelegateImpl.java:1220)
at androidx.appcompat.app.AppCompatActivity.invalidateOptionsMenu(AppCompatActivity.java:314)
at androidx.activity.ComponentActivity.invalidateMenu(ComponentActivity.java:588)
at androidx.activity.ComponentActivity$$ExternalSyntheticLambda0.run(Unknown Source:2)
at androidx.core.view.MenuHostHelper.addMenuProvider(MenuHostHelper.java:133)
at androidx.activity.ComponentActivity.addMenuProvider(ComponentActivity.java:566)
at androidx.fragment.app.FragmentActivity$HostCallbacks.addMenuProvider(FragmentActivity.java:736)
at androidx.fragment.app.FragmentManager.attachController(FragmentManager.java:2795)
at androidx.fragment.app.FragmentController.attachHost(FragmentController.java:117)
at androidx.fragment.app.FragmentActivity.lambda$init$3$androidx-fragment-app-FragmentActivity(FragmentActivity.java:140)
at androidx.fragment.app.FragmentActivity$$ExternalSyntheticLambda3.onContextAvailable(Unknown Source:2)
at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.kt:84)
at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:377)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
at com.getcapacitor.BridgeActivity.onCreate(BridgeActivity.java:23)
at android.app.Activity.performCreate(Activity.java:8591)
at android.app.Activity.performCreate(Activity.java:8570)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4150)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4325)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
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:2574)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8757)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
---------------------------- PROCESS ENDED (10981) for package com.tigoenergy.smart ----------------------------

If you check the res/values/styles.xml file, what’s the parent attribute of AppTheme.NoActionBar?

It should be parent="Theme.AppCompat.DayNight.NoActionBar"

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

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:background">@null</item>
    </style>


    <style name="AppTheme.NoActionBarLaunch" parent="Theme.SplashScreen">
        <item name="android:background">@drawable/splash</item>
    </style>
</resources>

Does your MainActivity.java extends BridgeActivity?
public class MainActivity extends BridgeActivity {}

does your Activity have that theme set in the AndroidManifest.xml?

I copied the MainAcitivity form a fresh Ionic project:

package com.tigoenergy.smart;

import com.getcapacitor.BridgeActivity;

public class MainActivity extends BridgeActivity {}

This was our old one that also had issues:

package com.tigoenergy.smart;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;

import java.util.ArrayList;

public class MainActivity extends BridgeActivity {
//   @Override
//   public void onCreate(Bundle savedInstanceState) {
//       super.onCreate(savedInstanceState);
//
//       
//       
//       // ATTENTION: This was auto-generated to handle app links.
//       Intent appLinkIntent = getIntent();
//       String appLinkAction = appLinkIntent.getAction();
//       Uri appLinkData = appLinkIntent.getData();
//   }
}

Both have the same issue

AndoridManifest:

<?xml version='1.0' encoding='utf-8'?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:networkSecurityConfig="@xml/network_security_config" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true">
        <meta-data android:name="asset_statements" android:resource="@string/asset_statements" />
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
                  android:exported="true"
                  android:label="@string/title_activity_main"
                  android:launchMode="singleTask"
                  android:name=".MainActivity
" android:theme="@style/AppTheme.NoActionBarLaunch">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="@string/custom_url_scheme" />
                <data android:host="ei.tigoenergy.com" android:scheme="http" />
                <data android:host="ei.tigoenergy.com" android:scheme="https" />
                <data android:scheme="https" />
            </intent-filter>
        </activity>
        <provider android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true" android:name="androidx.core.content.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
        </provider>
    </application>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:maxSdkVersion="30" android:name="android.permission.BLUETOOTH" />
    <uses-permission android:maxSdkVersion="30" android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <uses-feature android:name="android.hardware.camera" android:required="false" />
</manifest>

Yes it does have that same theme set. I’ve been copying to/from a fresh Ionic project that works on Capacitor 5, although it does throw errors once launched but doesnt crash.

hey @henryschultz & @julio-ionic ,
I run into the same problems when upgrading from Capacitor 4 to 5.

I’ve checked the theme and the MainActivity, but I can’t find issues there.

Are there other possible solutions to this?

In gradle.properties put

android.enableJetifier=true