Android开发中不错的库

96
追云_似梦
2016.06.04 11:41* 字数 784

现在做Android应用,有很多不错的开源库可以减少开发中的重复劳动,今天分享一下Android开发中的一些不错的库。

网络模块

【OkHttp+Retrofit+Stetho+RxJava+Glide】
分别涉及网络请求Retrofit+OkHttp,图片加载库Glide,RxJava支持;
另外Stetho主要是便于应用调试,查看一些网络请求数据情况、本地SharePerference等不可见的数据。Glide通过配置Stetho也可以拦截图片加载的地址。具体Gradle配置如下:

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'

日志模块

目前Android的Log显然不够强大,但是Github上的logger开源库基本上可以满足开发中对Log的显示需求,可以定位到行,点击日志链接跳转到相应类。打印日志的线程信息,函数调用的堆栈信息等。Github地址:https://github.com/orhanobut/logger

repositories { 
      // ... maven { 
            url "https://jitpack.io" 
      }
}
dependencies { 
    compile 'com.github.orhanobut:logger:1.12'
}

MVP框架管理模块

现在Android开发不用MVP模式感觉都不好意思说自己是搞Android开发的,首先现在对移动应用开发不仅仅涉及功能,还要应用是基于测试驱动开发。而旧的MVC开发模式不能很好的满足这以需求。而MVP模式能通过Presenter很好的解耦View(Activity/Fragment等) 和 Model层的代码。
下面介绍Nucleus库,以下是它的特性:

  • It supports save/restore Presenter's state in a View/Fragment/Activity's state Bundle. A Presenter can save request arguments into that bundles to restart them later.

  • It provides a facility to direct request results and errors right into a view with just one line of code, so you don't have to write all that != null
    checks.

  • It allows you to have more than one instance of a view that requires a presenter. You can't do this if you're instantiating a presenter with Dagger (a traditional way).

  • It provides a shortcut for binding a presenter to a view with just one line.

  • It provides base View classes: NucleusView
    , NucleusFragment
    , NucleusSupportFragment
    ,NucleusActivity
    . You can also copy/paste code from one of them to make any class you use to utilize presenters of Nucleus.

  • It can automatically restart requests after a process restart and automatically unsubscribe RxJava subscriptions during onDestroy
    .

  • And finally, it is plain simple, so any developer can understand it. (I recommend to spend some time diving into RxJava though.) There are just about 180 lines of code to drive Presenter and 230 lines of code for RxJava support.

之所以不翻译这些特性说明,当然是为了提高大家的英语水平。_你懂得~
简单的说一下,就是这个库可以在应用的Activity/Fragment被系统回收后重启能将一些状态值在Presenter层保存,在下次启动的时候回复保持上次的状态中的一些值。这是个非常好的特性,比如应用横竖屏切换导致当前Activity重新创建。
具体的可以参考这个库的示例代码。

END

好的东西往往比较少,而这些开源库可以在我们平时的开发中让代码更易维护,更易测试。这也就是今天为什么只简单的介绍这么几个不错的开源库的原因。
看看这几个库的源码,一定会有所收获~

艺技回忆录
Web note ad 1