Search code examples
androidgradlegroovyandroid-gradle-plugin

Use different assets for APKs and app bundles


I am using Play Asset Delivery with install-time assets which is working just fine with app bundles. However, I still need to build APKs where I need regular assets. In my app's build.gradle, I tried to configure it in this way:

android {
    sourceSets {
        main {
            assets.srcDirs = ['../assets/src/main/assets']
        }
    }
}

Unfortunately, the app bundle then contains this second pair of assets besides the Play Asset Delivery asset, which leads to conflicts. How can I set assets.srcDirs only for APKs but not for app bundles?


Solution

  • The problem can be solved by using two separate product flavors, e.g. apk and bundle. That way it is possible to only add the assets to the sourceSets for the apk flavor:

    android {
        flavorDimensions 'format'
        productFlavors {
            apk {
                dimension 'format'
            }
            bundle {
                dimension 'format'
            }
        }
        sourceSets {
            apk {
                assets.srcDirs = ['../assets/src/main/assets']
            }
        }
    }
    

    The downside of this solution is that you need to make sure to use the apk flavor when building APKs and the bundle flavor when building app bundles.