前篇《献给android原生应用层开发初学者技术架构选型和整合的方案思路(一)》

本篇侧重于安卓创建的创建和后续 demo 中用到的 lib 在 gradle 中的依赖配置,本文假设您已经安装并配置好 android studio和 SDK。

  • 创建项目
  1. 创建初始化 android 项目:打开 android studio -- File -- New Project。选择Phone And Tablet -- Add No Activity --Next. 
  2. 在“Configure your project”面板,取好项目名称,定义好包名,分配项目保存路径,选择 Kotlin 为主要开发语言,最小API Level 为 16,android 4.1,不勾选“This project will support instant apps”。不勾选“Use AndroidX artifacts.
  3. androidX 为下一代支持包标准以替代 supports系列,暂时没到完全采用 X 系列的必要,本 demo 仍旧采用 supports系列的扩展包。点击 Finish,稍等片刻gradle初始化完成(为了依赖包下载顺利您可能需要利用酸酸乳等科学上网)。

  4. 创建 Default  Launcher Activity.

创建一个 package名为activities,专门用来存放所有的 activity,代码路径做好规划和分类。

在 com.demo.mvvm.activities 下右击 -- New -- Activity -- Empty Activity.弹窗中取消勾选 Generate Layout File,选中 Launcher Activity 作为启动项,勾选 Backwards Compatibility(AppCompat)以向后兼容。取名 LaucherActivity.语言为 Kotlin.如图所示:

Finish 后 IDE会在AndroidManifest.xml 里配置启动参数 xml 项,

       

至此你的 app 可以编译到你的安卓设备或者模拟器中运行了。

  • 配置 Gradle lib 依赖

前篇文章中罗列的一些用到的组件库均通过 gragle 进行依赖,有关工程的 build.gradle和各个 app moudle build.gradle 的区别,请自行学习脑补。

  1. 在工程 build.gradle 中,allprojects -- repositories 中增加 maven { url "https://jitpack.io" }中央仓库,代码如下:
    allprojects {    repositories {        google()        jcenter()        maven { url "https://jitpack.io" }    }}
  2. 在工程的 build.gradle中增加所有依赖的 lib 的版本号变量
        ext.kotlinVersion = '1.3.21'    ext.mvrxVersion = '0.7.2'    ext.supportLibVersion = '28.0.0'    ext.buildToolsVersion = '28.0.3'    ext.lifecycleVersion = '1.1.1'    ext.robolectricVersion = '3.8'    ext.epoxyVersion = '2.16.4'    ext.moshiVersion = '1.6.0'    ext.koinVersion = '0.9.3'    ext.retrofitVersion = '2.5.0'    ext.navVersion = '1.0.0-alpha09'    ext.roomVersion = "1.1.1"    ext.arouterRegisterVersion = '1.0.2'    ext.arouterApiVersion = '1.4.1'    ext.arouterCompilerVersion = '1.2.2'    ext.rxAndroidVersion = '2.1.0'    ext.rxJavaVersion = '2.2.4'    ext.rxlifecycleVersion = '2.2.2'    ext.autoDisposeVersion = '0.8.0'    ext.rxPermissionsVersion = '0.9.5'    ext.rxrelayVersion = '2.1.0'    ext.fastandrutilsVersion = '1.0.0'    ext.androidUtilCodeVersion = '1.22.10'    ext.okhttpProfilerVersion = '1.0.4'    ext.eventBusVersion = '3.1.1'    ext.tDialogVersion = '2.1.1'    ext.rxToolVersion = 'v2.2.8'    ext.qmuiVersion = '1.1.12'    ext.leakVersion = '1.6.3'    ext.fragmentationVersion = '1.3.6'    ext.eventBusActivityScopeVersion = '1.1.0'

     用不到的读者自行删减,不一一详细说明用处,版本号集中在一起可方便以后依赖库升级时更新版本号。其中 supports 系列的版本号用到的是28.0.0,autoDispose的版本用的是0.8.0,rxLifeCycle等等因为后续版本支持 androidX 系列库,版本高低都会有依赖编译错误,请读者自行爬坑。

  3. 在总工程 build.gradle的buildscript -- dependencies 中增加 Kotlin extensions for android 插件 lib,该 lib的作用是削减大量 findViewById这种获取控件对象的代码,知识请参阅《Kotlin-Android-Extensions:不仅仅是替代findViewById》,另外还在在具体的模块工程里面添加此插件引用,后续文章会提到。同时,下面配置也加入了阿里巴巴的 ARouter lib 的引用,在具体工作模块 build.gradle 也会有后续配置。

        dependencies {        classpath 'com.android.tools.build:gradle:3.3.2'        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlinVersion"        classpath "com.alibaba:arouter-register:$arouterRegisterVersion"        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }

     

  4. 到具体的模块,如 app 中,编辑 build.gradle文件。

 

1)在文件顶部添加kotlin-kapt Kotlin 提示插件,arouter插件以启用,连同原有的配置,代码如下:

apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'apply plugin: 'kotlin-kapt'apply plugin: 'com.alibaba.arouter'

2)检查确保 compileSdkVersion和targetSdkVersion一样都为28,minSdkVersion为16,具体意义功能请自行脑补

3)增加 buildToolsVersion "$buildToolsVersion"

4)启用 multi-dex 以解决64k 的限制,在 android -- defaultConfig节点下添加multiDexEnabled true

5)添加arguments = [AROUTER_MODULE_NAME: project.getName()],此为 ARouter 库的要求,请查阅相关文章

6)其他一些配置项目,如androidExtensions -- experimental = true,compileOptions版本等等,代码如下

android {    signingConfigs {    }    compileSdkVersion 28    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_8        targetCompatibility JavaVersion.VERSION_1_8    }    defaultConfig {        applicationId "com.demo.mvvm"        minSdkVersion 16        targetSdkVersion 28        versionCode 1        versionName "1.1.0"        multiDexEnabled true        javaCompileOptions {            annotationProcessorOptions {                arguments = [AROUTER_MODULE_NAME: project.getName()]            }        }    }    buildToolsVersion "$buildToolsVersion"    buildTypes {        release {            // 混淆            minifyEnabled false            // Zip align优化            zipAlignEnabled true            // 移除无用的resource文件//            shrinkResources true            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }        debug {            minifyEnabled false        }    }//    dexOptions {//        incremental true//        javaMaxHeapSize '2g'//    }}androidExtensions {    experimental = true}

7)在 dependencies中添加所有用到的库的依赖,代码如下,您可以直接复制使用:

    implementation fileTree(include: ['*.jar'], dir: 'libs')    // java jdk kotlin version    implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"            , "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"            , "org.koin:koin-android-architecture:$koinVersion")    // support and compatible,lifecycle version    implementation("com.android.support:appcompat-v7:$supportLibVersion"            , "com.android.support:recyclerview-v7:$supportLibVersion"            , "android.arch.lifecycle:extensions:$lifecycleVersion"            , "com.android.support.constraint:constraint-layout:1.1.3")    // Airbnb company mvRx ,epoxy libs    implementation "com.airbnb.android:mvrx:$mvrxVersion"    implementation("com.airbnb.android:epoxy:$epoxyVersion", { exclude group: 'com.android.support' })    implementation 'com.android.support.constraint:constraint-layout:1.1.3'    kapt "com.airbnb.android:epoxy-processor:$epoxyVersion"    // rxjava rxandroid    implementation("io.reactivex.rxjava2:rxandroid:$rxAndroidVersion"            , "io.reactivex.rxjava2:rxjava:$rxJavaVersion")    // retrofit okhttp3 with network,adapter for rxjava    implementation("com.squareup.retrofit2:retrofit:$retrofitVersion"            , "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"            , "com.squareup.okhttp3:logging-interceptor:3.9.0"            , "com.squareup.retrofit2:converter-gson:2.3.0")    // navigator components    implementation("android.arch.navigation:navigation-fragment-ktx:$navVersion"            , "android.arch.navigation:navigation-ui-ktx:$navVersion")    // ORM    implementation("android.arch.persistence.room:runtime:$roomVersion"            , "android.arch.persistence.room:rxjava2:$roomVersion")    implementation 'com.android.support:multidex:1.0.3'    // 生命周期相关 lifecyble adapter to rxjava  https://github.com/trello/RxLifecycle    implementation "com.trello.rxlifecycle2:rxlifecycle-components:$rxlifecycleVersion"    implementation("com.trello.rxlifecycle2:rxlifecycle-kotlin:$rxlifecycleVersion"            , "com.trello.rxlifecycle2:rxlifecycle-android-lifecycle:$rxlifecycleVersion"            , "com.trello.rxlifecycle2:rxlifecycle-android-lifecycle-kotlin:$rxlifecycleVersion"    )    // Uber company rxJava memory auto release libs    implementation("com.uber.autodispose:autodispose-android:$autoDisposeVersion",            "com.uber.autodispose:autodispose-android-archcomponents:$autoDisposeVersion",            "com.uber.autodispose:autodispose-kotlin:$autoDisposeVersion",            "com.uber.autodispose:autodispose-android-kotlin:$autoDisposeVersion",            "com.uber.autodispose:autodispose-android-archcomponents-kotlin:$autoDisposeVersion")    // 权限申请    implementation "com.tbruyelle.rxpermissions2:rxpermissions:$rxPermissionsVersion"    // alibaba ARouter    implementation("com.alibaba:arouter-api:$arouterApiVersion", { exclude group: "com.android.support" })    implementation "org.greenrobot:eventbus:$eventBusVersion"    //两个android快速开发的工具类FastAndrUtils, androidUtilCode,功能会有部分重复    //https://github.com/570622566/FastAndrUtils    //https://blankj.com/2016/07/31/android-utils-code/    //https://github.com/Blankj/AndroidUtilCode//    implementation "cn.hotapk:fastandrutils:$fastandrutilsVersion"    implementation "com.blankj:utilcode:$androidUtilCodeVersion"    //拦截器 需要在 android studio 插件中心下载 okhttpprofiler 插件监听 okhttp3的请求,可生成 entity对象    implementation "com.itkacher.okhttpprofiler:okhttpprofiler:$okhttpProfilerVersion"    //弹窗组件 https://github.com/Timmy-zzh/TDialog    implementation "com.timmy.tdialog:tdialog:$tDialogVersion"    //RxTool UI 组件及库    //基础工具库,    //UI库    // 相机库    //功能库(Zxing扫描与生成二维码条形码 支付宝 微信)    //ArcGis For Android工具库(API:100.1以上版本)    implementation("com.github.vondear.RxTool:RxKit:$rxToolVersion", "com.github.vondear.RxTool:RxUI:$rxToolVersion",            "com.github.vondear.RxTool:RxCamera:$rxToolVersion", "com.github.vondear.RxTool:RxFeature:$rxToolVersion",            "com.github.vondear.RxTool:RxArcGisKit:$rxToolVersion")    // 腾讯 UI组件,集合控件,手势操作等    implementation(            "com.qmuiteam:qmui:$qmuiVersion"//            "com.qmuiteam:arch:0.3.0",//            "com.qmuiteam:qmuilint:1.0.1"    )    //内存泄露检测    debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakVersion"    releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakVersion"    // Optional, if you use support library fragments:    debugImplementation "com.squareup.leakcanary:leakcanary-support-fragment:$leakVersion"    implementation(//            "me.yokeyword:fragmentation:$fragmentationVersion",            "me.yokeyword:fragmentation-core:$fragmentationVersion",            "me.yokeyword:fragmentation-swipeback:$fragmentationVersion",            "me.yokeyword:eventbus-activity-scope:$eventBusActivityScopeVersion"    )    implementation "com.android.support.test.espresso:espresso-idling-resource:3.0.2"    debugImplementation "com.amitshekhar.android:debug-db:1.0.4"    testImplementation "junit:junit:4.12"    androidTestImplementation "androidx.test:runner:1.1.1"    androidTestImplementation "androidx.test.espresso:espresso-core:3.1.1"    kapt "com.alibaba:arouter-compiler:$arouterCompilerVersion"    kapt "android.arch.persistence.room:compiler:$roomVersion"

8)按照 IDE 的提示点击 Sync Now,等待稍许gradle down下所有依赖库

