Kotlin作为安卓的第一语言学习下还是必要的不废话上干货
新建项目配置gradle
buildscript {
ext{
kotlin_version = '1.3.71'
version_lifecycle_extensions = "2.2.0"
version_lifecycle = "2.2.0"
version_room = "2.2.5"
version_appcompat = "1.2.0"
version_fragment = "1.0.0"
version_core = "1.3.2"
version_coroutine = "1.3.6"
version_navigation = '1.0.0'
version_constraint_layout = "2.0.0-rc1"
version_retrofit_coroutines_adapter = "0.9.2"
version_joda = "2.10"
version_retrofit = "2.5.0"
version_timber = "4.7.1"
version_work = "1.0.1"
version_glide = "4.11.0"
version_moshi = "1.8.0"
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$anvigationVertion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这是项目的gradle文件我这边用的androidStudio 版本为3.6.3 接下来看下app的gradle 文件
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.application"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
// Support libraries
implementation "androidx.appcompat:appcompat:$version_appcompat"
implementation "androidx.fragment:fragment:$version_fragment"
implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"
// Android KTX
implementation "androidx.core:core-ktx:$version_core"
// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$version_navigation"
implementation "android.arch.navigation:navigation-ui-ktx:$version_navigation"
// Coroutines for getting off the UI thread
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version_core"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version_coroutine"
// Retrofit for networking
implementation "com.squareup.retrofit2:retrofit:$version_retrofit"
implementation "com.squareup.retrofit2:converter-moshi:$version_retrofit"
implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:$version_retrofit_coroutines_adapter"
// Joda time library for dealing with time
implementation "joda-time:joda-time:$version_joda"
// ViewModel and LiveData (arch components)
implementation "androidx.lifecycle:lifecycle-extensions:$version_lifecycle_extensions"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$version_lifecycle"
// Logging
implementation "com.jakewharton.timber:timber:$version_timber"
// Glide for images
implementation "com.github.bumptech.glide:glide:$version_glide"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
annotationProcessor "com.github.bumptech.glide:compiler:$version_glide"
kapt "com.github.bumptech.glide:compiler:$version_glide"
// Moshi for parsing the JSON format
implementation "com.squareup.moshi:moshi:$version_moshi"
implementation "com.squareup.moshi:moshi-kotlin:$version_moshi"
// Room database
implementation "androidx.room:room-runtime:$version_room"
kapt "androidx.room:room-compiler:$version_room"
testImplementation "androidx.room:room-testing:$version_room"
annotationProcessor "androidx.room:room-compiler:$version_room"
// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$version_room"
implementation "androidx.slice:slice-builders-ktx:1.0.0-alpha07"
implementation "androidx.sqlite:sqlite-ktx:2.1.0"
// WorkManager
implementation "android.arch.work:work-runtime-ktx:$version_work"
implementation "com.tencent:mmkv:1.0.19"
implementation "com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.2"
implementation "com.qmuiteam:qmui:2.0.0-alpha07"
implementation "me.jessyan:autosize:1.1.1"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
这里的依赖项包含了大部分常用的组件和框架
需要注意的是AndroidStudio4.0以上版本app的gradle配置开启databinding 变更
dataBinding {
enabled = true
}
// 改成
buildFeatures {
dataBinding true
}
manifest文件配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.application">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--autosize 这个配置为屏幕适配需要必须设置 除非不用autosize-->
<meta-data
android:name="design_width_in_dp"
android:value="375" />
<meta-data
android:name="design_height_in_dp"
android:value="667" />
<!-- autosize -->
<!-- fonts 这个为字体配置-->
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
<!-- fonts-->
<!-- provider 文件共享-->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.generic.file.provider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<!-- provider-->
</application>
</manifest>