Git Product home page Git Product logo

googlearchitecturedemo's Introduction

GoogleArchitectureDemo

谷歌最新MVVM架构,基于dataBinding、lifecycle、retrofit2、rxjava2、okhttp、fresco。

简书博客地址:http://www.jianshu.com/p/c0988e7f31fd

两个定制app的首页UI图:

app_universal app_specific
Activity组建为主,点击会跳转到不同组建库的Activity 通过ARoyter获取不同组建库的Fragment来展示
Alt text Alt text

一、MVVM架构优势

《两张图看懂Android开发中MVC与MVP的区别》 前面两张图真是了MVC和MVP的区别,我这里也来一张图看看MVVM:

2-MVVM架构

看上图ModelView是不会发生关系的,ViewModel是把View和Model关联起来的加工厂:

3-ViewModel工厂

MVVM优势总结:

  1. ViewModel双向绑定,一方的改变都会影响另一方,开发者不用再去手动修改UI的数据。额,互相自动的。

  2. 不需要findViewById也不需要butterknife,不需要拿到具体的View去设置数据绑定监听器等等,这些都可以用DataBinding完成。是不是很舒服?

  3. ViewModel的双向绑定是支持生命周期检测的,不会担心页面销毁了还有回调发生,这个由lifeCycle完成。

  4. 不会像MVC一样导致Activity中代码量巨大,也不会像MVP一样出现大量的ViewPresenter接口。项目结构更加低耦合。

  5. 更低的耦合把各个模块分开开发,分开测试,可以分给不同的开发人员来完成。

二、MVVM组件化示例项目架构分析

下图是项目模块和工程之间的依赖关系:

4-MVVM组件化示例项目架构图

下图是工程Android Studio中的目录结构:

5-工程目录结构

3.1 各模块和彼此之间的关系解释:

  • lib_opensource :第三方build.gradle依赖,本项目主要有support lifecycleroomfresco retrofitokhttpRxJavaARouter这些。

  • lib_coremodel: 存放MVVM中的ModelViewModel两个模块,就是数据的处理和数据与UI页面的绑定。依赖lib_opensource库。

  • lib_common : 公共库,主要有各种base,各种ui组件,自定义组件,公用的Activity、公用的Fragment,和公用的utils等等。依赖lib_coremodel库。

  • module_girls : 妹子功能模块,可以在libraryapplication之间切换,自己可以是一个app也可以成为别的app的一个组件模块。组件化编译时为app,反之为module。

  • module_news : 新闻功能模块,可以在libraryapplication之间切换,自己可以是一个app也可以成为别的app的一个组件模块。组件化编译时为app,反之为module。

  • app_universal : 定制版本的app,组件化编译时 module_girlsmodule_news为app,所以不能把这两个作为module加进来编译,所以组件化编译时app_universal要依赖lib_common库,反之就可以把 module_girlsmodule_news作为module加进来编译。

  • app_specific : 定制版本的app,组件化编译时 module_girlsmodule_news为app,所以不能把这两个作为module加进来编译,所以组件化编译时app_specific要依赖lib_common库,反之就可以把 module_girlsmodule_news作为module加进来编译。

3.2 ARouter串联各个模块

使用ARouter来跳转Activity和获取Fragment,记得看之前别人的组件化结构文章,一直都在纠结Fragment的获取问题,我想说的是有了ARouter来获取Fragment不是超级简单么?

ARouter典型应用

  • 从外部URL映射到内部页面,以及参数传递与解析
  • 跨模块页面跳转,模块间解耦
  • 拦截跳转过程,处理登陆、埋点等逻辑
  • 跨模块API调用,通过控制反转来做组件解耦

3.3 组件化编译和非组件化编译切换

我们在工程根目录下的gradle.properties文件中加入一个Boolean类型的变量,通过修改这个变量来识别编译模式:

# 每次更改“isModule”的值后,需要点击 "Sync Project" 按钮
# isModule是“集成开发模式”和“组件开发模式”的切换开关
isModule=false

然后在 module_girlsmodule_news中的build.gradle文件中支持切换:

if (isModule.toBoolean()) {
    //组件化编译时为application
    apply plugin: 'com.android.application'
} else {
    //非组件化编译时为library
    apply plugin: 'com.android.library'
}

