何时使用应用程序 ID 或程序包名称 a.k.a。 PackageManager#getLaunchIntentForPackage 方法名称不正确吗?

When to use application ID or package name a.k.a. is the PackageManager#getLaunchIntentForPackage method name incorrect?

什么时候使用 'application ID' 或 'package'?

(还是'package name'?)

我在实际情况中的困惑:

从一个应用程序切换到另一个应用程序的一种方法是获取应用程序的启动意图,这可以使用(给定一些 context)来完成:

PackageManager manager = context.getPackageManager();
Intent intent = manager.getLaunchIntentForPackage(packageName);

请注意,参数名称称为 packageName,甚至方法名称也提到 'package'。但是,这 不是 包名!这是我们应该在这里使用的应用程序 ID!

例如,如果我有一个调试构建类型,而不是发布应用程序 ID com.example 具有调试后缀,例如它是 com.example.debug,我必须使用 "com.example.debug" 作为 getLaunchIntentForPackage 的参数。但是应用程序的包命名空间保持不变(by default):仍然是com.example!如果我没有安装我的非调试应用程序,getLaunchIntentForPackage 方法 returns null,因为没有安装应用程序 ID com.example

那我在看什么?我会认为 com.example 是我的包名称,而 com.example.debug 是我的应用程序 ID。但是为什么PackageManager#getLaunchIntentForPackage方法提到'package'和'package name',而认为它应该提到'application ID'呢?

这只是 Android SDK 中错误命名的方法,还是我误解了什么?

我的问题基于以下信息。

来自 application IDs 的文档,我引用:

Every Android app has a unique application ID that looks like a Java package name, such as com.example.myapp. This ID uniquely identifies your app on the device and in Google Play Store. If you want to upload a new version of your app, the application ID (and the certificate you sign it with) must be the same as the original APK—if you change the application ID, Google Play Store treats the APK as a completely different app. So once you publish your app, you should never change the application ID.

[...]

When you create a new project in Android Studio, the applicationId exactly matches the Java-style package name you chose during setup. However, the application ID and package name are independent of each other beyond this point. You can change your code's package name (your code namespace) and it will not affect the application ID, and vice versa (though, again, you should not change your application ID once you publish your app). However, changing the package name has other consequences you should be aware of, so see the section about modifying the package name.

因此应用程序 ID 在 Play 商店中唯一标识您的应用程序。包名称是子包的命名空间和应用程序代码库中的 类。到目前为止一切顺利,我明白这一点。

然而,在本文档页面的末尾,我们有以下更令人困惑的亮点:

One more thing to know: Although you may have a different name for the manifest package and the Gradle applicationId, the build tools copy the application ID into your APK's final manifest file at the end of the build. So if you inspect your AndroidManifest.xml file after a build, don't be surprised that the package attribute has changed. The package attribute is where Google Play Store and the Android platform actually look to identify your app; so once the build has made use of the original value (to namespace the R class and resolve manifest class names), it discards that value and replaces it with the application ID.

所以我们在一页上有多个矛盾!

  1. Google Play 通过应用程序 ID 与程序包来识别应用程序。
  2. 清单的包名可能与应用程序 ID 不同,编译时包会被构建工具的应用程序 ID 覆盖。

从这个亮点来看,至少关于应用程序覆盖包名称的构建工具的说法似乎是不正确的,因为:

在应用 com.example.debug 中为 activity com.example.MainActivity 创建意图时,我必须将 com.example.debug 指定为应用程序 ID,将 com.example.MainActivity 指定为组件名称.在运行时,组件名称没有将其包名称更改为应用程序 ID,因此构建工具似乎没有更改包名称空间?

我希望...

...有人可以通过各种示例创建一个很好的答案,这些示例显示应用程序 ID 和程序包(名称)何时相同,何时不同,以及 Android 系统和 Google玩实际用哪个。还请考虑一个具有库依赖项的示例:据我了解,库的包命名空间不会更改,但它会使用您的应用程序 ID 运行,对吗?

<manifest>中的android:package属性就是我们这里说的。它过去被称为 "package name",在一些较旧的文档中仍被称为 "package name"。它 不是 Java 包名称 ,即使它看起来像一个,并且它与源代码中的 Java 包名称无关。

最近此属性被称为 "Application ID",尤其是在与 Firebase、Play 商店等相关的文档中。

这是一回事。 "package name" 是 "application ID".

参考https://developer.android.com/guide/topics/manifest/manifest-element.html#package

注意:

有一个地方可能会让人感到困惑,那就是清单中组件名称的 shorthand 符号。例如:

    <activity
            android:name=".ui.activities.MainActivity">
    </activity>

在这种情况下,android:name 属性指定 "fully qualified Java class name of the Activity"。但是,如果名称以句点开头(如本例所示),应用程序的 "package name"(来自 <manifest> 标记的 android:package 属性)将添加到 class 形成 Activity.

的完全限定 Java class 名称

如果您遇到 android:package 属性因不同构建变体而更改的情况(如您在包名称中使用 debug 的示例),您不应使用清单中组件名称的 shorthand 表示法! 您应该始终使用完全限定名称,如下所示:

    <activity
            android:name="com.mycompany.mypackage.ui.activities.MainActivity">
    </activity>