As a developer, it’s important to avoid reinventing the wheel so that you can spend a lot of time focusing on developing your business. 2018 is over. Here are a few frameworks that have been highly recommended in the industry since the beginning of this year, hoping to improve your development efficiency.
Retrofit
Retrofit is an HTTP client that defines REST apis as interfaces. It makes it easy and concise to manage request headers, Body, and parameters. Retrofit also supports synchronous and asynchronous calls
interface ApiService {
@GET("movie/{id}")
fun getMovieDetails(@Path("id") id: String) : Call<MovieDetails>
}
Copy the code
Of course, it also supports Rx, and if you’re used to RxJava, it can provide the same chained calls.
Moshi
Moshi is a library that converts JSON to Java or Kotlin objects. Many people compare Moshi to GSON3.0, but Moshi has more advantages than GSON. It’s faster, Kotlin compliant, and can throw exceptions in a controlled way. This is Kotlin code with Moshi for conversion
val moshi = Moshi.Builder().build()
val jsonAdapter = moshi.adapter(Model::class.java)
/* JSON to Model */
val model = jsonAdapter.fromJson(json)
/* Model to JSON */
val json = jsonAdapter.toJson(model)
Copy the code
Chuck
Chuck is an HTTP sniffing tool that listens for HTTP requests from your application and displays HTTP logs in the notification bar. You can click on Chuck in the notification bar to access Chuck’s screen to see the full HTTP request log. The test team can also use Chuck to verify whether a problem is Android – or server-side. Chuck can sometimes do more than replace logs, because the app may lose logs after restarting, and Chuck will keep them.
Glide
Android’s system apis for handling images are notoriously unfriendly. OOM often occurs even when zooming images. Glide is an image processing library that provides a very simple and convenient API for developers to manipulate images at will.
GlideApp.with(context)
.load(url)
.centerCrop()
.placeholder(R.drawable.ic_placeholder)
.into(imageView)
Copy the code
Glide makes it very easy to load images from urls, and it also provides callbacks, caching, and scaling. It would be very painful to implement these features without Glide. Glide supports even simple image morphing, such as processing into circles.
ThreeTen
ThreeTen is a Library that handles dates and times. It meets the JSR-310 standard and has been integrated into the java.time.* package as a standard library in Java8. It took off because the standard Android Calendar API was very unfriendly. A ThreeTen demo code looks like this
/* Current DateTime */
LocalDateTime.now()
/* String to DateTime */
val localDateTimeString = "2011-12-03T10:15:30"
val localDateTime = LocalDateTime.parse(localDateTimeString)
Copy the code
ThreenTen is smaller and has fewer methods than JodaTime, another library that handles dates and times. ThreeTen’s API is also cleaner. However, if your program already uses JodaTime, switching to ThreeTen is not recommended, and if it is a new program with A Haul, ThreeTen is strongly recommended.
Timber
Timber is a Log library based on Andorid Log. Android’s log functionality is not strong enough, and many projects have developed their own log tools. Timber can easily turn logs on and off. It also supports string log formatting and exception handling. Many open source projects have taken this advantage of Timber and used it as a core library.
/* Log error */
Timber.e("Error happened")
/* Log warning with params */
Timber.w("Your variable value is %s", variable)
/* Log exception */
try {
.
} catch (e: Exception) {
Timber.e(e)
}
Copy the code
Room
Room is the official Android ORM framework. ORM is an object-oriented approach to data/database processing technology. Room offers a nice API similar to Retrofit. It also relies on annotations and standard SQL syntax.
@Dao
interface MovieDao {
@Query("SELECT details FROM movies WHERE id = :id")
fun getMovieDetails(id: String): MovieDetails
}
Copy the code
Room also supports Rx and LiveData, so you can use it however you want. In contrast to other ORM frameworks, Room doesn’t need to read complex documentation to understand how to use it, and it doesn’t have a complex API. This is its advantage.
RxJava
RxJava is a Java implementation of the ReactiveX API, which replaces the original Java programming thinking with chain calls. Developers would prefer to see the UI updated in real time, in other words, they would prefer to see the UI updated automatically as the data changes. When we’re dealing with data, things can get complicated with thread switching and errors, and calls between multiple apis. RxJava handles this situation well. While RxJava isn’t easy to get started with, it’s really worth the time to learn. When RxJava is combined with Kotlin, the benefits of both are magnified and developers will love it.
-
Single .zip(
-
/* Execute first API call and retry twice if it fails */
-
apiService .getMovieActors( id).retry (2),
-
/* Execute second API call and retry twice if it fails */
-
apiService .getMovieDetails( id).retry (2),
-
/* Receive successful results and merge them into single model */
-
BiFunction
, MovieDetails, Movie> { actors , details -> Movie( details, actors ) }
-
)
-
/* Execute API calls on IO thread */
-
.subscribeOn( Schedulers.io ())
-
/* Receive results on MainThread */
-
.observeOn( AndroidSchedulers.mainThread ())
-
.subscribeBy(
-
onError = { /* Handle error */ },
-
onSuccess = { /* Handle full movie data */ }
-
)
Copy the code
The above code can be tried in Java and you’ll go crazy.
AndroidKTX
Android KTX is a library that wraps Android apis in Kotlin. The goal of this library is to make the Android API more user-friendly. It has ported many of the nice features from Kotlin, including named parameters, lambda expressions, and default parameter values.
/* Display View */
view.isVisible = true
/* Apply padding to all sides */
view.setPadding(padding)
/* Update padding on any side */
view.updatePadding(left = newPadding, right = newPadding)
/* Quick Toast */
toast("Display test toast")
/* New way to create bundle */
bundleOf {
"key1" to item1
"key2" to item2
}
/* Better way to use SharedPreferences */
sharedPreferences.edit {
putString("key1", "value1")
putString("key2", "value2")
}
Copy the code
Android KTX supports many extensions, and the only way to learn more is to read the source code to see how it works. KTX changes the way developers use the Android API, and if your project uses Kotlin, it should be one of your core libraries.
Dagger
No leaderboard is complete without Dagger. Dagger is a compiler-injected dependency injection framework. Like RxJava, it’s not easy to understand, but once you get used to it, it’s a huge benefit to your project. For Dagger, Google has a famous example of a coffee maker that CAN be seen. I have posted several analysis articles before, so you can see Dagger2 app: In general, dependency injection is a way to make small modules available to other modules with minimal effort. Simply take a car as an example. You can easily change the type of tyre without making any changes to the car itself. For large projects, developers don’t want to handle dependency injection manually because it’s not easy to maintain. Dagger does this for the developer through annotations. Dagger has been widely used in large server projects before. Last year, Google incorporated Dagger into Android project as one of the modules of Android.
==== Sand sculpture today ====