简体   繁体   中英

compile gradle task is always executed

I have a multidex project with minSDK 21, using gradle 4.5.1 and gradle plugin 3.0.1. My issue is that even without any source file change when I try to assemble and deploy to a phone the compile task is executed. When running with --info I get the following reasons:

Task ':app:compileStandardDebugJavaWithJavac' is not up-to-date because: Input property 'source' file C:...\\app\\build\\generated\\source\\buildConfig\\standard\\debug[package]\\BuildConfig.java has changed. Input property 'source' file C:...\\app\\build\\generated\\source\\dataBinding\\standard\\debug\\android\\databinding\\layouts\\DataBindingInfo.java has changed.

Can you help my identify what could cause these files to change and causes a recompile? I believe the Databinding sources either shouldn't change without changing sources, or it shouldn't affect if the compile task is up-to-date. The same goes for BuildConfig.java.

I have gradle caching, configure on demand and daemon enabled.

Here's the app build.gradle :

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 25
        applicationId 'package'

        // for automated testing
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        multiDexEnabled true

        vectorDrawables.useSupportLibrary = true

        buildConfigField "boolean", "PUBLIC_RELEASE", 'false'

    }

    dataBinding {
        enabled true
    }

    lintOptions {
        abortOnError false
    }

    flavorDimensions "tier1"

    productFlavors {
        standard {
            resValue "string", "app_name", "myapp"

            dimension "tier1"
        }
        beta {
            applicationId 'package.beta'

            resValue "string", "app_name", "myapp2"

            dimension "tier1"
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def project = "myapp"
            def separator = "-"
            def flavor = variant.productFlavors[0].name
            def buildType = variant.variantData.variantConfiguration.buildType.name
            def newApkName = project + separator + flavor + separator + buildType + ".apk"
            outputFileName = newApkName
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
        beta {
            aidl.srcDirs = ['src/standard/aidl']
            java.srcDirs = ['src/standard/java']
        }

    }

    signingConfigs {
        buildTypes {
            debug {
                buildConfigField "java.util.Date", "buildTime", "new java.util.Date(" + System.currentTimeMillis() + "L)"
            }
            release {
                buildConfigField "java.util.Date", "buildTime", "new java.util.Date(" + System.currentTimeMillis() + "L)"
            }
        }

    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug_StandardAndBeta
            debuggable true
            buildConfigField "boolean", "CallLogEnabled", "true"
            minifyEnabled false
        }

        release {
            signingConfig signingConfigs.release_StandardAndBeta
            debuggable false //set to true for custom debugable release build
            buildConfigField "boolean", "CallLogEnabled", "false"
            minifyEnabled true //set to false for custom debugable release build
            proguardFiles 'proguard.cfg'
        }
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    lintOptions {
        disable "NewApi", "RtlCompat", "MissingPermission", "InvalidPackage", "RecyclerView"
        abortOnError false
    }
}


repositories {
    flatDir {
        dirs 'libs'
    }

    // Crashlytics
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {


    // - Google APIs
    implementation('com.google.api-client:google-api-client-android:1.22.0') {
        exclude group: 'org.apache.httpcomponents'
    }
    implementation('com.google.apis:google-api-services-gmail:v1-rev53-1.22.0') {
        exclude group: 'org.apache.httpcomponents'
    }

    implementation 'net.openid:appauth:0.4.1'
    implementation 'com.microsoft.graph:msgraph-sdk-android:1.1.+'
    implementation 'net.hockeyapp.android:HockeySDK:4.1.1'

    // DB framework
    implementation 'com.j256.ormlite:ormlite-android:4.48'

    // RX Java
    implementation 'io.reactivex:rxandroid:1.2.1'
    implementation 'io.reactivex:rxjava:1.1.6'

    // HTTP frameworks
    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'

    implementation 'com.squareup.picasso:picasso:2.5.2'


    // Google Adwords Tracking
    implementation files('libs/Google/GoogleConversionTrackingSdk-2.2.4.jar')

    // GMS
    implementation 'com.google.android.gms:play-services-analytics:9.0.2'
    implementation 'com.google.android.gms:play-services-gcm:9.0.2'
    implementation 'com.google.android.gms:play-services-identity:9.0.2'
    implementation 'com.google.android.gms:play-services-ads:9.0.2'
    implementation 'com.google.android.gms:play-services-auth:9.0.2'

    // Other dependencies
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:preference-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.google.code.gson:gson:2.8.+'
    implementation 'com.github.clans:fab:1.6.+'
    implementation 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
    implementation 'com.caverock:androidsvg:1.2.2-beta-1'
    implementation 'de.greenrobot:eventbus:2.4.1'
    implementation 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    implementation 'com.facebook.device.yearclass:yearclass:1.0.+'
    implementation 'com.facebook.android:facebook-android-sdk:4.15.0'
    implementation 'org.parceler:parceler-api:1.1.9'
    annotationProcessor 'org.parceler:parceler:1.1.9'

    // Android architecture components
    implementation "android.arch.lifecycle:extensions:1.0.0"
}

It's because you're using a buildConfigField with System.currentTimeMillis() . This line causes the BuildConfig class to be updated for debug builds:

buildConfigField "java.util.Date", "buildTime", "new java.util.Date(" + System.currentTimeMillis() + "L)"

The Android Gradle Plugin updates the BuildConfig.java everytime you do a build and inserts the current Date, producing a static field similar to this one:

public static final java.util.Date buildTime = new java.util.Date(1518194256644L);

and since the time changes all the time (haha), the file has to be recompiled.

In my case, the problem was that I was appending date to version name.

def static getDate() {
    return new Date().format('yyyyMMddHHmmss')
}

buildTypes {
    debug {
        // REMOVE THIS LINE
        versionNameSuffix '-' + getDate()
        applicationIdSuffix ".test"
}

The line in question was

versionNameSuffix '-' + getDate()

In case anyone else encounters the same problem.

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