It's awesome! Say goodbye to Java. To develop a full client using Kotlin.

Source: Internet
Author: User

Must be written in front: it takes only a few days to move from Java to Kotlin!

This is the last article of Kotlin developing the Android series tutorial. The previous several introduced the basic syntax of Kotlin, and the differences with Java and so on. In this article, we will use the previous knowledge point to develop a Gank.io work Camp app from scratch (thanks to the API provided by Gank). The app is fully developed using the Kotlin language, based on MVP mode and uses the following mainstream open source framework:

    • Kotlin
    • Dagger2
    • Rxjava
    • DataBinding
    • Retrofit
    • Okhttp3
    • Deeplinkdispatch
    • Gson
    • Glide

In addition to the above mainstream library, also applied to one of my navigation bar slide Hidden library:

    • Byeburger

The project address is Gankclient-kotlin, if you want to learn Kotlin, then this project should give you a lot of help.

The interface style is MD-style, and most controls are content in the design package. The main implementation of a client of the content, interface interaction, information display, data acquisition and so on.

The author has used Kotlin in the development of the project for 2 months, during which there are some pits, but outside the pit, he brings me pleasure , since then. Writing code with Kotlin is a complete pleasure compared to Java. Since some students do not read the previous article, here is a brief introduction of Kotlin.

Kotlin is another JVM-based language developed by JetBrains (your Android studio is his home). If you have a Java foundation, then it's extremely easy for him to get started.

In addition to seamlessly calling Java (all Java classes, Java libraries are not compatible with any processing), one-touch Java to Kotlin, null pointer security, and many more features than Java dick. Here are some chestnuts:

1. Code volume comparison (refer to using Kotlin to develop Android (i)):

Java

    TextView textView = findViewById(R.id.textView);    textView.setText("Hello World");

Kotlin

    textView.text"hello kotlin"

Here is the extension function Display, refer to using Kotlin to develop Android (ii)

The extension function simply means that a class is not dynamically extended by inheritance to add methods, such as the following toast is the extension of the context class.

Java

    Button button = findviewbyid(R.id.button)    button.setOnClickListener(new View.OnClickListener() {        @OverridepublicvoidonClick(View v) {            Toast.makeText(this,"hello java",Toast.LENGTH_SHORT);        }    });

Kotlin

    button.setOnClickListener {toast("hello kotlin")}

Pojo Class (Java bean comparison)

Java

 Public  class User {    PrivateString name;PrivateString ID; Public User(string name, string id) { This. name = name; This. id = ID; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringgetId() {returnId } Public void setId(String ID) { This. id = ID; }}

Kotlin (Do not be frightened, indeed so short!!) )

    class User(varStringvarString)
Chestnuts Complete

Believe to see some of the above code comparison, we have been more clearly in mind kotlin characteristics. The code is at least three times times less than Java. This is great satisfaction!

Develop a full app client

Now, assuming you understand the basic usage of Kotlin, I'll introduce you to the structure of the client app.

Mvp

Manage the View Model presenter interface through the contract class contract (if your project is annoying MVP, then Amway automatically generates the MVP code plugin mvphelper).

    • model– primarily handles business for data acquisition (e.g., network, local cache).
    • view– is used to present data and provide interaction.
    • The bridge between Presenter–view and model, both of which are established by Presenter.

      The main flow is as follows: The user interacts with the view, the view learns that the user needs to load the data, tells the Presenter,presenter to inform the Model,model to get the data in the Prsenter,presenter to give the data to view to display.

      Steal an old picture:

Dagger2

Project, mainly for presenter, model, retrofit API and other classes of injection operations. Here does not do the detailed Dagger2 usage explanation, also not very will the classmate may first read the Great God's tutorial. Dagger2 in the Kotlin with some configuration is not the same, detailed configuration please see the Project source code .

apicomponent

主Component、用于注入AppComponent、便于提供子Component依赖。 依赖于: 1.ApiModule(提供okhttpClient、Retrofit、Api等) 2.AppModule(提供context对象(okhttp拦截器所需))

fuckgoodscomponent

父Component为ApiComponent 用于注入FuckGoodsPresenter 依赖于: FuckGoodsModule(提供FuckGoodsView)

randomcomponent

Rxjava + Retrofit + OKHTTP3

Primarily for network access. These are the mainstream libraries that are used in the same way as Java (except for syntax, of course)

Deeplinkdispatch

Page forwarding is based on routing. can solve multi-person multi-module collaborative jump problem.

Gankclienturi defines some routing rules, URIs, etc.

gankrouter Unified for routing operations

Gson

Parsing operation for JSON.

Glide

Used to load the picture.

Byeburger

Used for navigation bar and hover button slide hide.

Address of this client project:
Https://github.com/githubwing/GankClient-Kotlin

Open source This client reason is to let more people understand kotlin, contact Kotlin, thus use Kotlin, is really good thing dare not to stash. And hope that through the Kotlin can know more like-minded friends, together to discuss the common progress of technology.

If this project can help you learn Kotlin, it is the best thing. Welcome to star.

It's awesome! Say goodbye to Java. To develop a full client using Kotlin.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.