【问题标题】:How to referencing other (multi)module's resouce on test code[in Android-Studio or IntelliJ]如何在测试代码中引用其他(多)模块资源[在 Android-Studio 或 IntelliJ 中]
【发布时间】:2022-06-22 22:45:10
【问题描述】:

android 项目目录

my-app/
├─ app/
 │  ├─ test/ <- test here
 │  ├─ main/
 │  ├─ androidTest/ <- test here
├─ common_res/
 │  ├─ main/
 │  ├─ res/
 │      ├─ values/ <- reference resources
        … other resources   
…

在 IDE(在 Android-Studio 中),无法引用字符串或可绘制资源等资源。 它显示红色标志

(但单元或 UI 测试效果很好)

我尝试如下但失败了

(app/build.gradle) 
dependencies {

    ...
    implementation project(":common_res")
    testImplementation project(":common_res")
    androidTestImplementation project(":common_res")
    
}

并尝试了 sourceSet(in app/build.gradle) 的方式,但我没有实现我的目标。

(我对gradle sourceSet不太了解)

截图

dir 或 files 中的所有红色标志都有引用资源。

如何解决这个问题?

【问题讨论】:

    标签: android android-studio gradle intellij-idea android-testing


    【解决方案1】:

    我对此问题没有任何解决方案(设置)。

    在找到解决方案之前就这样使用。

    基本使用

    如果在应用资源中使用R.id.[something],在相同的测试代码中。

    getString([my-app].common_res.R.string.[something])
    

    或不在应用资源中使用

    import [my-app].common_res.R
    ...
    getString(R.string.[something])
    

    【讨论】:

      【解决方案2】:

      在 google tracker 上报告了同样的问题:https://issuetracker.google.com/issues/236727043

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-09
        • 1970-01-01
        • 2021-06-09
        • 2016-04-24
        • 2011-03-18
        • 1970-01-01
        • 2016-09-19
        相关资源
        最近更新 更多