android-gradle-plugin - 使用 AGP 4.2.0 或 7.0 时未为 androidTests 生成 R 文件

标签 android-gradle-plugin gradle-plugin android-viewbinding

我曾尝试迁移到新的 Gradle 插件,但由于我们有一些针对 AndroidTest 的自定义布局,它失败了,并出现错误,提示 Android 测试“未找到 R 类”。 我找不到任何消息来源说明这方面的任何行为变化。

错误看起来像这样,似乎与 View 查找有关,或者只是与未生成 R 类这一事实有关。

/pathToTheApp/build/generated/data_binding_base_class_source_out/debugAndroidTest/out/com/package1/test/databinding/CustomViewActivityBinding.java:12: error: cannot find symbol
import package.name.test.R;
                             ^
  symbol:   class R
  location: package package.name.test

最佳答案

升级到 AGP 7.0 后,我在 Android 测试中遇到了这个问题。找不到我们的测试 R 类。我设法通过将 testApplicationId 复制到看似新的 testNamespace 字段来修复它。

android {

    testNamespace = "com.example.tests"

    defaultConfig {
        testApplicationId "com.example.tests"
    }

}

我认为没有它它应该可以工作,所以这可能是一个插件错误,但是 docs说:

The namespace used by the android test and unit test components for the generated R and BuildConfig classes

关于android-gradle-plugin - 使用 AGP 4.2.0 或 7.0 时未为 androidTests 生成 R 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68652083/

相关文章:

android - 如何将 <merge> 与 View 绑定(bind)一起使用?

android - ViewBinding.getRoot().getLayoutParams() 总是返回 null

Android 访问 onCreate Activity 之外的 View 绑定(bind) val

cordova - 错误 : Could not find gradle wrapper within Android SDK. 可能需要更新您的 Android SDK - Android

java - Gradle 同步失败 : Unable to load class 'org.gradle.internal.logging.LoggingManagerInternal'

intellij-idea - 找不到taskdef类com.sun.tools.xjc.XJCTask

android - gradle 更新到 2.10 后,无法运行应用程序

android - 从eclipse导入android studio出错

android - 签名 apk 中的 ClassNotFoundException

android - Gradle compileOnly和Maven提供