如何在某个生成的资源值中包含`applicationIdSuffix`?

4

想像一下这个设置:

android  {
   buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
   }
   productFlavors {
       foo {
           applicationId defaultConfig.applicationId + '.foo'
       }
   }

我可以帮您设置动态字符串值,例如

resValue "string", "package_name", applicationId

那么如何在调试版本中包含applicationIdSuffix呢?

如果我把它添加到defaultConfig,那么将设置默认配置的applicationId。如果我将其添加到口味配置中,则会缺少applicationIdSuffix(此时null)。

有什么提示吗?


如果我将这个添加到什么地方,this是什么? - Blackbelt
@Blackbelt resValue标记。 - Thomas Keller
1个回答

4
应用程序ID后缀的一大优势是你可以在不同的风味和构建类型中使用它。请看这个例子:
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "dev.bmax.suffixtext"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix '.debug'
        }
    }

    productFlavors {
        prod {
            applicationIdSuffix '.prod'
        }
        mock {
            applicationIdSuffix '.mock'
        }
    }
}

现在,当我构建我的“prodDebug”变体时,最终的应用程序ID是“dev.bmax.suffixtext.prod.debug”,如果我正确理解您的问题,这正是您想要的。 编辑
  1. You can access the variant's name in a Gradle task like this:

    applicationVariants.all { variant ->
        // Use 'variant.name'
    }
    
  2. Gradle Android Plugin version 0.14.3 added support of the variant specific BuildConfigField/resValue:

    applicationVariants.all { variant ->
        variant.resValue "string", "name", "value"
    }
    

好的,我不知道applicationIdSuffix也可以在flavor级别上使用,但这实际上并不是我的问题。我希望_resolved_应用程序ID被写入生成的strings.xml中,以便我可以在首选项屏幕XML配置中引用它。 - Thomas Keller
我假设你是在Gradle任务中生成字符串。尝试使用${variant.name}代替'applicationId'。 - dev.bmax
> Could not find property 'variant' on ProductFlavor_Decorated{name=dev, dimension=backend, minSdkVersion=null, targetSdkVersion=null, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptNdkModeEnabled=null, versionCode=null, versionName=null, applicationId=my.app.id, testApplicationId=null, testInstrumentationRunner=null, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}}. - Thomas Keller
如果您将任务包装在以下代码块中,则应该可以访问它:android.applicationVariants.all { variant -> .... } - dev.bmax
好的,我也是这么想的 :) 但是在这个阶段我能操作 resValue 吗? - Thomas Keller
3
太好了!variant.resValue 真的起作用了!而我可以从变体的属性中获取 applicationId,方法如下:variant.resValue "string", "package_name", "${variant.properties.applicationId}" - Thomas Keller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接