tags:耳朵_android
引用
首先创建新项目,并进行Kotlin的配置,目前最新的版本号为1.0.3:
然后添加项目的build.gradle中添加jitpack,再到app的build.gradle中引用HMLibrary,目前稳定的版本号为v0.0.6m:
maven { url "https://jitpack.io" }
...
compile 'com.github.bxcx:HMLibrary:v0.0.6m'
Ear build.gradle:
buildscript {
ext.kotlin_version = '1.0.3'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
maven { url "https://jitpack.io" }
mavenCentral()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 24
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "ear.life"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs //这里可以直接去掉kotlin目录,不需要创建它
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.github.bxcx:HMLibrary:v0.0.6m'
}
OK,同步一下Gradle没有异常的话我们就算配置成功了,没有成功的同学看看是不是需要翻墙,或者在下面提个问。
配置
-
Application
创建一个Application,继承自HMApp,这一步是很有必要的,我们可以看看HMApp中的源码,如果没有的同学可以到https://github.com/bxcx/HMLibrary去下载:open class HMApp : Application() { override fun onCreate() { super.onCreate() //初始化通用缓存 Cacher.init(this) //初始化图片加载 val config = ImageLoaderConfiguration.Builder(this).threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory().discCacheSize(50 * 1024 * 1024) //.diskCache(UnlimitedDiskCache(File(PathUtil.PHOTOCACHEPIC))).discCacheFileNameGenerator(Md5FileNameGenerator()) .writeDebugLogs().tasksProcessingOrder(QueueProcessingType.LIFO).build() ImageLoader.getInstance().init(config) //初始化日志输出 Logger.init(packageName) // default PRETTYLOGGER or use just init() .setMethodCount(2) // default 2 .hideThreadInfo() // default shown .setLogLevel(LogLevel.FULL) // default LogLevel.FULL .setMethodOffset(0) // default 0 //设置网络请求超时时限 OkHttpUtils.getInstance().setConnectTimeout(2000, TimeUnit.SECONDS) } }
可以看到目前Library使用的是OkHttp进行网络请求,ImageLoader做图片加载,还有一些其它功能的初始化,如果实在不想继承HMApp的话,可以将你需要用到的代码Copy至你自己的创建一个Application中即可。
这里我们先创建一个Application,并修改网络请求的默认方式为Post,不明白的同学自行移步[HMRequest.md][2]:
class App : HMApp() {
override fun onCreate() {
super.onCreate()
HMRequest.method = Method.POST
}
}
> 不要忘记去AndroidManifest中修改application的name。
最后,再次编译一下确认没有异常,我们就可以上传一下备份了.
github: https://github.com/bxcx/ear
本节分支: https://github.com/bxcx/ear/tree/init