简体   繁体   中英

Can't sync after updating to gradle 5.1.1 in Android Studio 3.4

I am not sure why this is taking a long time to figure out and solve. Everything was running just fine before i do the upgrade. this is the build.gradle file:

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'
}

This is the error i get after syncing:

  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' to class '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' to class 'org.gradle.api.file.FileCollection'

I got this after i upgraded the recommendation from Android studio.

this is the 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'
}

The first issue is caused by:

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

which is invalid as it passes a File to a property expecting a FileCollection . Instead of using Project.file you should use Project.files here.

I would first fix that, to see if a proper evaluation then allows the build to pass.

I finally fixed it and it was the "file" method name that needs to be put in front all the destinations in quality.gradle file:

/**
 * 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")
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM