android - Difference between android:name and android:label - android

I am flutter developer and some Android settings confuses me.
What is the difference between android:label and android:name in AndroidManifest.xml??
<application
android:name="io.flutter.app.FlutterApplication"
android:label="flutterapp2"
android:icon="#mipmap/ic_launcher">

The
android:name="io.flutter.app.FlutterApplication"
is a default to a Flutter application & you should NOT edit this anyway (unless you created a class that extends FlutterApplication class).
The android:label is to define your app name, which is display in the installed application list.
If you want to change the app name in home screen, check android:label inside the <activity /> tag
For more information, check out the official documentation:
https://developer.android.com/guide/topics/manifest/application-element
android:name
The fully qualified name of an Application subclass
implemented for the application. When the application process is
started, this class is instantiated before any of the application's
components. The subclass is optional; most applications won't need
one. In the absence of a subclass, Android uses an instance of the
base Application class.
android:label
A user-readable label for the application as a whole, and a default
label for each of the application's components. See the individual
label attributes for , , ,
, and elements. The label should be set as a
reference to a string resource, so that it can be localized like other
strings in the user interface. However, as a convenience while you're
developing the application, it can also be set as a raw string.

According to https://developer.android.com/guide/topics/manifest/application-element
android:name
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this
class is instantiated before any of the application's components.
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base
Application class.
android:label
A user-readable label for the application as a whole, and a default label for each of the application's components. See the
individual label attributes for , ,
, , and elements.
The label should be set as a reference to a string resource, so that it can be localized like other strings in the user interface.
However, as a convenience while you're developing the application, it
can also be set as a raw string.

android:name this is a class which will wxecute first time (this is not your app name ,this is a functional thing)
and
android:label this is name of app shown on icon which represent the app name

android name is the name of the package that you are defining for that project and android label is the default name for your application

The android documentation states the following and you may find the list of attributes here
android:name
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.
android:label
A user-readable label for the application as a whole, and a default label for each of the application's components. See the individual label attributes for , , , , and elements.
The label should be set as a reference to a string resource, so that it can be localized like other strings in the user interface. However, as a convenience while you're developing the application, it can also be set as a raw string.

Related

Android application vs activity in the manifest

In Android Studio there is a manifest file where I can give attributes to either the application tag or the activity tag.
I'm just struggling to understand what the difference is between these two things.
In a tutorial I followed, orientation was fixed by forcing Portrait at the activity level. Why not do this at the application level?
What is the difference between giving a label attribute at the application level vs. the activity level? Or both?
There is no option to force screen orientation on <application>.
The label on <application> controls things like how your app appears in Settings in the list of installed apps, or any other place where we deal with things at the app level. It also is the default label for activities. Activities can specify a separate label, overriding the application-level default, if they so choose.
Starting with Question 2:
The manifest is the dictionary/table of contents for your android app, one of the first files that are looked at by the system when the your app is loaded/started is the manifest file
application and activity are totally different things
The application tag is your whole app overall(think of it as a Book), and the label in the application is your app title/name (Book title)
The activity tag is your context/content of your app (your Book chapters/sections), so when you define label for your activity as if you are naming that Chapter (example Chapter 1).
Summary: The application (book) must at least have label/title, and so activity/chapter, and sometimes your activities/chapters don't have titles but would be not good practice.
Question1: Its part of the Android framework (rules) that you have to specify what on the activities (chapters) level 1 by 1 not application (Book) level

application's name attribute - What is it for?

Inside my android manifest file I have an application tag and inside is name property/label.For what is this used for ?
If you are referring to the android:name XML attribute, then, quoting the documentation, it is:
The fully qualified name of an Application subclass implemented for the application. When the application process is started, this class is instantiated before any of the application's components.
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.
Application class in the Android is used for global settings or running entrance.
In the Android reference it describes the Application class: "Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created." more

Android Manifest: Why sometimes ".<classname>" instead of just "<classname>"?

