简体   繁体   中英

Android: MVVM and separation of concerns

I'm almost finishing migrating my app to MVVM with databinding and livedata (still java though) and now I have much more than a decent architectured Android app (which I'm showing below). I'm happy with that, but would like to go one step further.

Talking about clean architecture, I'm trying to figure out how to do a proper separation of concerns in Android (database, business, services, etc).

I work in .net and in that platform, what you do to separate layers is to create a different proyect for each layer (database, bussiness, presentation) and then you reference them in the correct order, but projects are mostly independent one of the others.

In Android, and as far as I know, you have an app module and even though I have a nice package agrupation, all is "together" into the same project.

I'm not sure if this is the best approach to really follow clean architecture principles. I've heard about Dagger, heard you can create modules with it, but not sure if it is intended for what I'm trying to do.

Any help/hints about a good way to implement separation of concerns in Android?

My current app structure:

com
    xxx
        xxx
            dto
                class_1_dto.java
                ...
                class_N_dto.java
            helpers
                helper_http.java
                helper_json.java
                helper_utils.java
                helper_enum.java
                helper_file.java
                helper_smtp.java
                helper_date.java
                ...
            model
                model_class_1
                model_class_2
                ...
                all_model_classes_linked_to_AWS_database
            poco
                some_poco_classes
            repository
                aws
                    IAWSDAO
                    AWS_Repository
                    ...
                    all_stuff_related_to_AWS_database_query
                local
                    model_class_1_repo
                    model_class_2_repo
                    ...
                    all_stuff_related_to_SQLite_database_query
            services
                model_class_1_serv
                model_class_2_serv
                ...
                all_stuff_related_to_local_repos_query
            ui
                activities
                    activity_1
                    activity_1_viewmodel
                    activity_2
                    activity_2_viewmodel
                    ...
                    activity_N
                    activity_N_viewmodel
                component
                    custom_view_1
                    custom_view_2
                    ...
                helpers
                    view_helper_1
                    ...
                    view_helper_N
                assets
                res
                ...

You can segregate your concerns like (app, core, network, service, repository) by making multiple modules. Just like 'app' is a module, you can create an independent module for each concern and you can use Koin for dependency injection between the modules.

For reference here is an example github repo:

https://github.com/Fahad-github/Bykea-CaseStudy-MusicApp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM