Enabling androidX causes 'Cannot change strategy of configuration ':app:compile' after it has been resolved' error

Viewed 1450

I am currently trying to migrate OpenId/Android-Apputh to androidX and facing a strange problem after performing all changes gradle starts throwing the following error :

> Cannot change strategy of configuration ':app:compile' after it has been resolved.

After I add

android.enableJetifier=true
android.useAndroidX=true

to 'gradle.properties'

Hint

I tried to log all possible (using println(cofigBlockName)) configuration blocks and found that sourceSets configBlock was not called during the build process. So it could be that Jetifier applies some settings to sourceSets.

Does anybody know how to fix that?

android-common.gradle

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion
    defaultConfig {
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.compileSdkVersion
        versionCode rootProject.versionNum
        versionName rootProject.versionName
    }
    sourceSets {
        main.manifest.srcFile 'AndroidManifest.xml'
        main.java.srcDirs = ['java']
        main.aidl.srcDirs = ['java']
        main.res.srcDir 'res'
        main.assets.srcDir 'assets'
        main.resources.srcDir 'java'
        test.setRoot('javatests');
        test.java.srcDir('javatests');
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    lintOptions {
        warningsAsErrors true
        disable 'InvalidPackage', 'TrulyRandom', 'UseCompoundDrawables', 'GradleDependency'
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

// produces just the classes JAR
task jar(type: Copy, dependsOn:'bundleRelease') {
    from("${project.buildDir}/intermediates/bundles/release/")
    into("${project.buildDir}/libs/")
    include('classes.jar')
    rename('classes.jar', "appauth-${rootProject.versionName}.jar")
}

// produces a JAR containing sources
task sourcesJar(type: Jar, dependsOn:'generateReleaseSources') {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

tasks.withType(JavaCompile) {
    options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked"
}

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'checkstyle'
apply from: '../config/android-common.gradle'
apply from: '../config/keystore.gradle'

android {
    defaultConfig {
        applicationId 'net.openid.appauthdemo'
        project.archivesBaseName = 'appauth-demoapp'
        vectorDrawables.useSupportLibrary = true

        // Make sure this is consistent with the redirect URI used in res/raw/auth_config.json,
        // or specify additional redirect URIs in AndroidManifest.xml
        manifestPlaceholders = [
                'appAuthRedirectScheme': 'com.lohika.android.test'
        ]
    }

    signingConfigs {
        debugAndRelease {
            storeFile file("${rootDir}/appauth.keystore")
            storePassword "appauth"
            keyAlias "appauth"
            keyPassword "appauth"
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debugAndRelease
        }
        release {
            signingConfig signingConfigs.debugAndRelease
        }
    }
}

project.ext.glideVersion = '4.7.1'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':library')
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    implementation 'com.google.android.material:material:1.1.0-alpha02'
    implementation "com.github.bumptech.glide:glide:${project.glideVersion}"
    implementation 'com.squareup.okio:okio:1.14.1'
    implementation 'joda-time:joda-time:2.10'

    annotationProcessor "com.github.bumptech.glide:compiler:${project.glideVersion}"
}

apply from: '../config/style.gradle'
2 Answers

I faced a similar issue. In my case there is was problem with java language levels where the annotation processor runs on Java 9+, but android is compiled as Java 8 or lower. So adding org.gradle.java.home to build options worked:

./gradlew --info --scan --stacktrace --debug :app:clean :app:compileDebugSources -Dorg.gradle.java.home=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home

Hope this helps

This work fine for me:

    implementation 'com.google.android.exoplayer:exoplayer-core:2.10.2'
    implementation 'com.google.android.exoplayer:exoplayer-dash:2.10.2'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.6'
    implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.10.2'
Related