What does the dot mean?
most of the time I just write that:
<activity android:name="OneActivity" ...>...</activity>
But sometimes I see in the autogenerated file:
<activity android:name=".OtherActivity" ...>...</activity>
And also in the Docs for Service I see that they write
<manifest ... >
...
<application ... >
<service android:name=".ExampleService" />
...
</application>
</manifest>
But I never saw any differences in trying the one or the other.
If you take a look at above, there is package definition like
package="app.package.name"
The class name with the dot means that that class is under the defined package.
If you have another package like
app.package.name.another
and there is a class in that package, you have to defined class name like
<activity android:name=".another.activityname"
From the the Android Dev Guide < activity > reference
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the element. There is
no default. The name must be specified.
Credit: jaywon from Activity Name in android Manifest in Stack Overflow
You can find some differences if you create more than one package, android checks for the class in the default folder that you might have mentioned while creating project.
As for the service, it automatically appends "services" to the package name and looks for the service in it. so its more like relative and absolute paths, if you place your service in different package name, you will have to mention the whole package path with class name. It applies to receivers too..
For picking up any activity Android requires fully qualified name...
For that Our Manifest files has attribute (i.e. package="com.test")
So to make it fully qualified we put dot before activity name (i.e. android:name=".FirstActivity" )
If you do not want to use dot before every activity then simply put dot after your package attribute in manifest tag.(i.e. package="com.test. ") and write activity name without dot (i.e. android:name="FirstActivity" )
So that it can overall make a fully qualified name (i.e. com.test.FirstActivity)
The dot before the name means that it is a hidden file that will not be seen by others. You can see on youtube how to hide files by android cell.

What's the "dot" for when registering an Activity

I'm kind of a noob at programming for the Android OS. I noticed in the books I have been reading that the authors have placed a "dot" in front of the activity name when registering their activities in the manifest. I've looked around the Android developer site and I can't figure out why we need the "dot". Does the "dot" actually server a purpose? Do I need it? I have included an example below. Notice the "dot" before "NewActivity":
<activity android:name=".NewActivity"></activity>
As you have noticed the point is not necessary but it basically means: the activity class lives in the same package of the app. So, if your app package is: com.my.package then:
.YourActivity means that your class is inside com.my.package.
YourActivity means that your class is inside com.my.package (same as above).
.activities.YourActivity means that your class is inside com.my.package.activitites.
You can even do something like: com.my.package.activities.YourActivity which is useful when you want to have different versions of your app and use Ant to change the references to the package automatically.
http://developer.android.com/guide/topics/manifest/activity-element.html#nm
android:name
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the <manifest>.
So given ApplicationManifest.xml:
<manifest
...
package="com.stackoverflow.android.geotask"
...>
<application ...>
<activity android:name=".view.TaskListListView" ...>
...
</application>
</manifest>
then since android:name=".view.TaskListListView" has a leading period, so it is interpreted as android:name="com.stackoverflow.android.geotask.view.TaskListListView".
That dot will append your package in your application manifest.
If your package name is com.app.demo.
<activity android:name=".HelloWorldActivity">
It means that Activity is lying inside demo package.
You can replace this with
<activity android:name="com.app.demo.HelloWorldActivity">

Is the activity name in AndroidManifest.xml required to start with a dot?

Is it required to start activity name with dot ('.') in manifest file.? for example activity
ContactManager starts with '.'
<activity android:name=".ContactManager" android:label="#string/app_name">
where as the activity ContactAdder is without dot
<activity android:name="ContactAdder" android:label="#string/addContactTitle">
in the manifest file of ContactManager sample http://developer.android.com/resources/samples/ContactManager/AndroidManifest.html
UPDATE: If activity name starts with . it is appended to package name to become fully qualified name, but what happens if it doesn't start with '.'
I got curious too, and went looking for it in the Android source code.
I found what seems to be the relevant code at the platform/frameworks/base repository, in the tools/aapt/Resource.cpp file. The relevant function is fullyQualifyClassName, called by massageManifest.
The rule it applies is explained in a comment block within the fullyQualifyClassName function:
// asdf --> package.asdf
// .asdf .a.b --> package.asdf package.a.b
// asdf.adsf --> asdf.asdf
Explaining this rule, we have:
If the name starts with a dot, always prefix it with the package.
If the name has a dot anywhere else, do not prefix it.
If the name has no dot at all, also prefix it with the package.
So, to answer your question: as long as there is no dot anywhere else, both ways of writing the activity name should have the same effect.
As an extra, the massageManifest function shows where this rule is applied:
In the application element, on the name and backupAgent attributes.
In the activity, service, receiver, provider, and activity-alias elements, on the name attribute.
In the activity-alias element, on the targetActivity attribute.
From the Android Dev Guide < activity > reference:
The name of the class that implements
the activity, a subclass of Activity.
The attribute value should be a fully
qualified class name (such as,
"com.example.project.ExtracurricularActivity").
However, as a shorthand, if the first
character of the name is a period (for
example, ".ExtracurricularActivity"),
it is appended to the package name
specified in the element.
There is no default. The name must be
specified.
Recently I understood the application package concept in Android and the answer for this question, thought i should share it.
If the application package(specified in manifest) is same as the java package in which Activity is present then it is not required to specify full package name in manifest for activities. If application package name is different from the java package name then activity name should be complete with package name.
This blog post give information about the application package and java packages in android.
http://blog.javia.org/android-package-name/comment-page-1/#comment-14063

Categories

Resources