Android添加room依赖的正确姿势(附带完整流程)

Android添加room依赖的正确姿势(附带完整流程)

1.添加完room依赖,一直build running,最后提示Read time out

查看错误发现以下错误:

Execution failed for task ':app:kaptGenerateStubsDebugKotlin'

Could not resolve all files for configuration ':app:kapt'.

Could not download kotlinx-metadata-jvm.jar

Could not get resource 'https://jcenter.bintray.com/org/jetbrains/kotlinx/kotlinx-metadata-jvm/0.0.5/kotlinx-metadata-jvm-0.0.5.jar'

Could not GET 'https://repo.jfrog.org/artifactory/libs-release-bintray/org/jetbrains/kotlinx/kotlinx-metadata-jvm/0.0.5/kotlinx-metadata-jvm-0.0.5.jar?referrer'

java.net.SocketTimeoutException: Read timed out

发现主要是无法下载文件kotlinx-metadata-jvm.jar导致超时

2.解决方案

在build.gradle(Poject:xxx)文件中添加远程厂库mavenCentral() 完整代码如下:

buildscript {

ext.kotlin_version = '1.3.50'

repositories {

google()

jcenter()

}

dependencies {

classpath 'com.android.tools.build:gradle:3.5.3'

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

}

allprojects {

repositories {

mavenCentral()

google()

jcenter()

}

}

task clean(type: Delete) {

delete rootProject.buildDir

}

3.添加room依赖完整流程如下

3.1查看官方文档,添加最新稳定版room依赖

注意:使用kotlin开发项目必须使用插件kapt添加room-compiler,所以先添加插件apply plugin: 'kotlin-kapt',再添加room依赖:

def room_version = "2.2.3"

implementation "androidx.room:room-runtime:$room_version"

kapt "androidx.room:room-compiler:$room_version"

implementation "androidx.room:room-ktx:$room_version"

testImplementation "androidx.room:room-testing:$room_version"

build.gradle(Module:app)文件完整代码如下:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {

compileSdkVersion 29

buildToolsVersion "29.0.2"

defaultConfig {

applicationId "com.example.words"

minSdkVersion 21

targetSdkVersion 29

versionCode 1

versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'androidx.appcompat:appcompat:1.1.0'

implementation 'androidx.core:core-ktx:1.1.0'

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.test.ext:junit:1.1.1'

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

//room

def room_version = "2.2.3"

implementation "androidx.room:room-runtime:$room_version"

kapt "androidx.room:room-compiler:$room_version"

implementation "androidx.room:room-ktx:$room_version"

testImplementation "androidx.room:room-testing:$room_version"

}

3.2在build.gradle(Poject:xxx)文件中添加远程厂库mavenCentral()

build.gradle(Poject:xxx)文件完整代码如下:

buildscript {

ext.kotlin_version = '1.3.50'

repositories {

google()

jcenter()

}

dependencies {

classpath 'com.android.tools.build:gradle:3.5.3'

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

}

allprojects {

repositories {

mavenCentral()

google()

jcenter()

}

}

task clean(type: Delete) {

delete rootProject.buildDir

}

相关文章

葛氏鲈塘鳢怎么钓(老头鱼的学名是葛氏鲈塘鳢)
照片调色滤镜软件哪个好?看过来!这里有简单好用的修图软件分享
慕思股份借世界杯推广睡眠文化 传递中国家居业上市品牌影响力
Git Credential Manager
365安卓版

Git Credential Manager

08-15 阅读: 7062
肺癌的分型及其预后
365安卓版

肺癌的分型及其预后

06-29 阅读: 9108
[世界杯]科拉罗夫任意球 完美弧线直挂死角