在Android Studio 3.4中更新到Gradle 5.1.1后无法同步

时间:2019-04-30 19:32:48

标签: android gradle

我不确定为什么要花很长时间找出并解决问题。在进行升级之前,一切都运行良好。 这是build.gradle文件:

apply plugin: 'com.android.application'
apply from: '../config/quality/quality.gradle'


android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    defaultConfig {
        applicationId "com.yo"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            buildConfigField("String", "BASE_URL", "\"http://www.mocky.io/v2\"")
            buildConfigField("String", "API_KEY", "\"ABCXYZ123TEST\"")
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField("String", "BASE_URL", "\"http://www.mocky.io/v2\"")
            buildConfigField("String", "API_KEY", "\"ABCXYZ123TEST\"")
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dataBinding {
        enabled = true
    }
}

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

    // android support libraries
    implementation "androidx.appcompat:appcompat:$rootProject.supportLibraryVersion"
    implementation "androidx.recyclerview:recyclerview:$rootProject.recyclerViewVersion"
    implementation "androidx.cardview:cardview:$rootProject.cardViewVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"
    implementation "androidx.vectordrawable:vectordrawable:$rootProject.vectorDrawableVersion"
    implementation "androidx.vectordrawable:vectordrawable-animated:$rootProject.animatedVectorDrawableVersion"
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"

    // network
    implementation "com.squareup.retrofit2:retrofit:$Retrofit2Version"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$Retrofit2Version"
    implementation "com.squareup.retrofit2:converter-gson:$Retrofit2Version"
    implementation "com.squareup.okhttp3:logging-interceptor:$OkHttpVersion"
    implementation "com.squareup.okhttp3:okhttp-urlconnection:$OkHttpVersion"
    implementation "com.squareup.okhttp3:logging-interceptor:$OkHttpVersion"

    // database
    implementation "androidx.room:room-rxjava2:$rootProject.roomDatabaseVersion"
    annotationProcessor "androidx.room:room-compiler:$rootProject.roomDatabaseVersion"

    // font
    implementation "uk.co.chrisjenx:calligraphy:$rootProject.calligraphyVersion"

    // image
    implementation "com.github.bumptech.glide:glide:$rootProject.glideVersion"

    // parser
    implementation "com.google.code.gson:gson:$rootProject.gsonVersion"

    // debug database
    debugImplementation "com.amitshekhar.android:debug-db:$rootProject.debugDBVersion"

    // dependency injection
    implementation "com.google.dagger:dagger:$rootProject.dagger2Version"
    annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2Version"
    annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.dagger2Version"
    implementation "com.google.dagger:dagger-android-support:$rootProject.dagger2Version"

    // reactive
    implementation "io.reactivex.rxjava2:rxjava:$rootProject.rxjava2Version"
    implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rxandroidVersion"

    // swipe view
    implementation "com.mindorks:placeholderview:$rootProject.placeholderviewVersion"

    // logger
    implementation "com.jakewharton.timber:timber:$rootProject.timberVersion"

    // dependencies for local unit tests
    testImplementation "junit:junit:$rootProject.ext.junitVersion"
    testImplementation "org.mockito:mockito-core:$rootProject.mockitoVersion"
    testAnnotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2Version"

    // UI Testing
    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$rootProject.espressoVersion"
    androidTestImplementation "org.mockito:mockito-core:$rootProject.mockitoVersion"
    androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2Version"

    // view model
    implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.lifecycleVersion"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$rootProject.lifecycleVersion"


}



configurations.all {
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}

这是同步后出现的错误:

 FAILURE: Build completed with 2 failures.

    1: Task failed with an exception.
    -----------
    * Where:
    Script '/Users/projects/x/config/quality/quality.gradle' line: 56

    * What went wrong:
    A problem occurred evaluating script.
    > Cannot cast object '/Users/projects/x/app/build/intermediates/classes' with class
     

“ java.io.File”到“ org.gradle.api.file.FileCollection”类

    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    ==============================================================================

    2: Task failed with an exception.
    -----------
    * What went wrong:
    A problem occurred configuring project ':app'.
    > compileSdkVersion is not specified.

    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    ==============================================================================

    * Get more help at https://help.gradle.org

    Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
    Use '--warning-mode all' to show the individual deprecation warnings.
    See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings

    BUILD FAILED in 0s
    ERROR: Cannot cast object '/Users/projects/x/app/build/intermediates/classes' with class
     

“ java.io.File”到“ org.gradle.api.file.FileCollection”类

从Android Studio升级推荐后,我得到了这个。

这是quality.gradle:

**
 * Set up Checkstyle, Findbugs and PMD to perform extensive code analysis.
 *
 * Gradle tasks added:
 * - checkstyle
 * - findbugs
 * - pmd
 *
 * The three tasks above are added as dependencies of the check task so running check will
 * run all of them.
 */

apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'pmd'

dependencies {
    checkstyle 'com.puppycrawl.tools:checkstyle:6.5'
}

def qualityConfigDir = "$project.rootDir/config/quality";
def reportsDir = "$project.buildDir/reports"

check.dependsOn 'checkstyle', 'findbugs', 'pmd'

task checkstyle(type: Checkstyle, group: 'Verification', description: 'Runs code style checks') {
    configFile file("$qualityConfigDir/checkstyle/checkstyle-config.xml")
    source 'src'
    include '**/*.java'
    exclude '**/model/**'
    exclude '**/AppLogger.java'
    reports {
        xml.enabled = true
        xml {
            destination file("$reportsDir/checkstyle/checkstyle.xml")
        }
    }

    classpath = files()
}

