Android:Which one to use for package name? Manifest's packagename or Gradle applicationId?

The application id specified in the build.gradle will be used.

The entry in the AndroidManifest.xml is overridden

Your AndroidManifest.xml will look like this

<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver ...>
    <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="${applicationId}" />
        </intent-filter>
</receiver>

There's a great support article about this here by Android Studio.

Why are there two?

  • The final package that is used in your built .apk's manifest, and is the package your app is known as on your device and in the Google Play store, is the "application id" as specified in the gradle file.
  • The package that is used in your source code to refer to your R class, and to resolve any relative activity/service registrations, continues to be called the "package" as defined in your manifest.

Which one should I edit to change my final package name
The package name in Gradle will overwrite the package name in the Manifest. So you should change it in Gradle.

If you would like to actually change the structure of your project, then you'd need to change your packagename in Manifest.xml

For permissions
In your particular case, the permissions, you ask which package name you should provide for Google Messaging. Since Google Messaging requires your final package name, you should enter your gradle packagename. If you're using flavours, you should do this dynamically as suggested by @alim's answer.