gpt4 book ai didi

android - 当开发名称以数字开头时,Android Studio 中的包名称?

转载 作者:太空狗 更新时间:2023-10-29 15:45:50 26 4
gpt4 key购买 nike

我想以我公司的名义创建应用,Android Studio 建议使用您的域名,例如:

域名:example.com

应用:我的应用

给出包名 com.example.myApp

没关系,但我的公司名称以数字开头,Android Studio 告诉我不能有数字,例如:

com.1example.myApp

现在我可以尝试“oneexample”,希望 oneexample.com 上没有人开始开发 Android 应用程序,但考虑到有多少开发者的名字开头都有数字,这大概是以前出现过的。所以如果我可以请问:

1) 我应该如何处理命名我的包?

2) 出于好奇,为什么包名不能以数字开头?

非常感谢!

昆汀。

最佳答案

1) How should I tackle naming my packages?

简答:Android 包/子包名称必须以 ASCII 拉丁字母开头。由于在域名以数字开头的情况下,谷歌似乎没有关于包命名的指导,因此开发人员只能自己解决这个问题。

以下是一些以数字开头的域名的真实示例,以及开发人员决定用于 Android 的包名称:

http://www.11bitstudios.com/
https://play.google.com/store/apps/details?id=com.elevenbitstudios.AnomalyWarzoneEarthHD

http://www.23snaps.com/
https://play.google.com/store/apps/details?id=com.snaps23.android

http://www.36you.com/
https://play.google.com/store/apps/details?id=com.thirtysixyougames.google.slotanddragons

更多背景

Java 允许包名以 ASCII 拉丁字母或下划线字符开头。来自 Naming a Package (The Java™ Tutorials > Learning the Java Language > Packages) :

In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore.

下面还展示了“包名合法化”的例子:

Domain Name                         Package Name Prefix
--------------------------- ---------------------------
hyphenated-name.example.org org.example.hyphenated_name
example.int int_.example
123name.example.com com.example._123name

但是,Android 比 Java 限制更多,包/子包名称不能以下划线开头。来自 Issue 65570 - Android Open Source Project - Issue Tracker - Google Project Hosting :

While Java allows package names that start with underscores, Android application packages (which have additional restrictions) do not. When you try to install an app whose package starts with _, the device PackageManager will reject it. For that reason we tightened the package name validation in Studio when you create the app such that you don't end up creating these packages up front and then suffering later.

不幸的是,对于以数字开头的 Android 域包名称合法化,Google 似乎没有任何建议的约定。

2) Out of curiosity, why can't you have package names beginning with a number?

来自 Chapter 3. Lexical Structure (The Java® Language Specification - Java SE 7 Edition) :

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

<截图>

The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

同样,这在 Android 包名称中受到进一步限制。来自 <manifest> | Android Developers :

package A full Java-language-style package name for the application. The name should be unique. The name may contain uppercase or lowercase letters ('A' through 'Z'), numbers, and underscores ('_'). However, individual package name parts may only start with letters.

感谢@Quentin 提供有关 Android 对 Java 的额外限制的信息。

关于android - 当开发名称以数字开头时,Android Studio 中的包名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28666297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com