Reputation: 40503
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.
Upvotes: 17
Views: 14557
Reputation: 4563
An alternative not yet posted here is "${packageName}"
. This is useful, for example if you're developing a library with a certain package name, but the application(s) using that library will have a different package name and/or application ID: "${packageName}"
in the library's manifest will expand to the library's package name.
Upvotes: 6
Reputation: 7804
You can use ${applicationId}
to substitute your package name, eg.
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
Upvotes: 10
Reputation: 376
you can uses a relative usage.
Static :
<permission
android:name="com.mypackagename.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
Relative :
<permission
android:name=".permission.C2D_MESSAGE"
android:protectionLevel="signature" />
when the name starts with dot is like to put MYPACKAGENAME declared in
<?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>
Upvotes: 0
Reputation: 449
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.
Upvotes: 27
Reputation: 101
AndroidManifest.xml is (as its name suggests) for declaring and manifesting things.
Reading your package name dynamically within your application is possible because its declared in your manifest file.
Theoritically it's not possible to read or pass values inside your AndroidManifest. You will need to do the dynamic operations inside your application.
Upvotes: -1
Reputation: 5472
Your answer is NO..
Once you publish your application, you cannot change the package name. The package name defines your application's identity, so if you change it, then it is considered to be a different application and users of the previous version cannot update to the new version.
In other words
Android considers application with different package name as different application altogether
EDIT:
Is it Context.getPackageName()
you are looking for
Upvotes: -3