基于构建类型的应用程序 ID

Application Id based on build type

我需要根据构建类型更改完整的应用程序 ID,而不是在 ID 后添加后缀。我试图在每个构建类型中移动 applicationId,但 Gradle 默认使用最后一个构建类型中的最后一个。 那可能吗?

为了更改您的 applicationId,您需要更改清单和所有目录中的 package

  1. Go to top left corner of Android Studio where it says Android
  2. Click on it and change it to Project Files as of Android Studio 3.3
  3. Right next to the side of Project Files click on the gear and Uncheck "Compact Directories"
  4. Open folders -> click on app -> src -> main -> java -> your -> package -> name
  5. Now that you see individual folders, right click the parts that you want to change -> Refactor -> Rename

然后你可以在你的 AndroidManifest.xml

中更改你的 applicationId 和 package

希望对您有所帮助

来源: Change Application Id Official Android Documentation

您可以使用 gradle 过滤器忽略某些 build/flavor 个目标。
检查以下代码:

variantFilter { variant ->
   def flavor = variant.flavors*.name
   def buildType = variant.buildType*.name

   // To check for a certain build type, use variant.buildType.name == "<buildType>"
   if ((flavor.contains("xyz") && buildType.contains("debug"))) {
       // Gradle ignores any variants that satisfy the conditions above.
       setIgnore(true)
   }
}

我使用这种简单的方法并且效果很好

android {

    ...
    
    applicationVariants.all { variant ->
        if (variant.name.contains("debug")) {
            variant.mergedFlavor.applicationId = "application.id.debug"
        } else {
            variant.mergedFlavor.applicationId = "application.id.release"
        }
    }
}