apache / cordova

Apache Cordova
https://cordova.apache.org/
631 stars 65 forks source link

android:exported duplicates the activity #340

Closed InesBorgesCore closed 2 years ago

InesBorgesCore commented 2 years ago

I need help figuring out how toprepare Cordova app for android API 31.

I tried to deploy an .aab file in Google PLay Console for a new app but it that it needs to support API 31 so I changed the preference attribute in config.xml.

<preference name="android-targetSdkVersion" value="31" />

After that it gave me this error: Apps targeting Android 12 and higher are required to specify an explicit value forandroid:exportedwhen the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

So I googled it and stack overflow gave some tips and my code is the following:

<platform name="android">
        <allow-intent href="market:*" />
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
            <activity android:exported="true"/>
        </edit-config>
        <config-file after="uses-permission" parent="/manifest" target="AndroidManifest.xml">
            <uses-permission android:name="android.permission.CAMERA" />
        </config-file>

        <edit-config
        file="app/src/main/AndroidManifest.xml"
        target="/manifest/application/activity[@android:name='MainActivity']"
        mode="merge">
            <activity android:exported="true"/>
        </edit-config>

        <edit-config
        file="app/src/main/AndroidManifest.xml"
        target="/manifest/application/activity[@android:name='com.google.zxing.client.android.encode.EncodeActivity']"
        mode="merge">
            <activity android:exported="true"/>
        </edit-config>
        <edit-config
        file="app/src/main/AndroidManifest.xml"
        target="/manifest/application/activity[@android:name='com.google.zxing.client.android.HelpActivity']"
        mode="merge">
            <activity android:exported="true"/>
        </edit-config>

        <edit-config
        file="app/src/main/AndroidManifest.xml"
        target="/manifest/application/activity"
        mode="merge">
            <activity android:exported="true"/>
        </edit-config>

        <icon src="./res/icons/android/48.png" density="mdpi" />
        <icon src="./res/icons/android/72.png" density="hdpi" />
        <icon src="./res/icons/android/96.png" density="xhdpi" />
        <icon src="./res/icons/android/144.png" density="xxhdpi" />
        <icon src="./res/icons/android/192.png" density="xxhdpi" />
    </platform>

Now it gives me this error: Element activity#com.google.zxing.client.android.encode.EncodeActivity at AndroidManifest.xml:31:9-36:20 duplicated with element declared at AndroidManifest.xml:19:9-24:20 app\src\main\AndroidManifest.xml:37:9-42:20 Error: Element activity#com.google.zxing.client.android.HelpActivity at AndroidManifest.xml:37:9-42:20 duplicated with element declared at AndroidManifest.xml:25:9-30:20 \app\src\main\AndroidManifest.xml Error: Validation failed, exiting

In my AndroidManifest.xml file:

<application android:exported="true" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:usesCleartextTraffic="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:exported="true" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@style/Theme.AppCompat.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden" android:exported="false" android:name="com.google.zxing.client.android.CaptureActivity" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:windowSoftInputMode="stateAlwaysHidden">
            <intent-filter>
                <action android:name="com.phonegap.plugins.barcodescanner.SCAN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:exported="true" android:label="@string/share_name" android:name="com.google.zxing.client.android.encode.EncodeActivity">
            <intent-filter>
                <action android:name="com.phonegap.plugins.barcodescanner.ENCODE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:exported="true" android:label="@string/share_name" android:name="com.google.zxing.client.android.HelpActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:label="@string/share_name" android:name="com.google.zxing.client.android.encode.EncodeActivity">
            <intent-filter>
                <action android:name="com.phonegap.plugins.barcodescanner.ENCODE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:label="@string/share_name" android:name="com.google.zxing.client.android.HelpActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

How do I solve this? I solved this problem once but I don't know how. I didn't do anything special. It just started working after I commented the edit-config I added for the activities. Now it doesn't work like that.

breautek commented 2 years ago

cordova-android has the patch which landed in version 10.1.2 for the android:exported.

Cordova-android will manage the main activity, but if third-party plugins or libraries provides their own activities that may need to be updated as well. In those cases, the plugin maintainers needs to update their plugins to use the android:exported on the activities they include.

The particular duplicate error is because you have two <activity> tags with the same android:name attribute. This could be caused by a conflict between your <edit-config> rules and (presumably) the plugin that adds these activity entries. If the plugin maintainers are unable or otherwise unwilling to update their plugins, it may be best to fork the plugin if their license allows it. Alternatively you can try writing a hook instead of using <edit-config> that manipulates the AndroidManifest.

There are several known issues on <edit-config> that makes it very difficult to use, unfortunately.