AndroidManifest.xml 中的活动名称是否需要以点开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3608017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:57:17  来源:igfitidea点击:

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

android

提问by Suresh

Is it required to start activity name with dot ('.') in manifest file.? for example activity ContactManager starts with '.'

是否需要在清单文件中以点('.')开头的活动名称。?例如活动 ContactManager 以 '.' 开头

<activity android:name=".ContactManager" android:label="@string/app_name">

where as the activity ContactAdder is without dot

因为活动 ContactAdder 没有点

<activity android:name="ContactAdder" android:label="@string/addContactTitle">

in the manifest file of ContactManager sample http://developer.android.com/resources/samples/ContactManager/AndroidManifest.html

在 ContactManager 示例的清单文件中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 '.'

更新:如果活动名称以 . 它被附加到包名后成为完全限定名,但如果它不以 '.' 开头会发生什么。

回答by CesarB

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/baserepository, in the tools/aapt/Resource.cppfile. The relevant function is fullyQualifyClassName, called by massageManifest.

platform/frameworks/basetools/aapt/Resource.cpp文件中的存储库中找到了似乎是相关代码的内容。相关函数是fullyQualifyClassName,由 调用massageManifest

The rule it applies is explained in a comment block within the fullyQualifyClassNamefunction:

它应用的规则在fullyQualifyClassName函数内的注释块中进行了解释:

// asdf     --> package.asdf
// .asdf  .a.b  --> package.asdf package.a.b
// asdf.adsf --> asdf.asdf

Explaining this rule, we have:

解释这个规则,我们有:

  1. If the name starts with a dot, always prefix it with the package.
  2. If the name has a dot anywhere else, do not prefix it.
  3. If the name has no dot at all, also prefix it with the package.
  1. 如果名称以点开头,请始终使用包作为前缀。
  2. 如果名称在其他任何地方都有点,请不要添加前缀。
  3. 如果名称根本没有点,还要在包前面加上前缀。

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 massageManifestfunction shows where this rule is applied:

此外,该massageManifest函数还显示了应用此规则的位置:

  • In the applicationelement, on the nameand backupAgentattributes.
  • In the activity, service, receiver, provider, and activity-aliaselements, on the nameattribute.
  • In the activity-aliaselement, on the targetActivityattribute.
  • application元素中,在namebackupAgent属性上。
  • activityservicereceiverprovider,和activity-alias元素,在name属性。
  • activity-alias元素中,在targetActivity属性上。

回答by jaywon

From the Android Dev Guide < activity > reference:

来自Android 开发指南 <activity> 参考

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.

实现活动的类的名称,是活动的子类。属性值应该是完全限定的类名(例如,“com.example.project.ExtracurricularActivity”)。但是,作为简写,如果名称的第一个字符是句点(例如,“.ExtracurricularActivity”),则会将其附加到元素中指定的包名称。没有默认值。必须指定名称。

回答by Suresh

Recently I understood the application package concept in Android and the answer for this question, thought i should share it.

最近了解了Android中的应用包概念和这个问题的答案,觉得应该分享一下。

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.

如果应用程序包(在清单中指定)与 Activity 所在的 java 包相同,则不需要在清单中为活动指定完整的包名。如果应用程序包名称与 java 包名称不同,则活动名称应与包名称一起完成。

This blog post give information about the application package and java packages in android.

这篇博文提供了有关 android 中的应用程序包和 java 包的信息。

http://blog.javia.org/android-package-name/comment-page-1/#comment-14063

http://blog.javia.org/android-package-name/comment-page-1/#comment-14063