Rclass按产品口味配置applicationId后未找到

R class not found after configuring applicationId by product flavors

我的应用程序中有两种风格,我希望每种风格都有不同的 applicationId。这是我的:

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

...

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

AndroidManifest.xml中,我有package="com.mycompany.app",所有代码都在那个包下。

当我 assemble 它带有风味 firstflavor 时,我收到此错误:

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

不应该在com.company.app而不是com.company.app.first下生成R.class吗?

package和application id有什么关系?

如果我删除应用程序 ID 风格配置 (applicationIdSuffix),一切正常。

编辑

我读过这个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.

但是,错误提示找不到 com.mycompany.app.first.R。但是,com.mycompany.app.first 是应用程序 ID,不是包名!这是为什么?

我正在使用 Android 注释 3.3.2

使用文件菜单并使缓存无效并重新启动。

在那里使用无效并重新启动选项。

它将重建索引并解决您的问题。如果这不起作用,也可以从构建菜单中进行干净的构建,工具栏上会有一个 gradle 同步按钮,请使用它。大多数情况下,无效和重新启动应该可以解决您没有任何其他错误的问题

我找到了解决方案 here。正如我所说,我使用的是 Android 注解,所以我需要在 apt 插件中配置包名:

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

一切正常。