Gil*_*eig 5 android annotation-processing android-gradle-plugin
I have an annotation processing library that generates RecyclerView adapters in compile time.
I'm currently rebuilding it from the ground up with many changes and improvements, but while testing, I received a warning stating:
Resource IDs will be non-final in Android Gradle Plugin version 7.0, avoid using them as annotation attributes
This is a problem because it means I won't be able to use R.layout variables in annotations.
I currently use it to associate the layout file's integer value with the R class variable name; this is to locate the layout file from the resource folder and later call inflate(layoutResId).
Currently, I solve this issue like so
Given a simple ViewHolder annotation.
annotation class ViewHolder(val layoutResId: Int)
Run Code Online (Sandbox Code Playgroud)
With the usage
@ViewHolder(R.layout.sample)
data class Sample(val text: String) : GencyclerModel
Run Code Online (Sandbox Code Playgroud)
And the Generated R.layout class.
public final class R {
public static final class layout {
public static final int sample = 567541283;
}
}
Run Code Online (Sandbox Code Playgroud)
When processing the ViewHolder annotation, the processor would receive the integer value 567541283.
In the first processing cycle, the processor would analyze the R class and create a table to map the integer to the layout name, in this case, 567541283 <-> sample.
With that info, I can iterate over the layout resource directory and find the layout file with the name sample.xml.
and I can also later call inflate(R.layout.sample)
The field will be non-final in the new version, thus throw a compile-time error.
An annotation argument must be a compile-time constant.
(Butterknife solution) Creating a duplicate R class that will generate the R.layout variables as static final, thus removing my R class dependency.
(AndroidAnnotations 解决方案)。使用字符串而不是Resources类。我不太喜欢这个解决方案,因为如果布局被重命名或拼写错误,它会导致问题。
我不确定我对这两者的感觉有多开心,但老实说,我看不到其他解决方法。
如果有人有更好的方法来解决这个问题,我很想听听,如果没有,您更喜欢哪种解决方案?
谢谢
如果你想贡献,我正在链接我在 GitHub 项目中打开的问题。
另一个解决方案是向 GencyclerModel 添加一个方法,该方法返回布局引用并从注释中删除该引用。我想这种方法会造成的唯一问题是您检查编译器中布局文件是否存在的条件。但是使用这种方法,您不会在注释中硬编码布局引用,而是可以从每个模型内的方法获取它们
| 归档时间: |
|
| 查看次数: |
334 次 |
| 最近记录: |