Blehi
Blehi

Reputation: 2020

Android Gradle merge string resources

I would like to create 2 different APKs (release and debug) using Gradle and I want to use different names for them ('appName' and 'appName debug').

I've found some solutions but it doesn't work for me:
link 1
link 2

I would like to install both apks on the device but I have the following error:

 Duplicate resources: 
 ...\src\release\res\values\config.xml:string/config_app_name,
 ...\src\main\res\values\config.xml:string/config_app_name

If I delete the config_app_name key from the main\res\values\config.xml then Gradle says the the key was not found.

I have 3 manifest files:

...\src\main\AndroidManifest.xml (this uses the `android:label="@string/config_app_name"`)

...\src\release\AndroidManifest.xml

...\src\debug\AndroidManifest.xml

buildTypes {

    debug {
        packageNameSuffix ".debug"
        versionNameSuffix "-debug"
        buildConfig "public static final String PROVIDER_AUTHORITY = \"" + PROVIDER_DEBUG + "\";"
        signingConfig signingConfigs.debug
    }
    release {
        buildConfig "public static final String PROVIDER_AUTHORITY = \"" + PROVIDER_RELEASE + "\";"
        signingConfig signingConfigs.release
    }
}

sourceSets {
    debug {
        java.srcDirs = [
                'src/main/java']
        res.srcDirs = [
                'src/main/res',
                'src/debug/res']
    }

    release {
        java.srcDirs = [
                'src/main/java']
        res.srcDirs = [
                'src/main/res',
                'src/release/res']
    }
}

Is it possible to use one common key (strings, integers, dimens) in the main part of the project and override it in the release/debug part?

Thanks in advance.

Upvotes: 4

Views: 9160

Answers (2)

tidotua
tidotua

Reputation: 43

Look here: http://tools.android.com/tech-docs/new-build-system/resource-merging

You defined multiple resources sources for build type. In such case they have same priority and can cause conflicts.

If you want correct merging you need to create separate main source set. It will have less priority than build types resources.

sourceSets {

    main {
        java.srcDirs = ['src/main/java']
        res.srcDirs = ['src/debug/res']

    }

    debug {
        res.srcDirs = ['src/debug/res']
    }

    release {
        res.srcDirs = ['src/release/res']
    }
}

But with such directory structure it must work out from the box without sourceSets definition.

Upvotes: 1

Blehi
Blehi

Reputation: 2020

I just realized that it was only a naming issue. The name of the release package was misspelled. Sorry for this stupid problem.

Now the merge is working as expected.

Upvotes: 1

Related Questions