提供者消费者_新的资源提供者

提供者消费者

If you’ve been following my work at all, you know that I’m obsessed with Passive View MVP software architectures, and that I’m constantly looking for new and better ways to implement them. Enter ResourceProvider: The Annotation Processor I wrote to generate agnostic resource management APIs for Android app development.

如果您一直在关注我的工作,那么您会知道我对Passive View MVP软件体系结构很着迷,并且我一直在寻找实现它们的更好的新方法。 输入ResourceProvider :我编写的注释处理器, 用于为Android应用程序开发生成不可知的资源管理API

Lots of you might argue that setting resources can just be a responsibility that you delegate to the View class in your MVP system, but logic around which strings and images go where needs to go into the Presenter, and that can lead to writing lots of View-class APIs with names like setErrorTextForInvalidAmount(), which can be tedious and time consuming.

许多人可能会争辩说,设置资源只是您在MVP系统中委派给View类的责任,但是围绕字符串和图像进入需要放置到Presenter中的逻辑,这可能导致编写大量View名称类似于setErrorTextForInvalidAmount()的类API,这可能很乏味且耗时。

Instead, ResourceProvider looks at your R.java file, and generates a few helpful classes that contain all the APIs you could ever want, based on the options you set. That way, your presenter can affectively handle resource-related logic, without being polluted with an Android Context instance. The processor will generate APIs just for strings, or strings and drawables, or any combination of resources that you want. Or… well… it used to do that.

相反,ResourceProvider会查看您的R.java文件,并根据您设置的选项生成一些有用的类,其中包含您可能想要的所有API。 这样,您的演示者可以有效地处理与资源相关的逻辑,而不会受到Android Context实例的污染。 处理器将仅针对字符串,字符串和可绘制对象或所需资源的任意组合生成API。 或者……好吧……它曾经这样做过。

Image for post
Perfect, every time.
完美,每次。

Ok, so I didn’t break it, so much as not update it, and then everything else broke it. Well, not everything, so much as specifically the Android Gradle Plugin. As it turns out, in version 3.6.0 of the AGP, they stopped generating the R.java file, and started generating the R class as bytecode directly, and making a JAR of it. From the release notes:

好的,所以我没有破坏它,以至于没有更新它,然后其他所有东西都破坏了它。 嗯,不是全部,而是Android Gradle插件 。 事实证明,在AGP的3.6.0版本中,他们停止生成R.java文件,并开始直接将R类作为字节码生成,并对其进行JAR。 从发行说明中

Faster R class generation for library projects: Previously, the Android Gradle plugin would generate an R.java file for each of your project's dependencies and then compile those R classes alongside your app's other classes. The plugin now generates a JAR containing your app's compiled R class directly, without first building intermediate R.java classes. This optimization may significantly improve build performance for projects that include many library subprojects and dependencies, and improve the indexing speed in Android Studio.

为图书馆项目更快地生成R类:以前,Android Gradle插件会为项目的每个依赖项生成一个R.java文件,然后将这些R类与应用程序的其他类一起编译。 现在,该插件会直接生成一个JAR,其中包含您应用程序的已编译R类,而无需首先构建中间R.java类。 此优化可以显着提高包含许多库子项目和依赖项的项目的构建性能,并提高Android Studio中的索引编制速度。

You can still force R.java generation, but that’ll be going away soon, so instead I decided to:

您仍然可以强制生成R.java,但这很快就会消失,因此我决定:

将整个内容重写为Gradle插件 (Rewrite the Whole Thing As a Gradle Plugin)

Yup, I started over from scratch, and I’m happy to say that, as of today, you can start using the new Resource Provider plugin in your Android MVP projects with a few quick additions to your build script. Hopefully soon I’ll be able to publish the source, but until then you can start playing with it by adding these lines to the top of your build.gradle file:

是的,我从头开始,很高兴地说,从今天起,您可以开始在Android MVP项目中使用新的Resource Provider插件,并对构建脚本进行一些快速补充。 希望不久后我将能够发布源代码,但是在此之前,您可以通过将以下行添加到build.gradle文件的顶部来开始使用源代码:

plugins {
    id 'com.xfinity.resourceprovider' version '1.0.0'
}

After that, you just need to tell ResourceProvider the Android application id. This is the package name specified either in your AndroidManifest.xml or your build.gradle. I like to do this by setting up an ext variable for it, and then using it everywhere:

之后,您只需要告诉ResourceProvider Android应用程序ID。 这是您在AndroidManifest.xml或build.gradle中指定的包名称。 我喜欢通过为它设置一个ext变量,然后在各处使用它来做到这一点:

ext {
    appId = 'com.xfinity.resourceprovider.sample'
}


resourceprovider {
    applicationId = project.appId
    generateStringProvider = false
    generateColorProvider = false
    generateDrawableProvider = false
    generateDimenProvider = false
    generateIdProvider = false
}


android {
    defaultConfig {
        applicationId appId
    }
}

All of the generateXProvider variables shown above are optional and default to true. But to cut down on the method count, if there are any you don’t need, you can just turn them off.

上面显示的所有generateXProvider变量都是可选的,默认为true。 但是为了减少方法数量,如果不需要任何方法,可以将其关闭。

The new ResourceProvider plugin will generate the exact same code as the old annotation provider, so if you’re using it currently, you can just drop in the replacement and you’ll be functioning again with AGP version 3.6.0 and above.

新的ResourceProvider插件将生成与旧的注释提供程序完全相同的代码,因此,如果您当前正在使用它,则只需插入替换项,即可在AGP 3.6.0及更高版本中再次使用。

For now leave any questions or issues in the comments, and I’ll try to get a new repo published soon.

现在,在评论中留下任何疑问或问题,我将尝试尽快发布新的存储库。

更新资料 (Update)

The RP2 github repo is now available, as is version 1.1.0, which includes some bug fixes and support for Kotlin projects

RP2 github存储库以及1.1.0版本现已可用,其中包括一些错误修复以及对Kotlin项目的支持

https://github.com/Comcast/resourceprovider2

https://github.com/Comcast/resourceprovider2

翻译自: https://medium.com/swlh/the-new-resourceprovider-9ad444d78be0

提供者消费者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值