构建Android与摇篮,替换字符串中的每个产品的风味字符串、摇篮、风味、产品

2023-09-05 05:51:43 作者:养了一个月亮

在我构建Android项目,以两个不同的应用免费或付费的。

Before i build android project to two different application paid and free.

昨天我改变了每一个值和字符串,所以我犯了大错。

I changed each values and strings so yesterday I made a big mistake.

所以,我laerning如何使用摇篮打造我的应用程序。

So, I'm laerning how to use gradle to build my app.

我的应用程序有一定的型动物。

My app have some differents.

应用程序名称(只需要添加后缀 - 免费') - >值/ string.xml

app name (just add suffix '-Free') -> values/string.xml

更​​改标志在我*的.java

change flag in my *.java

// signingConfigs是ommited。

// signingConfigs is ommited.

productFlavors{
    free{
        packageName "my.app.free"
        versionCode 20
        signingConfig signingConfigs.freeConfing

        copy{
            from('/res'){
                include '**/*.xml'
            }
            into 'build/res/'

            filter{
                String line -> line.replaceAll("android:label=\"@string/app_name\"", "android:label=\"@string/app_name_free\"")
            }
        }
        copy{
            from('/src'){
                include '**/*.java'
            }
            into 'build/src/'

            filter{
                String line -> line.replaceAll("public static final Boolean IS_FULL_VER = true;", "public static final Boolean IS_FULL_VER = false;")
            }
        }
    }
    paid{
        packageName "my.app.paid"
        versionCode 20
        signingConfig signingConfigs.paidConfing
    }
}

不过,内置的应用程序改变了一切尽在不言中。

but, built app changed nothing at all.

我错过了什么?

推荐答案

查看产品口味的文档:

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors

在你的 build.gradle ,在每一个味道,你可以定义标记在 BuildConfig.java 文件:

In your build.gradle, in each flavor, you can define flags to be generated in your BuildConfig.java file:

productFlavors {
    free {
        packageName "com.company.appfree"
        buildConfig  "public final static com.company.common.MonetizationType monetizationType = com.company.common.MonetizationType.FREE;"
    }

    paid {
        packageName "com.company.apppaid"
        buildConfig  "public final static com.company.common.MonetizationType monetizationType = com.company.common.MonetizationType.PAID;"
    }
}

此示例使用枚举(你需要在你的java code定义的地方):

This example uses an enum (that you need to define somewhere in your java code):

public enum MonetizationType {
    PAID, FREE
}

现在你可以使用这个任何地方是这样的:

You can now use this anywhere like this:

if (BuildConfig.monetizationType == MonetizationType.FREE) { ... } 

有关压倒一切的资源,您可以创建为每个风味源文件夹不同的资源文件:

android字符串赋值导致crash

For overriding resources, you can create different resource files in the source folders for each flavor:

使用以下结构

app/build.gradle
app/ [.. some other files...]
app/src/main/
app/src/main/java
app/src/main/res
app/src/main/assets
app/src/main/AndroidManifest.xml


app/src/free/res/values/apptitle.xml
app/src/paid/res/values/apptitle.xml

apptitle.xml将是一个字符串资源文件(就像strings.xml中),但只有一个字符串:要根据不同的味道是不同的人。 (你不需要在你的主/ res目录一apptitle.xml)。

apptitle.xml would be a string resource file (just like strings.xml), but with only one string: the one you want to be different depending on the flavor. (You don't need have a apptitle.xml in your main/res directory).

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <string name="app_title">App Title (or whatever you want)</string>
</resources>

您可能能够覆盖不同的方式串,但我喜欢保持覆盖字符串的休息清晰区分开来。

You might be able to override strings in different ways, but I like to keep the overridden strings separate from the rest for clarity.