kotlin-android实战项目(App项目通用配置--gradle及App类)

kotlin最早是在俩年前开始接触,一直有关注与学习,作为大哥java的一个补充式的语言,给人一种清新的感觉。废话不多说了
先从project的配置来说吧

//用来给android配置统一管理的,project的各类module的
ext {
    compileSdkVersion = 25
    buildToolsVersion = "25.0.2"
    minSdkVersion = 17
    targetSdkVersion = 25
    versionCode = 1
    versionName = "1.0"
}
//一些基库的版本配置
buildscript {
    ext.espresso_core_version = '2.2.2'
    ext.gradle_plugin_version = '3.0.0-alpha1'
    ext.realm_plugin_version="2.2.1"
    ext.kotlin_version = '1.1.2-4'
    ext.basic_lib_version='25.2.0'
    ext.junit_lib_version='4.12'
    ext.realm_lib_version='1.4.0'

    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        //realm
        maven {
            url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
        }
        jcenter()
        mavenCentral()
        maven { url "https://jitpack.io" }
        maven { url 'https://maven.google.com' }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_plugin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        classpath "io.realm:realm-gradle-plugin:$realm_plugin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

主module的配置

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "com.twp.app.crm"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName

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

那丛第一个kotlin类开始说吧

class App : Application() {

    //登录验证
    val loginname: String? by lazy { resources.getString(R.string.login_name) }
    val loginpwd: String? by lazy { resources.getString(R.string.login_pwd) }

    companion object {
        var instance: App? = null
            get() {
                if (field == null) {
                    field =  App()
                }
                return field
            }
    }

    var realm: Realm? = null
        get() {
            if (field == null) {
                Realm.init(this)
                field = Realm.getDefaultInstance()
            }
            return field
        }
    var iconfont: Typeface? = null
        get() {
            if (field == null) {
                field = Typeface.createFromAsset(assets, "iconfont/IcoMoon.ttf")
            }
            return field
        }
    var user: User? = null
        get() {
            if (field == null) {
                //user = User_PO.getUserModel(this);
                field = realm?.where(User::class.java)?.findFirst()
            }
            try {
                if (field!!.isValid && field!!.isManaged) {
                    return field
                }
            } catch (e: NullPointerException) {
                field = null
            }
            return field
        }

    override fun onCreate() {
        super.onCreate()
        instance = this
    }
}

App需要集成Application类病配置到manifest中去,
loginname与loginpwd是做一个静态登录认证,
by lazy 是懒加载使用
companion是kotlin的静态块,instance是保存App的一个静态实例,用于保存工具或者其他实例,只提供get
?是Optional可选类型,
App采用Realm来做orm数据存储,为了跟ios的数据存储同步
iconfont是App中使用的所以图标库

你可能感兴趣的:(kotlin-android实战项目(App项目通用配置--gradle及App类))