简体   繁体   English

用gradle构建android,替换每个产品的字符串味道

[英]Build android with gradle, replace string each product flavor

Before i build android project to two different application paid and free. 之前我建立Android项目到两个不同的应用程序付费和免费。

I changed each values and strings so yesterday I made a big mistake. 我改变了每个值和字符串,所以昨天我犯了一个大错。

So, I'm laerning how to use gradle to build my app. 所以,我是如何使用gradle来构建我的应用程序的。

My app have some differents. 我的应用程序有一些不同。

  1. app name (just add suffix '-Free') -> values/string.xml 应用名称(只需添加后缀'-Free') - > values / string.xml

  2. change flag in my *.java 在我的* .java中更改标志

// signingConfigs is ommited. // signedConfigs是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. 但是,构建的应用程序根本没有改变。

What i missed? 我错过了什么?

See the documentation on product flavors : 请参阅有关product flavors的文档:

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

In your build.gradle , in each flavor, you can define flags to be generated in your BuildConfig.java file: build.gradle ,在每个flavor中,您可以定义要在BuildConfig.java文件中生成的标志:

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;"
    }
}

This example uses an enum (that you need to define somewhere in your java code): 此示例使用枚举(您需要在Java代码中的某处定义):

public enum MonetizationType {
    PAID, FREE
}

You can now use this anywhere like this: 你现在可以在任何地方使用它:

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

For overriding resources, you can create different resource files in the source folders for each flavor: 对于覆盖资源,您可以在源文件夹中为每个flavor创建不同的资源文件:

Use the following structure 使用以下结构

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 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. apptitle.xml将是一个字符串资源文件(就像strings.xml一样),但只有一个字符串:根据风格你想要的不同。 (You don't need have a apptitle.xml in your main/res directory). (您不需要在main / res目录中有apptitle.xml)。

<?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. 您可能能够以不同的方式覆盖字符串,但为了清楚起见,我希望将重写的字符串与其余字符串分开。

The accepted answer is not working with the newer versions of Gradle. 接受的答案不适用于较新版本的Gradle。 You need to replace buildConfig with buildConfigField to get the same result: 您需要使用buildConfigField替换buildConfig以获得相同的结果:

productFlavors {
free {
    packageName "com.company.appfree"
    buildConfigField  "com.company.common.MonetizationType", "MONETIZATION_TYPE",  "company.common.MonetizationType.FREE"
}

paid {
    packageName "com.company.apppaid"
    buildConfigField  "com.company.common.MonetizationType", "MONETIZATION_TYPE",  "company.common.MonetizationType.PAID"
}

} }

treesAreEverywhere's answer (as well as user name) is right on. treesAreEverywhere的答案(以及用户名)是正确的。 But it's also very valuable to know that Android Studio will greatly simplify the process of creating build flavor or attribute specific resource files. 但是,知道Android Studio将极大地简化创建构建风格或属性特定资源文件的过程也非常有价值。

In the AS project window, right click on the res/values folder and select New > Values resource file. 在AS项目窗口中,右键单击res / values文件夹,然后选择New> Values resource file。 Then name it (eg, "strings"), select the Source set if not the default, and select any desired qualifiers (eg, Screen Width = 800). 然后命名它(例如,“字符串”),如果不是默认值,则选择源集,并选择任何所需的限定符(例如,屏幕宽度= 800)。 This is the easiest way to make sure you're putting your resource overrides where the compiler wants them. 这是确保将资源覆盖放在编译器需要的地方的最简单方法。

This is how I did in my project. 这就是我在项目中的表现。 I created multiple build types instead of flavours. 我创建了多种构建类型而不是flavor。 This solution will add a prefix to your package name based on what build type you are trying to assemble. 此解决方案将根据您尝试组装的构建类型为您的包名添加前缀。 For ex, for dev the package name will be com.sample.myapp.dev and similarly for beta package name will be changed to com.sample.myapp.release. 例如,对于dev,包名称将为com.sample.myapp.dev,类似地,beta包名称将更改为com.sample.myapp.release。 You can tweak it to get Free and Paid prefixes. 您可以调整它以获得免费和付费前缀。 Hope it would help. 希望它会有所帮助。

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.production
        applicationIdSuffix '.release'
        versionNameSuffix '-RELEASE'
    }

    dev {
        signingConfig signingConfigs.debug
        applicationIdSuffix '.dev'
        versionNameSuffix '-DEV'
    }

    beta {
        signingConfig signingConfigs.debug
        applicationIdSuffix '.beta'
        versionNameSuffix '-BETA'
    }

    debug {
        signingConfig signingConfigs.debug
        applicationIdSuffix '.debug'
        versionNameSuffix '-DEBUG'
        debuggable true
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM