我为Android创建了一个库,它本身依赖于appcompat
和design
支持库。我通过在我的图书馆的Public.xml
中放置一个虚拟资源来标记我所有的图书馆资源为私有的:
<public name="string_res_that_does_not_exist" type="string"/>
当我将我的库导入到另一个项目时,Android Studio正在使用appcompat
和design
库中的公共资源的行中产生警告,例如:
-
?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声明,所以现在它最终将这些符号公开为声明的,但不是公共的——例如private。
如何防止这些警告显示,使我的库的其他用户不会遇到这些问题?
如果你想让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
在他们的应用程序的资源目录。但希望有人会发现这是有用的,因为它是一个可行的解决方案,这里的主要问题。
2021
我相信现在如果你设置了标志:
android.nonTransitiveRClass=true
在你的gradle.properties
文件中,这将阻止你依赖的库中的资源通过你的库可用。
又名:如果最终用户需要支持库资源,他们需要依赖于支持库