Search code examples
androidandroid-productflavors

R class not found after configuring applicationId by product flavors


I have two flavors in my app and I want a different applicationId for each one. This is what I have:

defaultConfig {
    applicationId "com.mycompany.app"
    //more stuff
}

...

productFlavors {
    firstflavor {
        applicationIdSuffix '.first'
        //more stuff
    }
    secondflavor {
        applicationIdSuffix '.second'
        //more stuff
    }
}

In AndroidManifest.xml, I have package="com.mycompany.app", and all the code is under that package.

When I assemble it with, say, flavor firstflavor, I get this error:

Error:The generated com.mycompany.app.first.R class cannot be found

Shouldn't R.class be generated under com.company.app instead of com.company.app.first?

How is it package related to application id?

If I remove the application id flavor configuration (applicationIdSuffix), everything works properly.

EDIT

I have read this here:

If the BuildConfig.java and R.java files was set to the applicationId rather than the package name, then the code which referenced these would need to have different imports for different build variants – in other words you would need to have separate source sets (with huge amounts of duplication) for your different build flavors in order to access these. This would have some quite profound implications for the maintainability of our code.

However, error says that com.mycompany.app.first.R cannot be found. But, com.mycompany.app.first is the application id, not the package name! Why is that?

I'm using Android Annotations 3.3.2


Solution

  • I found the solution here. As I said, I'm using Android Annotations, so I needed to configure package name in apt plugin:

    apt {
        arguments {
            //more things
            resourcePackageName "com.mycompany.app" //add this line
        }
    }
    

    and everything is working fine.