gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:33:20 28 4
gpt4 key购买 nike

什么时候使用“应用程序 ID”或“程序包”?

(或者是“包名”?)

我在实际情况中的困惑:

从一个应用程序切换到另一个应用程序的一种方法是获取应用程序的启动 Intent ,这可以使用(给定一些 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 方法返回 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. list 的包名称可以与应用程序 ID 不同,而在编译时,包会被构建工具的应用程序 ID 覆盖。

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

在应用 com.example.debug 中为 Activity com.example.MainActivity 创建 Intent 时,我必须指定 com.example.debug 作为应用程序 ID,com.example.MainActivity 作为组件名称。在运行时,组件名称并没有将其包名称更改为应用程序 ID,因此包 namespace 似乎没有被构建工具更改?

我希望...

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

最佳答案

android:package <manifest> 中的属性这就是我们在这里谈论的。它曾经被称为“包名”,在一些较旧的文档中仍然被称为“包名”。它不是 Java 包名,即使它看起来像一个包名,但它与源代码中的 Java 包名无关。

最近此属性被称为“应用程序 ID”,尤其是在与 Firebase、Play 商店等相关的文档中。

其实是一样的。 “包名”为“应用ID”。

引用https://developer.android.com/guide/topics/manifest/manifest-element.html#package

注意:

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

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

在这种情况下,android:name属性指定“Activity 的完全限定 Java 类名”。但是,如果名称以句点开头(如本例所示),则应用程序的“程序包名称”(来自 android:package 标记的 <manifest> 属性)将添加到类的名称前面,以形成完全限定的 Java Activity 的类名.

如果您遇到 android:package不同构建变体的属性已更改(如在包名称中使用 debug 的示例),您不应在 list 中使用组件名称的简写符号!您应始终使用完全限定名称,像这样:

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

关于android - 何时使用应用程序 ID 或程序包名称 a.k.a. PackageManager#getLaunchIntentForPackage 方法名称不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55222971/

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