task findbugs(type: FindBugs,
        group: 'Verification',
        description: 'Inspect java bytecode for bugs',
        dependsOn: ['compileDebugSources', 'compileReleaseSources']) {

    ignoreFailures = false
    effort = "max"
    reportLevel = "high"
    excludeFilter = new File("$qualityConfigDir/findbugs/android-exclude-filter.xml")
    classes = file("$project.rootDir/app/build/intermediates/classes")

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    reports {
        xml.enabled = true
        html.enabled = false
        xml {
            destination file("$reportsDir/findbugs/findbugs.xml")
        }
        html {
            destination file("$reportsDir/findbugs/findbugs.html")
        }
    }

    classpath = files()
}


task pmd(type: Pmd, group: 'Verification', description: 'Inspect sourcecode for bugs') {
    ruleSetFiles = file("$qualityConfigDir/pmd/pmd-ruleset.xml")
    ignoreFailures = false
    ruleSets = []

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'
    exclude '**/model/**'

    reports {
        xml.enabled = true
        html.enabled = true
        xml {
            destination file("$reportsDir/pmd/pmd.xml")
        }
        html {
            destination file("$reportsDir/pmd/pmd.html")
        }
    }
}

build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ext {
    minSdkVersion = 16
    targetSdkVersion = 28
    compileSdkVersion = 28
    buildToolsVersion = '28.0.3'
    constraintLayoutVersion = '1.1.3'
    recyclerViewVersion = '1.0.0'
    cardViewVersion = '1.0.0'

    // App dependencies
    supportLibraryVersion = '1.0.2'
    materialVersion = '1.0.0'
    vectorDrawableVersion = '1.0.1'
    animatedVectorDrawableVersion = '1.0.0'
    gsonVersion = '2.8.2'
    calligraphyVersion = '2.2.0'
    glideVersion = '3.7.0'
    roomDatabaseVersion = '2.0.0'
    rx2FastAndroidNetworking = '1.0.1'
    Retrofit2Version = "2.5.0"
    OkHttpVersion = "3.14.0"

    dagger2Version = '2.16'
    rxjava2Version = '2.1.9'
    rxandroidVersion = '2.0.2'
    placeholderviewVersion = '0.6.1'
    debugDBVersion = '1.0.3'
    timberVersion = '4.5.1'
    lifecycleVersion = '2.0.0'

    // Test dependencies
    junitVersion = '4.12'
    espressoVersion = '3.1.0'
    mockitoVersion = '2.15.0'
}

2 个答案:

答案 0 :(得分:0)

第一个问题是由以下原因引起的:

classes = file("$project.rootDir/app/build/intermediates/classes")

无效,因为它将File传递给期望FileCollection的属性。而不是使用Project.file,而应在此处使用Project.files

我首先要解决这个问题,看看是否经过适当的评估才能使构建通过。

答案 1 :(得分:0)

我终于修复了它,它是需要在quality.gradle文件中所有目标前面放置的“文件”方法名称:

/**
 * Set up Checkstyle, Findbugs and PMD to perform extensive code analysis.
 *
 * Gradle tasks added:
 * - checkstyle
 * - findbugs
 * - pmd
 *
 * The three tasks above are added as dependencies of the check task so running check will
 * run all of them.
 */

apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'pmd'

dependencies {
    checkstyle 'com.puppycrawl.tools:checkstyle:6.5'
}

def qualityConfigDir = "$project.rootDir/config/quality";
def reportsDir = "$project.buildDir/reports"

check.dependsOn 'checkstyle', 'findbugs', 'pmd'

task checkstyle(type: Checkstyle, group: 'Verification', description: 'Runs code style checks') {
    configFile file("$qualityConfigDir/checkstyle/checkstyle-config.xml")
    source 'src'
    include '**/*.java'
    exclude '**/model/**'
    exclude '**/AppLogger.java'
    reports {
        xml.enabled = true
        html.enabled = false
        xml {
            destination file("$reportsDir/checkstyle/checkstyle.xml")
        }
        html {
            destination file("$reportsDir/checkstyle/checkstyle.html")
        }
    }

    classpath = files()
}

task findbugs(type: FindBugs,
        group: 'Verification',
        description: 'Inspect java bytecode for bugs',
        dependsOn: ['compileDebugSources', 'compileReleaseSources']) {

    ignoreFailures = false
    effort = "max"
    reportLevel = "high"
    excludeFilter = new File("$qualityConfigDir/findbugs/android-exclude-filter.xml")
    classes = files("$project.rootDir/app/build/intermediates/classes")

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    reports {
        xml.enabled = true
        html.enabled = false
        xml {
            destination file("$reportsDir/findbugs/findbugs.xml")
        }
        html {
            destination file("$reportsDir/findbugs/findbugs.html")
        }
    }

    classpath = files()
}


task pmd(type: Pmd, group: 'Verification', description: 'Inspect sourcecode for bugs') {
    ruleSetFiles = files("$qualityConfigDir/pmd/pmd-ruleset.xml")
    ignoreFailures = false
    ruleSets = []

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'
    exclude '**/model/**'

    reports {
        xml.enabled = true
        html.enabled = true
        xml {
            destination file("$reportsDir/pmd/pmd.xml")
        }
        html {
            destination file("$reportsDir/pmd/pmd.html")
        }
    }
}