Search code examples
javaandroidandroid-productflavorsandroid-build-flavors

Android Flavors - Cannot resolve symbol R


I have two apps in the same codebase. They use the same java files and save res files, except for the values and drawable folders, which have the same files & filenames but different content.

The problem I'm having is that when my Gradle builds, I am having a problem because everywhere that I use R I have an error

Cannot resolve symbol R

My build.gradle file looks like this (with a few settings left out for brevity):

android {
    signingConfigs {
        // left out
    }
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    useLibrary 'org.apache.http.legacy'
    lintOptions {
        disable "ResourceType"
    }
    defaultConfig {
        applicationId = "com.au.testapp"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode = 1
        buildConfigField 'String', 'app_ver_numb', '"1.00"'
        multiDexEnabled = true
    }
    sourceSets {
        main {
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            assets.srcDirs = ['assets']
            res.srcDirs = ['res/main']
            manifest.srcFile 'AndroidManifest.xml'
        }

        flavorone{
            res.srcDirs = ['res/flavorone']
        }

        flavortwo{
            res.srcDirs = ['res/flavortwo']
        }

        instrumentTest.setRoot('tests')
    }

    productFlavors {
        flavorone {
            applicationId "com.au.testapp.flavorone"
            versionName = "''"
            buildConfigField 'String', 'app_id', '"79"'
        }
        flavortwo {
            applicationId "com.au.testapp.flavortwo"
            versionName = "''"
            buildConfigField 'String', 'app_id', '"79"'
        } 
    }

    buildTypes {
        debug {
            productFlavors.flavorone.signingConfig signingConfigs.flavorone
            productFlavors.flavortwosigningConfig signingConfigs.flavortwo 
        }
        release {
            productFlavors.flavorone.signingConfig signingConfigs.flavorone
            productFlavors.flavortwo.signingConfig signingConfigs.flavortwo 

            minifyEnabled true
            shrinkResources true
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
}

My folder structure looks like this:

src
    com
        au
            testapp
                adapter
                utils
                db
                common
                ...
res
    main
        anim
        animator
        ...
        drawable
        layout
        values
        ...
    flavorone
        drawable
        values
    flavortwo
        drawable
        values

When I build this in Gradle I don't get any errors, but when I build the project my java files have errors all through them because they can't find R.. I'm guessing it's to do with the package name not being correct due to my build flavor setup.

Any ideas?


Solution

  • Okay, now that my nitpicking is done, here is my actual attempt at an answer:

    I think the issue lies in the fact that you have your resource directories for each flavor inside of res/. if you look at this post you will see that flavor specific resources should go in the src/flavorName/res directory not in res/flavorname as you have it.

    While you are at it, go ahead and remove the flavor closures from your source sets closure. As long as the directory names match the flavor names and you follow the canonical organization of folders you won't need to define the source sets for flavors.