访问依赖于其他库的Android库的公共资源会产生警告,表明这些资源被标记为私有

时间:2016-10-08 09:48:07

标签: android android-studio aar

我为Android创建了一个库,它本身依赖于appcompatdesign支持库。我通过在我的库中Public.xml放置一个虚拟资源,将我所有的库资源标记为私有:

<public name="string_res_that_does_not_exist" type="string"/>

当我将库导入另一个项目时,Android Studio会在使用appcompatdesign库中的公共资源的行中生成警告,例如:

  • ?colorPrimary
  • ?colorPrimaryDark
  • ?colorAccent
  • ?selectableItemBackground

警告说The resource @attr/<resource-name> is marked as private in <my-library-name>

此问题似乎与我的问题有关: https://code.google.com/p/android/issues/detail?id=183120

  

这里的根本原因是当一个库依赖于另一个库时,它会将所有依赖的库中的所有资源导入到它自己声明的R.txt(在AAR文件中)。但是,它并没有包含来自这些依赖项的public.txt声明,所以现在它最终将这些符号公开为声明但不公开 - 例如私有的。

如何防止这些警告显示,以便我图书馆的其他用户不会遇到这些问题?

1 个答案:

答案 0 :(得分:0)

如果您想将String值设为私有您的图书馆项目,可以执行以下操作:

<强>实施例

// This annotation will restrict everything in this class to the library project.
@RestrictTo(RestrictTo.Scope.LIBRARY)
public final class MyPrivateStrings
{
    public static final String SUPER_SECRET_STRING = "I wet my pants in the 6th grade,"
                   + "this information can’t escape the library!";

    private MyPrivateStrings()
    {

    }
}

修改:这可能不是OP正在寻找的完全答案,因为他们说他们要将Strings保留在他们应用的资源目录中。但希望有人会觉得这很有用,因为这是解决主要问题的可行方案。