辅助android开发者搭建基于JetPack组件构建MVVM框架的注解处理框架。通过注解自动生成ViewModel的Factory类、lazy方法等;支持在项目的任意位置注入ROOM的dao层接口与Retrofit库中的api接口。
android开发者
可以将brick
理解为一个轻量级的注入框架,使用非常简单,使用4-6个注解即可工作。brick
主要在编译期工作, 不会在App
运行时产生任何额外的性能消耗 ,并且只有1个注解库会打包到你的android
工程中,不用担心体积增大的问题。
androidx
而非support
库。JetPack
的ViewModel
组件。Retrofit
作为网络请求库。ROOM
数据库框架。(可选)buildscript {
...
ext {
brick_version = '0.2.0'
}
repositories {
...
maven { url 'https://jitpack.io' }
}
dependencies {
classpath "com.gitee.numeron.brick:plugin:$brick_version"
}
}
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
...
apply plugin: 'kotlin-kapt'
apply plugin: 'brick'
...
dependencies {
...
implementation "com.gitee.numeron.brick:annotation:$brick_version"
kapt "com.gitee.numeron.brick:compiler:$brick_version"
}
@Provide
class WxAuthorViewModel: ViewModel() {
...
}
gradlew :[ModuleName]:kaptDebugKotlin
运行脚本;[PrjectName] -> [ModuneName] -> Tasks -> other -> kaptDebugKotlin
并双击运行脚本;Ctrl + F9
编译整个项目。lazyWxAuthorViewModel()
扩展方法,在Activity或Fragment中直接调用即可。get()
方法,在不方便使用lazy方法时,可使用此方法获取ViewModel的实例。lazyWxAuthorViewModel
方法就是对get()
方法的包装。private val wxAuthorViewModel by lazyWxAuthorViewModel()
或在onCreate()之后,通过get创建:
private lateinit var wxAuthorViewModel: WxAuthorViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
wxAuthorViewModel = get(this)
}
-2. (必需) 在获取Retrofit
实例的方法上添加@RetrofitInstance
,如:
@RetrofitInstance
val retrofit: Retrofit by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
Retrofit.Builder()
.client(okHttpClient)
.baseUrl(WANDROID_BASE_URL)
.addConverterFactory(MoshiConverterFactory.create())
.build()
}
val okHttpClient: OkHttpClient by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
val logInterceptor = HttpLoggingInterceptor()
logInterceptor.level = HttpLoggingInterceptor.Level.BODY
OkHttpClient.Builder()
.addInterceptor(logInterceptor)
.callTimeout(15, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.connectTimeout(15, TimeUnit.SECONDS)
.build()
}
注:@RetrofitInstance
注解只能标记在public
修饰的val
属性上或方法上,val
属性上或方法可以在object 单例
或companion object
中,也可以是包级属性/方法。
-1. (可选) 在获取RoomDatabase
实例的属性或方法上标记@RoomInstance
,如:
@RoomInstance
val wandroidDatabase: WandroidDatabase by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
Room.databaseBuilder(CONTEXT, WandroidDatabase::class.java, "wandroid.db")
.build()
}
注:@RoomInstance
注解只能标记在public
修饰的val
属性上或方法上,val
属性上或方法可以在object 单例
或companion object
中,也可以是包级属性/方法。
Retrofit Api
接口和WxAuthorRepo
类interface WxAuthorApi {
@GET("wxarticle/chapters/json ")
suspend fun getWxAuthorList(): List
}
class WxAuthorRepo {
...
}
lateinit var
修饰的WxAuthorApi
字段,并用@Inject
标记:class WxAuthorRepo {
@Inject
lateinit var wxAuthorApi: WxAuthorApi
}
lateinit var
修饰的WxAuthorRepo
字段,并用@Inject
标记:@Provide
class WxAuthorViewModel: ViewModel() {
@Inject
private lateinit var wxAuthorRepo: WxAuthorRepo
}
标记后,继续编写业务代码即可,所有被@Inject
标记的字段,都会在编译期自动获取或创建实例,无需担心它们在何时被赋值。
注:虽然是lateinit var
修饰的字段,但是不要尝试为它们赋值,这会导致致命的错误。
注:@Inject
可以注入的类型只有Retrofit
的api
接口和ROOM
的dao
接口、以及有无参构造的类。
假设有另一个Retrofit api接口,它的访问地址或端口与baseUrl
中的不一样,此时,可以在Retrofit
的api
接口上添加@Port
和@Url
注解来设置它们的url或port。
@Port
的使用:@Port(1080)
interface ArticleApi {
@GET("wxarticle/list/{chapterId}/{page}/json")
suspend fun getArticleList(@Path("chapterId") chapterId: Int, @Path("page") page: Int): Paged
}
添加此注解后,brick会在编译期根据@RetrofitInstance
注解标记的Retrofit
实例和@Port
的端口号,重新创建一个Retrofit
实例,并使用新的Retrofit
实例创建ArticleApi
的实例。
@Url
的使用:@Url("http://www.wanandroid.com:1080/")
interface ArticleApi {
@GET("wxarticle/list/{chapterId}/{page}/json")
suspend fun getArticleList(@Path("chapterId") chapterId: Int, @Path("page") page: Int): Paged
}
与@Port
的使用基本一致,实现的原理也是一样的。
生成的WxAuthorViewModels.kt
文件:
//kotlin 扩展方法,在Activity/Fragment中通过by调用
fun ViewModelStoreOwner.lazyWxAuthorViewModel(): Lazy =
LazyWeChatAuthorViewModel(this)
//包级方法,在Activity/Fragment的onCreate方法之后调用
fun get(owner: ViewModelStoreOwner): WxAuthorViewModel {
val factory = WxAuthorViewModelFactory()
return ViewModelProvider(owner, factory).get(WxAuthorViewModel::class.java)
}
private class WxAuthorViewModelFactory : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun create(clazz: Class): VM = WxAuthorViewModel() as VM
}
private class LazyWxAuthorViewModel(
private val owner: ViewModelStoreOwner
) : Lazy {
private var _value: WxAuthorViewModel? = null
override val value: WxAuthorViewModel
get() {
if(_value == null) {
_value = get(owner)
}
return _value!!
}
override fun isInitialized(): Boolean = _value != null
}
反编译后的WxAuthorViewModel.class
:
class WxAuthorViewModel extends ViewModel {
private final WxAuthorRepo wxAuthorRepo = new WxAuthorRepo();
}
反编译后的WxAuthorRepo.class
:
class WxAuthorRepo {
private final WxAuthorApi wxAuthorApi = RuntimeKt.getRetrofit().create(WxAuthorApi.class);
public final WxAuthorApi getWxAuthorApi() {
...
return wxAuthorApi;
}
}
WxAuthorApi
添加@Port
注解后的WxAuthorRepo.class
:
class WxAuthorRepo {
private final WxAuthorApi wxAuthorApi = newRetrofit(RuntimeKt.getRetrofit(), 1080, null).create(WxAuthorApi.class);
public final WxAuthorApi getWxAuthorApi() {
...
return wxAuthorApi;
}
private final Retrofit newRetrofit(Retrofit retrofit, int port, String url) {
if (port > 0) {
HttpUrl httpUrl = retrofit.baseUrl().newBuilder().port(port).build();
return retrofit.newBuilder().baseUrl(httpUrl).build();
} else if(url != null && url.length() != 0) {
return retrofit.newBuilder().baseUrl(url).build();
}
return retrofit;
}
}
通过反编译class
后的代码以及整篇文章后可以得出一个大概的结论:brick
就是在java
编译成class
后,class
编译成dex
之前,对class
的字节码进行修改,给@Inject
标记的字段赋值,实现的注入框架。
目前对ViewModel
的注入还需要手动调用生成的方法来初始化,这在编译代码之前,AS上都会有红色的错误标记,接下来,让@Inject
支持ViewModel
的创建就是主要工作啦,等完成后,再发文章吧。