-2

In my manifest file, I declare two BroadcastReceiver for my Activity. So far, while I was working in debug mode, the receivers were declared in the activity block and everything was fine. As my project is close to the end, I have decided to build an signed APK. The problem I face is that android studio returns me the following error :

Error:(20) Error: The <receiver> element must be a direct child of the <application> element [WrongManifestParent]

If I move the receivers blocks out of the activity block, the signed APK is generated. The consequence is that I get a runtime error when the receiver is called (java.lang.RuntimeException: Unable to instantiate receiver...).

How can I do to have my app run correctly both in debug and release mode?

Zelig63
  • 1,592
  • 1
  • 23
  • 40

2 Answers2

3

The structure of your Manifest file should look similar to code below. You should not declare receivers anywhere else.

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!-- -- First Activity -- -->

    <activity
        ...
        ... >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- -- Second Activity -- -->

    <activity
        ...
        ... >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="..." />
    </activity>

    <!-- -- First Receiver -- -->

    <receiver android:name="..."/>

    <!-- -- Second Receiver -- -->

    <receiver android:name="..." android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

</application>
Marat
  • 6,142
  • 6
  • 39
  • 67
0

Well, as my receiver class was an inner-class, there was apparently no need to declare it in the manifest file (and as it was an inner-class, an error was generated when I declared it outside the activity block). Now that I have removed it's declaration, the signed APK is correctly generated and I don't get runtimes error any more.

Zelig63
  • 1,592
  • 1
  • 23
  • 40