android - 多维中的不同样式的applicationId

标签 android android-studio gradle

问题:根据口味设置applicationId。
更多问题:商店中已经有两个应用程序,它们都具有不同的applicationId样式。

  • com.name.dimension1.dimension2
  • com.name.dimension1dimension2(不带点)

  • 在我们的Android应用中,我们需要引入新的口味/尺寸。
    尺寸图

    flavourDimensions“公司”,“应用”,“服务器”

    看到这一点,这就是为什么我们不能在build.gradle中使用applicationIdSuffix的原因,因为它会自动添加。 (点)后缀。

    我们已经有了确定每种口味应使用哪个versionCode的方法(thanks to that answer on Stack)
    def getCurrentFlavor() {
        Gradle gradle = getGradle()
        String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
        Pattern pattern
        if (tskReqStr.contains("assemble"))
            pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
        else
            pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
        Matcher matcher = pattern.matcher(tskReqStr)
        if (matcher.find())
            return matcher.group(1).toLowerCase()
        else {
            println "NO MATCH FOUND"
            return ""
        }
    

    甚至更多的问题:可以帮助我们设置版本代码的相同方法,不能帮助applicationId。
    def getFlavorApplicationId() {
        def flavor = getCurrentFlavor()
        if (flavor.contains("company1") && flavor.contains("app1")) {
            return ext.company1app1AppId
        } else if (flavor.contains("company2") && flavor.contains("app1")) {
            return ext.company2app1AppId
        } else if (flavor.contains("company2") && flavor.contains("app2")) {
            return ext.company2app2AppId
        }
        return "nothing"
    }
    

    构建/同步应用程序后-一切正常(文件BuildConfig和生成的apk具有正确的applicationId)。
    当我们尝试使用applicationId运行应用程序时,会出现问题,具体取决于口味。
    Error while executing: am start -n "**non.of.those**/com.rsqtechnologies.rsqphysio.splash.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
    Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=non.of.those/com.rsqtechnologies.rsqphysio.splash.SplashActivity }
    Error type 3
    Error: Activity class {**non.of.those**/com.rsqtechnologies.rsqphysio.splash.SplashActivity} does not exist.
    Error while Launching activity
    

    如果我很了解, Android Studio 不会使用applicationId从BuildConfig甚至生成的.apk中运行应用程序。它正在尝试在启动应用程序时生成它(当它无法从gradle任务中收集有关 flavor 的信息时-def getCurrentFlavor())

    当我在终端中使用相同的命令但正确的appId独自运行该应用程序时-一切正常。

    我已经尝试过的事情还包括:
  • Solution from this Stack answer
  • 尝试在Android Studio中找到一种方法来编辑配置,以便采用正确的appId(失败)

  • 有人建议吗?相当复杂的问题,如果有人感兴趣,我可以分享更多详细信息。

    最佳答案

    使用groovy时,可以更改所有合并的 flavor 的applicationId:

    android.applicationVariants.all { variant ->
        def isApp1 = variant.name.contains('app1')
        def isApp2 = variant.name.contains('app2')
    
        def idAppendix = ""
        if (isApp1) idAppendix = ".withDotForApp1"
        if (isApp2) idAppendix = "noDotForApp2"
    
        mergedFlavor.setApplicationId(mergedFlavor.applicationId + idAppendix)
    }
    

    请注意,如果您打算使用kotlinscript,则此方法将无效,因为合并 flavor 的applicationid是val,并且不提供setter。

    关于android - 多维中的不同样式的applicationId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56291036/

    相关文章:

    android - Webview 链接在外部浏览器中打开

    android - 创建文件和所有父目录

    android - Google+ 从应用发送邀请信息

    android-studio - 超出 GC 开销限制 - Android Studio

    java - 无法使用 ShadowJar 运行构建的 gradle 项目 NoClassDefFoundError

    android - ./gradlew assembleRelease 更新以将 native 59.8 和 gradle react 为 5.1 时构建失败

    android-studio - uiautomatorviewer 是否取代了 Android 设备监视器中的 HierarchyViewer?

    java - 将用户发送到应用程序。如果应用程序不存在发送到Playstore

    java - 无法获取 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/7.0.2/gradle-7.0.2.pom'

    android - 从 Jenkins 管道中的 build.gradle 中读取 android 的版本名称