【问题标题】:Drawables not Found Under Custom Build Type在自定义构建类型下未找到可绘制对象
【发布时间】:2023-03-27 03:40:01
【问题描述】:

我已尝试为我的应用程序配置与我的发布配置相同的构建类型,只是它加载了一些用于测试应用程序的示例数据。我执行了以下步骤:

我在应用程序的build.gradle 中使用以下buildTypes 配置:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.config
    }
    debug {
        applicationIdSuffix ".debug"
        versionNameSuffix "-debug"
    }
    sampledata {
        applicationIdSuffix ".sampledata"
        versionNameSuffix "-sampledata"
        signingConfig signingConfigs.config
    }
}

sourceSets {
    main { res.srcDirs = ['src/main/res', 'src/androidTest/res'] }
    sampledata { res.srcDirs = ['src/sampledata/res', 'src/main/res'] }
}

我在src文件夹下创建了对应的源集(文件夹结构)。

当我尝试使用以下语句获取资源时,找不到资源(返回 0):

int resId = context.getResources().getIdentifier("someResource", "drawable", "com.myapp");

我在源集中使用字符串资源没有问题,但我无法在此配置中访问可绘制对象。将它们移动到main 下的drawable 文件夹也不起作用。我错过了什么?

【问题讨论】:

    标签: android android-gradle-plugin android-drawable android-build-type


    【解决方案1】:

    为了访问源集中的资源,包名称必须与构建类型匹配,即包含applicationIdSuffix。将getIdentifier() 语句更改为

    int resId = context.getResources()
        .getIdentifier("someResource", "drawable", "com.myapp.sampledata");
    

    int resId = context.getResources()
        .getIdentifier("someResource", "drawable", context.getPackageName());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2016-12-13
      • 2014-08-21
      • 2016-03-31
      • 2021-01-31
      • 1970-01-01
      相关资源
      最近更新 更多