Home > OS >  Using different res values in a multi-flavor android library uses wrong values in compiled APK when
Using different res values in a multi-flavor android library uses wrong values in compiled APK when

Time:12-09

I've discovered an issue in an android project which uses separate library modules. The project uses multiple product flavors and those flavors are reflected in each of the libraries.

One of the libraries uses the resValue function to generate different string values based on the flavor.

However, there is an issue that happens when assembling multiple flavors - it seems like the apks generated after the first one get assigned the resource value of the first.

Here is an example repo that shows the problem

Relevant part of App gradle file:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    flavorDimensions("environment")

    defaultConfig {
        dimension = "environment"
    }

    productFlavors {
        create("dev")
        create("ppe")
    }
}

dependencies {
    implementation(project(":testlib"))
}

Relevant part of library gradle file:

plugins {
    id 'com.android.library'
    id 'kotlin-android'
}

android {
    flavorDimensions("environment")

    defaultConfig {
        dimension = "environment"
    }

    productFlavors {
        create("dev") {
            resValue("string", "aaa_lib_test_gradle", "dev")
        }
        create("ppe") {
            resValue("string", "aaa_lib_test_gradle", "ppe")
        }
    }
}

With this configuration if you run ./gradlew clean assemblePpeDebug assembleDevDebug and inspect the generated dev APK you'll notice that the value of aaa_lib_test_gradle is ppe, not dev as expected.

What is causing this - is there some missing gradle setting I'm not using that I'm supposed to? For reference I am using AGP 7.0.3 and gradle 7.0.2

UPDATE: I found out this only happens when caching is set to true in the gradle properties org.gradle.caching=true. Setting it to false produces expected results.

UPDATE 2: This apparently is an issue with AGP 7.x tracked here: https://issuetracker.google.com/issues/201930057

CodePudding user response:

Apparently this was an issue with older 7.0 versions of AGP. It was addressed in 7.0.4. Can confirm the issue doesn't happen when using that version.

Issues relating to this: https://issuetracker.google.com/issues/201930057 https://issuetracker.google.com/issues/196852190

CodePudding user response:

I think for that you need to use

sourceSets {
    dev {
      res.srcDirs = ['src/dev/res']
    }
}
  • Related