android {
    compileSdkVersion build_versions.target_sdk
    buildToolsVersion build_versions.build_tools

    defaultConfig {
        minSdkVersion build_versions.min_sdk
        targetSdkVersion build_versions.target_sdk
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        //ARouter
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [moduleName: project.getName()]
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
    lintOptions {
        abortOnError false
    }
    sourceSets {
        main {
            if (isModule.toBoolean()) {
                //组件化编译时为app,在对应的AndroidManifest文件中需要写ndroid.intent.action.MAIN入口Activity
                manifest.srcFile 'src/main/module/AndroidManifest.xml'
            } else {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                //集成开发模式下排除debug文件夹中的所有Java文件
                java {
                    //debug文件夹中放的是Application类,非组件化时不用有此类
                    exclude 'debug/**'
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    api project(':lib_coremodel')
    api project(':lib_common')
    implementation 'com.android.support:support-v4:26.1.0'
    annotationProcessor deps.arouter.compiler
}

上面看到了组件化和非组件化编译会有不用的AndroidManifest文件,组件化时需要debug文件夹下面的application类,非组件化时排除此文件夹。 6-组件化非组件化编译切换

  • module下的AndroidManifest文件是组件化app编译时的,写了MAIN入口Activity
  • dubug下是组件化app编译时的Application类,初始化作为一个app运行时需要的资源等等。在非组件化编译在build.gradle文件中排除debug文件夹的所以东西。

googlearchitecturedemo's People

Contributors

dawish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

googlearchitecturedemo's Issues

编译报错

studio导入之后,提示几乎所有的gradle中的依赖都报错,Unable to resolve dependency for ...Could not download ...jar or aar

思路很好啊

思路很好啊,公司项目中你用的这个思路吗

ApiClient中代码错误

lib_coremodel类库中的http包中的ApiClient类
getRetrofitInstance方法没有参数
initService的baseUrl参数没有作用
导致新增service方法的话如果复用getGankDataService方法
则ApiConstants中的host设置无效,导致404

编译错误

编译时发现

  • What went wrong:
    Could not resolve all files for configuration ':lib_opensource:debugAnnotationProcessorClasspath'.

Could not download kotlin-stdlib.jar (org.jetbrains.kotlin:kotlin-stdlib:1.2.0)
Could not get resource 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-stdlib/1.2.0/kotlin-stdlib-1.2.0.jar'.
> Could not HEAD 'https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-stdlib/1.2.0/kotlin-stdlib-1.2.0.jar'.
> sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Could not download sqlite-jdbc.jar (org.xerial:sqlite-jdbc:3.20.1)
Could not get resource 'https://jcenter.bintray.com/org/xerial/sqlite-jdbc/3.20.1/sqlite-jdbc-3.20.1.jar'.
> Could not GET 'https://jcenter.bintray.com/org/xerial/sqlite-jdbc/3.20.1/sqlite-jdbc-3.20.1.jar'.
> sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Could not download commons-codec.jar (commons-codec:commons-codec:1.10)
Could not get resource 'https://jcenter.bintray.com/commons-codec/commons-codec/1.10/commons-codec-1.10.jar'.
> Could not HEAD 'https://jcenter.bintray.com/commons-codec/commons-codec/1.10/commons-codec-1.10.jar'.
> sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Could not download gson.jar (com.google.code.gson:gson:2.8.0)
Could not get resource 'https://jcenter.bintray.com/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar'.
> Could not HEAD 'https://jcenter.bintray.com/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar'.
> sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

请问如何解决, Thanks

编译的时候报错了

Error:FAILURE: Build completed with 2 failures.

1: Task failed with an exception.

  • What went wrong:
    Execution failed for task ':module_news:dataBindingProcessLayoutsDebug'.

java.lang.NullPointerException (no error message)

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    ==============================================================================

2: Task failed with an exception.

  • What went wrong:
    Execution failed for task ':module_girls:dataBindingProcessLayoutsDebug'.

-1

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    ==============================================================================

  • Get more help at https://help.gradle.org

BUILD FAILED in 17s

Arouter

这个能算是模块化开发吗?

个人一些想法

项目思路很好,但有个地方不是很认同,把业务逻辑都共用在lib_coremodel,这不符合模块化**吧。例如news和girsl的接口请求已经耦合在同一个模块,如果项目来一个需求,需要girls单独成立一个app。lib_coremodel里面news的代码已经是多余的了。

关于数据库问题

如果开发过程中有数据库的需求.那数据库放在哪里呢?也都放在lib_coremodel?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.