9)到android studio 插件中心下载并安装OkHttp Profiler,重启以生效,有关此插件的用法,请参阅《OkHttp Profiler plugin》,此插件在查看请求信息,导出 json转换成 entity 实体 kotlin 或者 java 语言代码 data class 代码非常方便。

至此所有依赖的项目和插件配置完毕,续篇主要讲基础代码的封装和项目组成结构。

续篇《献给android原生应用层开发初学者技术架构选型和整合的方案思路(三)》

更多相关文章

  1. 单点登录(三)| JIRA 安装及 JIRA 集成 CAS 实践
  2. android中的配置权限
  3. Android(安卓)studio 设置签名
  4. pinpoint安装与配置
  5. 分环境配置
  6. 吐血整理:推荐几款顶级好用的IDEA插件
  7. No.11 使用firewall配置的防火墙策略的生效模式
  8. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  9. 搭建Flutter环境及创建第一个demo

随机推荐

  1. android user agent修改
  2. Android图形解锁的绘制
  3. Android 模拟器支持的分辨率
  4. android linux工具移植
  5. Android 提示框
  6. 在Android中使用Gradle
  7. Mac os Android 源码开发环境搭建
  8. unity游戏在安卓按home或者锁屏键后不能
  9. android 实现拖动效果
  10. getActionBar() return null