在产品风味中获取 gradle 构建类型

Sti*_*oni 5 android build gradle build.gradle android-gradle-plugin

我需要根据使用的产品风味创建不同的应用程序名称。

虽然通过简单地设置字符串资源很容易,但我不能再这样做了,因为当应用程序上传到 hockeyapp 时,应用程序名称设置为“@string/app_name”而不是 app_name 的值。

通过将清单中的标签设置为 '${applicationName}' 并将值设置为

manifestPlaceholders = [ applicationName : appName ];
Run Code Online (Sandbox Code Playgroud)

在产品风味块中,以便在编译时设置该值。

当我尝试将构建类型附加到应用程序名称时,问题就出现了。我似乎无法找到一种方法来了解当前在产品风味中使用的构建类型。

这是一个精简版本的构建以提高可读性

android {
    buildVersionName "1.0.0

    buildTypes {
        release {
            ... nothing special
        }
        uat {
            signingConfig signingConfigs.debug
            buildType = "uat"
            applicationIdSuffix = "." + buildType
        }
        debug {
            signingConfig signingConfigs.debug
            buildType = "uat"
            applicationIdSuffix = "." + buildType
        }
    }
    productFlavors{
        flavor1{
            def appName = "app name " + buildType;
            manifestPlaceholders = [ applicationName : appName ];
            applicationId [id]
            def clientIteration = [client iteration]
            versionName buildVersionName + clientIteration
            versionCode [version code]
        }
        flavor2{
            ... same as above with different app name
        }
        flavor3{
            ... same as above with different app name
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码工作正常,除了变量“buildType”始终是最后一个构建类型(在本例中为 debug),这意味着应用程序名称始终在末尾具有 debug。

可能值得注意的是,我不需要在发布的应用程序名称末尾附加任何内容。

Iva*_*vic 5

您可以附加这样的值

   android {

    productFlavors {
        Foo {
             applicationId "com.myexample.foo"
             manifestPlaceholders = [ appName:"Foo"]
        }

        Bar {
             applicationId "com.myexample.bar"
             manifestPlaceholders = [ appName:"Bar"]
        }
    }

    buildTypes {
        release {
            manifestPlaceholders = [ appNameSuffix:""]
        }

        debug {
            manifestPlaceholders = [ appNameSuffix:".Debug"]
            applicationIdSuffix ".debug"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并在清单中

<application
        android:label="${appName}${appNameSuffix}"
        ...
 </application>
Run Code Online (Sandbox Code Playgroud)

如果您想根据构建类型访问不同的值,您可以这样做

buildTypes {
    debug{
        buildConfigField "String", "Your_string_key", '"yourkeydebugvalue"'
        buildConfigField "String", "SOCKET_URL", '"some text"'
        buildConfigField "Boolean", "LOG", 'true'
    }
    release {
        buildConfigField "String", "Your_string_key", '"yourkeyreleasevalue"'
        buildConfigField "String", "SOCKET_URL", '"release text"'
        buildConfigField "Boolean", "LOG", 'false'

    }
}
Run Code Online (Sandbox Code Playgroud)

并使用构建变体访问这些值:

 if(!BuildConfig.LOG)
      // do something with the boolean value
Run Code Online (Sandbox Code Playgroud)

或者

view.setText(BuildConfig.yourkeyvalue);
Run Code Online (Sandbox Code Playgroud)


and*_*ast 3

我知道我参加聚会有点晚了,但如果你想要根据口味不同的名字,你应该有这样的东西:

  productFlavors{
    flavour 1 {
        applicationId "your_app_id"
        resValue "string", "app_name", "Flavour 1 app name"
        .......
    }

    flavour 2 {
        applicationId "your_app_id"
        resValue "string", "app_name", "Flavour 2 app name"
        .......

    }
}
Run Code Online (Sandbox Code Playgroud)

并在您的 AndroidManifest.xml 中:

    android:label="@string/app_name"
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。