Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Is it possible to read package name inside AndroidManifest.xml file

I have a question, is it possible to read package name inside AndroidManifest.xml

I mean

I have a AndroidManifest.xml as below

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackagename"
    android:installLocation="auto"
    android:versionCode="1"
    android:versionName="1.0" >

....

</manifest>

Now I have to add a permission (used for GCM)

<permission
        android:name="MYPACKAGENAME.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

Is it possible to replace MYPACKAGENAME with com.mypackagename dynamically in the above permission tag.

like image 905
Vinayak Bevinakatti Avatar asked May 08 '13 11:05

Vinayak Bevinakatti


People also ask

What is contained within the AndroidManifest XML file?

Every app project must have an AndroidManifest. xml file (with precisely that name) at the root of the project source set. The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play.

How do I read a manifest XML file?

Just open your APK and in treeview select "AndroidManifest. xml". It will be readable just like that.

What is the Android name attribute used for in the AndroidManifest XML file?

It has many attributes such as label, name, theme, launchMode etc. android:label represents a label i.e. displayed on the screen. android:name represents a name for the activity class. It is required attribute.

Which of the following is element of AndroidManifest XML?

Element Tags of Manifest File It is the root element of this element. It consists of a package attribute package that tells the activity's package name. It is the subelement of the manifest file that includes the declaration of the namespace. It contains certain attributes.


2 Answers

You can drop the package name part of the name:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackagename">
    ...

    <permission
        android:name=".permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    ...
</manifest>

When you start your permission name with . it gets prefixed with package name automatically.

You cannot change package name dynamically, but you can do certain things by defining applicationId in build.gradle and then prefixing names in android manifest with ${applicationId}.

in build.gradle:

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.2"
    productFlavors {
       development {
           applicationId = "com.mypackage.development"
       }
    }
}

And then in your AndroidManifest.xml have:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackagename">
    ...

    <permission
        android:name="${applicationId}.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    ...
</manifest>

You can find a bit more from here: http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger

Later approach will allow you to run development flavor of your app side by side your production version. I hope this helps.

like image 105
miko Avatar answered Sep 17 '22 15:09

miko


You can use ${applicationId} to substitute your package name, eg.

<permission
        android:name="${applicationId}.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
like image 45
Tom Avatar answered Sep 21 '22 15:09

Tom