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
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章