0

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?

Nickmccomb
  • 1,467
  • 3
  • 19
  • 34

3 Answers3

1

Most of the time it is due to a bad XML file. XML files can be layout files, value files, or the Manifest file.

1) Check your xml files.

2) import R like this import android.R.*; or like this import com.au.testapp.R;

3) Clean the project and rebuild

Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
  • the XML files haven't changed since i built before adding flavors, i'll try your R import and clean and rebuild and see if that works. – Nickmccomb Jun 29 '16 at 03:35
  • try importing this way `import com.au.testapp.R;` – Sathya Baman Jun 29 '16 at 03:37
  • that didn't work. Still have issues with R.. There is no flavorone or flavortwo in my build -> generated -> source -> r – Nickmccomb Jun 29 '16 at 03:47
  • I've voted this up because i know that an error in an xml file can cause issues with R. It's was not the answer i was looking for, but may be helpful for others – Nickmccomb Jun 29 '16 at 05:40
1

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.

Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16
  • i'll give that a go and get back to you soon nitpicker! Thanks for your help so far :) – Nickmccomb Jun 29 '16 at 04:21
  • 1
    No problem. I took more of a look at your question and I think you will also have to pop the contents form res/main into res after you move the flavor resources. (don't forget to update the build.gradle after that). In addition you will have to move your java files for the base app (the ones you have in src previously) into src/main/java. This SO post does a better job than me at explaining how to structure your files for flavors: http://stackoverflow.com/a/20345749/6526330 – Dr. Nitpick Jun 29 '16 at 04:38
  • Ahhh now it makes sense! It's my first day on flavors so i'm still getting used to how it should all be set up. I'm on the right track now, thanks to your help! – Nickmccomb Jun 29 '16 at 04:46
  • Wohoo! Im glad to hear it. Glad I earned a nickname along the way. – Dr. Nitpick Jun 29 '16 at 04:55
0

I don't have enough reputation to comment so I have to answer.

Is your directory in res actually called "flavor1" while the directory name you are using in the build.gradle is called "flavorone"? I noticed that in your post but Im not sure if that is actually the case in your project. It could be causing the issue if so.

Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16