目录
1、在gradle.properties文件中设置一个参数:isUserModule = false
2、在UserCenter的build.gradle中加入以下判断:
3、创建两个manifest文件,因为两种情况下需要读取的manifest文件是不同的
4、在UserCenter的build.gradle中的buildTypes同级下面配置sourceSets,如下:
1、在kotlin多模块开发中是不能使用butterknife的
一、Module与独立APP切换
1、在gradle.properties文件中设置一个参数:isUserModule = false
当设置为false时,作为独立APP运行,设置为true时,作为library使用
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
isUserModule = false
2、在UserCenter的build.gradle中加入以下判断:
if (isUserModule.toBoolean()) {
apply plugin: 'com.android.library'
} else {
apply plugin: 'com.android.application'
}
3、创建两个manifest文件,因为两种情况下需要读取的manifest文件是不同的
这是debug中的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gskotlin.user">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.activity.RegisterActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是release中的
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gskotlin.user">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">
<activity android:name=".ui.activity.RegisterActivity"/>
</application>
</manifest>
4、在UserCenter的build.gradle中的buildTypes同级下面配置sourceSets,如下:
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
if (isUserModule.toBoolean()) {
manifest.srcFile 'src/main/release/AndroidManifest.xml'
//release模式下排除debug文件夹中的所有Java文件
java{
exclude'debug/**'
}
} else {
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
}
}
}
5、在App的build.gradle中的依赖中配置
if(isUserModule.toBoolean()){
implementation project(':UserCenter')
}
二、踩坑
1、在kotlin多模块开发中是不能使用butterknife的
因为会报编译错误:Unable to find resource ID
错误原因:Module资源ID与主工程资源ID不一致
Github issue:https://github.com/JakeWharton/butterknife/issues/974
解决方案:不使用butterknife,使用kotlin-android-extensions
- 视图绑定,可直接使用xml中的ID操作该控件
- 插件级别,无需引入第三方库
- 无需定义变量,极大地减少代码量
- 适用于Activity,Fragment,Adapter以及自定义View
配置:apply plugin:'kotlin-android-extensions'
使用:xml中:
<Button
android:id="@+id/mRegisterBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="register"
android:textAllCaps="false"
android:layout_marginTop="30dp"/>
代码中:
package com.gskotlin.user.ui.activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.gskotlin.user.R
import kotlinx.android.synthetic.main.activity_register.*
class RegisterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
mRegisterBtn.setOnClickListener {
Toast.makeText(this, "haha", Toast.LENGTH_SHORT).show()
}
}
}
三、Anko
Github地址:https://github.com/Kotlin/anko
这是kotlin中的一个扩展库
3.1、组成部分:
Anko Commons
Anko Layouts
Anko SQLite
Anko Coroutines
3.1.1 Anko Commons配置
依赖包:compile "org.jetbrains.anko:anko-commons:$anko_version"
v4依赖包:compile "org.jetbrains.anko:anko-commons:$anko_version"
3.1.2 Anko Commons使用
跳转类:startActivity,intent等
提示类:toast、dialog等
日志类:verbose()、debug()、info()、warn()、error()等
尺寸类:dip()、sp()、px2dp()、px2sp()、dimen()等
3.1.3 Anko Layouts配置:
依赖包:compile "org.jetbrains.anko:anko-sdk25:$anko_version"
v7依赖包:compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
事件依赖包:compile "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
v7事件依赖包:compile "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"
3.1.4 Anko Layouts使用:
verticlaLayout{
padding = dip(30)
editText{
hint = "Name"
textSize = 24f
}
editText{
hint = "Password"
textSize = 24f
}
button{
textSize = 26f
onClick{toast("Hello")}
}、
}
3.1.5 Anko SQLite配置与使用
依赖包:compile "org.jetbrains.anko:anko-sqlite:$anko_version"
使用:不直接使用,第三方库替代,如OrmLite、LitePal、GreenDao、Realm等
3.1.6 Anko Coroutines配置与使用
依赖包:compile "org.jetbrains.anko:anko-coroutines:$anko_version"
使用:不直接使用,第三方库替代,如Rx等
四、MVP架构配置
五、RxKotlin及RxAndroid配置
5.1 RxKotlin配置:
compile "io.reactivex:rxkotlin:${rx_kotlin_version}"
5.2 RxAndroid配置
compile "io.reactivex:rxandroid:${rx_android_version}"
六、Retrofit集成
6.1 配置:
//Retrofit
compile "com.squareup.okhttp3:okhttp:${ok_http_version}"
compile "com.squareup.retrofit2:retrofit:${retrofit_version}"
compile "com.squareup.okhttp3:logging-interceptor:${ok_http_version}"
compile "com.squareup.retrofit2:converter-gson:${retrofit_version}"
compile "com.squareup.retrofit2:adapter-rxjava:${retrofit_version}"
七、Dagger2
依赖注入框架,能够减少代码量
八、RxLifecycle
8.1 介绍
8.2 配置
//RxLifecycle
compile "com.trello:rxlifecycle-kotlin:$rx_lifecycle_version"
compile "com.trello:rxlifecycle-components:$rx_lifecycle_version"