无法解析变量“ $ {project.groupId}”

时间:2018-10-15 11:42:53

标签: android android-gradle dagger androidx

我将项目迁移到AndroidX,并且在构建项目时遇到了以下错误:

[TAG] Failed to resolve variable '${project.groupId}'   
[TAG] Failed to resolve variable '${project.version}'   
[TAG] Failed to resolve variable '${project.groupId}'   
[TAG] Failed to resolve variable '${project.version}'   
[TAG] Failed to resolve variable '${project.groupId}'   
[TAG] Failed to resolve variable '${project.version}'   

我已经尝试了几种可能的解决方案:

  • 清洁项目
  • 使缓存无效并重新启动
  • enableJetifier设置为false
  • 将选项--refresh-dependencies添加到编译器设置

这是我的项目gradle文件:

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        google()
        maven {
            url 'https://maven.fabric.io/public'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'com.google.gms:google-services:4.0.0'
        classpath 'io.fabric.tools:gradle:1.25.4'
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://jitpack.io'
        }
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
}

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

这是我的应用gradle文件:

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

android {

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    signingConfigs {
        //not showing this
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId "my.app.package"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 59
        versionName "1.5.2"

        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true

    }
    dataBinding {
        enabled = true
    }
    // workaround for "duplicate files during packaging of APK" issue
    // see https://groups.google.com/d/msg/adt-dev/bl5Rc4Szpzg/wC8cylTWuIEJ
    packagingOptions {
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'

        //Workaround to an issue due to google play-services 11.0.0 + rxJava
        exclude 'META-INF/rxjava.properties'
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release_config

        }
        debug {
            applicationIdSuffix ".debug"
            versionNameSuffix " - debug"
            debuggable true
        }
    }

    flavorDimensions "public"

    productFlavors {
        //not useful to show this
    }
}

ext {
    supportLibVersion = '28.0.0'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'
    implementation 'org.jetbrains:annotations-java5:15.0'
    implementation project(path: ':security')

    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.legacy:legacy-support-v13:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'

    implementation 'com.google.firebase:firebase-core:16.0.0'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.3'
    implementation 'com.google.guava:guava:22.0-android'
    implementation 'com.google.android:flexbox:0.2.3'
    implementation 'com.google.code.gson:gson:2.8.0'

    implementation 'com.hannesdorfmann.fragmentargs:annotation:3.0.2'
    annotationProcessor 'com.hannesdorfmann.fragmentargs:processor:3.0.2'

    implementation 'com.f2prateek.dart:dart:2.0.0'
    annotationProcessor 'com.f2prateek.dart:dart-processor:2.0.0'

    implementation 'com.google.dagger:dagger:2.2'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.2'

    implementation 'com.couchbase.lite:couchbase-lite-android:1.4.1'
    implementation 'com.couchbase.lite:couchbase-lite-android-sqlcipher:1.4.1'

    implementation 'com.squareup.retrofit2:retrofit:2.2.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.2.0'

    implementation 'com.wang.avi:library:1.0.5'
    implementation 'com.nineoldandroids:library:2.4.0'
    implementation 'com.jaredrummler:twodscrollview:1.0.1'

    implementation 'com.arasthel:asyncjob-library:1.0.3'
    implementation('org.simpleframework:simple-xml:2.7.1') {
        exclude group: 'xpp3', module: 'xpp3'
        exclude group: 'stax', module: 'stax-api'
        exclude group: 'stax', module: 'stax'
    }

    implementation 'io.reactivex:rxandroid:1.2.1'
    implementation 'io.reactivex:rxjava:1.1.6'

    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
    testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'

    implementation 'com.github.daniel-stoneuk:material-about-library:2.2.1'
    implementation 'org.apache.commons:commons-lang3:3.4'

    implementation('com.github.hotchemi:permissionsdispatcher:3.0.1') {
        exclude module: "support-v13"
    }
    annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:3.0.1"

    implementation 'com.pascalwelsch.compositeandroid:activity:27.0.0'
    implementation 'com.pascalwelsch.compositeandroid:fragment:27.0.0'

    implementation 'commons-codec:commons-codec:1.10'

    implementation 'com.jakewharton.timber:timber:4.7.1'
    implementation 'com.firebase:firebase-jobdispatcher:0.8.5'


}

apply plugin: 'com.google.gms.google-services'

// https://stackoverflow.com/a/49981467/3520621
configurations {
    cleanedAnnotations
    compile.exclude group: 'org.jetbrains' , module:'annotations'
}

更新

我运行gradlew assembleDebug --stacktrace --debug --info,这是更多信息:

...
Caused by: org.gradle.api.artifacts.transform.ArtifactTransformException: Failed to transform file 'dagger-compiler-2.2.jar' to match attributes {artifactType=processed-jar} using transform JetifyTransform
...
Caused by: java.lang.RuntimeException: Failed to transform 'C:\Users\me\.gradle\caches\modules-2\files-2.1\com.google.dagger\dagger-compiler\2.2\e411f732e5cd9697cdb291f6073b0d6ee2352023\dagger-compiler-2.2.jar' using Jetifier. Reason: The type does not support '.' as package separator! Received 'java.lang.Object;Ljava.lang.Class'.. (Run with --stacktrace for more details.) To disable Jetifier, set android.enableJetifier=false in your gradle.properties file.

所以看来 jetifier dagger 是原因,但是 jetifier 是向AndroidX迁移所必需的,我不想删除匕首

如果我设置了android.enableJetifier=false,则有关DataBinding的问题将超过50个。


更新2 这是完整的例外

org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':app:devDebugAnnotationProcessorClasspath'.
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.rethrowFailure(DefaultConfiguration.java:944)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.access$1600(DefaultConfiguration.java:120)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$ConfigurationFileCollection.getFiles(DefaultConfiguration.java:918)
        at org.gradle.api.internal.file.AbstractFileCollection.iterator(AbstractFileCollection.java:68)
        at org.gradle.api.internal.changedetection.state.AbstractFileCollectionSnapshotter$FileCollectionVisitorImpl.visitCollection(AbstractFileCollectionSnapshotter.java:72)
        at org.gradle.api.internal.file.AbstractFileCollection.visitRootElents(AbstractFileCollection.java:234)
        at org.gradle.api.internal.file.CompositeFileCollection.visitRootElents(CompositeFileCollection.java:185)
        at org.gradle.api.internal.changedetection.state.AbstractFileCollectionSnapshotter.snapshot(AbstractFileCollectionSnapshotter.java:55)
        at org.gradle.api.internal.changedetection.state.DefaultClasspathSnapshotter.snapshot(DefaultClasspathSnapshotter.java:42)
        at org.gradle.api.internal.changedetection.state.CacheBackedTaskHistoryRepository.snapshotTaskFiles(CacheBackedTaskHistoryRepository.java:329)
        at org.gradle.api.internal.changedetection.state.CacheBackedTaskHistoryRepository.createExecution(CacheBackedTaskHistoryRepository.java:151)
        at org.gradle.api.internal.changedetection.state.CacheBackedTaskHistoryRepository.access$100(CacheBackedTaskHistoryRepository.java:61)
        at org.gradle.api.internal.changedetection.state.CacheBackedTaskHistoryRepository$1.getCurrentExecution(CacheBackedTaskHistoryRepository.java:111)
        at org.gradle.api.internal.changedetection.changes.DefaultTaskArtifactStateRepository$TaskArtifactStateImpl.getStates(DefaultTaskArtifactStateRepository.java:208)
        at org.gradle.api.internal.changedetection.changes.DefaultTaskArtifactStateRepository$TaskArtifactStateImpl.isUpToDate(DefaultTaskArtifactStateRepository.java:93)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:50)
        at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
        at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter.java:44)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:91)
        at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:62)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:256)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:249)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:238)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:663)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:597)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
        at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: org.gradle.api.artifacts.transform.ArtifactTransforxception: Failed to transform file 'dagger-compiler-2.2.jar' to match attributes {artifactType=processed-jar} using transform JetifyTransform
        at org.gradle.api.internal.artifacts.transform.UserCodeBackedTransforr.transform(UserCodeBackedTransforr.java:89)
        at org.gradle.api.internal.artifacts.transform.TransformArtifactOperation.run(TransformArtifactOperation.java:41)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.access$900(DefaultBuildOperationExecutor.java:63)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$ParentPreservingQueueWorker.execute(DefaultBuildOperationExecutor.java:378)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.runOperation(DefaultBuildOperationQueue.java:230)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.access$600(DefaultBuildOperationQueue.java:172)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable$1.call(DefaultBuildOperationQueue.java:209)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable$1.call(DefaultBuildOperationQueue.java:203)
        at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:152)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.runBatch(DefaultBuildOperationQueue.java:202)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.run(DefaultBuildOperationQueue.java:177)
        ... 3 more
Caused by: java.lang.RuntiException: Failed to transform 'C:\Users\martin\.gradle\caches\modules-2\files-2.1\com.google.dagger\dagger-compiler\2.2\e411f732e5cd9697cdb291f6073b0d6ee2352023\dagger-compiler-2.2.jar' using Jetifier. Reason: The type does not supp
ort '.' as package separator! Received 'java.lang.Object;Ljava.lang.Class'.. (Run with --stacktrace for more details.) To disable Jetifier, set android.enableJetifier=false in your gradle.properties file.
        at com.android.build.gradle.internal.dependency.JetifyTransform.transform(JetifyTransform.kt:204)
        at org.gradle.api.internal.artifacts.transform.TransformArtifactsAction.apply(TransformArtifactsAction.java:44)
        at org.gradle.api.internal.artifacts.transform.TransformArtifactsAction.apply(TransformArtifactsAction.java:29)
        at org.gradle.api.internal.artifacts.transform.DefaultTransfordFileCache$TransformAction.execute(DefaultTransfordFileCache.java:224)
        at org.gradle.api.internal.artifacts.transform.DefaultTransfordFileCache$TransformAction.execute(DefaultTransfordFileCache.java:211)
        at org.gradle.internal.resource.local.DefaultPathKeyFileStore$1.execute(DefaultPathKeyFileStore.java:111)
        at org.gradle.internal.resource.local.DefaultPathKeyFileStore$1.execute(DefaultPathKeyFileStore.java:107)
        at org.gradle.internal.resource.local.DefaultPathKeyFileStore.doAdd(DefaultPathKeyFileStore.java:160)
        at org.gradle.internal.resource.local.DefaultPathKeyFileStore.add(DefaultPathKeyFileStore.java:107)
        at org.gradle.internal.resource.local.DefaultPathKeyFileStore.add(DefaultPathKeyFileStore.java:50)
        at org.gradle.api.internal.artifacts.transform.DefaultTransfordFileCache$1$1.create(DefaultTransfordFileCache.java:143)
        at org.gradle.api.internal.artifacts.transform.DefaultTransfordFileCache$1$1.create(DefaultTransfordFileCache.java:122)
        at org.gradle.cache.internal.LockOnDemandCrossProcessCacheAccess.withFileLock(LockOnDemandCrossProcessCacheAccess.java:89)
        at org.gradle.cache.internal.DefaultCacheAccess.withFileLock(DefaultCacheAccess.java:193)
        at org.gradle.cache.internal.DefaultPersistentDirectoryStore.withFileLock(DefaultPersistentDirectoryStore.java:158)
        at org.gradle.cache.internal.DefaultCacheFactory$ReferenceTrackingCache.withFileLock(DefaultCacheFactory.java:187)
        at org.gradle.api.internal.artifacts.transform.DefaultTransfordFileCache$1.create(DefaultTransfordFileCache.java:122)
        at org.gradle.api.internal.artifacts.transform.DefaultTransfordFileCache$1.create(DefaultTransfordFileCache.java:115)
        at org.gradle.cache.internal.ProducerGuard$AdaptiveProducerGuard.guardByKey(ProducerGuard.java:97)
        at org.gradle.api.internal.artifacts.transform.DefaultTransfordFileCache.loadIntoCache(DefaultTransfordFileCache.java:115)
        at org.gradle.api.internal.artifacts.transform.DefaultTransfordFileCache.getResult(DefaultTransfordFileCache.java:107)
        at org.gradle.api.internal.artifacts.transform.UserCodeBackedTransforr.transform(UserCodeBackedTransforr.java:87)
        ... 16 more
Caused by: java.lang.IllegalArguntException: The type does not support '.' as package separator! Received 'java.lang.Object;Ljava.lang.Class'.
        at com.android.tools.build.jetifier.core.type.JavaType.<init>(JavaType.kt:28)
        at com.android.tools.build.jetifier.processor.transform.bytecode.asm.CustomRemapper.mapValue(CustomRemapper.kt:49)
        at org.objectweb.asm.commons.thodRemapper.visitLdcInsn(thodRemapper.java:179)
        at org.objectweb.asm.ClassReader.readCode(ClassReader.java:1582)
        at org.objectweb.asm.ClassReader.readthod(ClassReader.java:1126)
        at org.objectweb.asm.ClassReader.accept(ClassReader.java:698)
        at org.objectweb.asm.ClassReader.accept(ClassReader.java:500)
        at com.android.tools.build.jetifier.processor.transform.bytecode.ByteCodeTransforr.runTransform(ByteCodeTransforr.kt:39)
        at com.android.tools.build.jetifier.processor.Processor.visit(Processor.kt:328)
        at com.android.tools.build.jetifier.processor.archive.ArchiveFile.accept(ArchiveFile.kt:41)
        at com.android.tools.build.jetifier.processor.Processor.visit(Processor.kt:316)
        at com.android.tools.build.jetifier.processor.archive.Archive.accept(Archive.kt:66)
        at com.android.tools.build.jetifier.processor.Processor.transformLibrary(Processor.kt:312)
        at com.android.tools.build.jetifier.processor.Processor.transform(Processor.kt:175)
        at com.android.build.gradle.internal.dependency.JetifyTransform.transform(JetifyTransform.kt:199)
        ... 37 more

8 个答案:

答案 0 :(得分:6)

迁移到androidx后,Android Studio无法解析dagger-androiddagger-android-support类,例如DaggerAppCompatActivity。 Issue link

答案 1 :(得分:5)

我一直都遇到相同的问题,只需在Gradle中再次添加同步,问题就解决了,我认为这是Android Studio的错误

答案 2 :(得分:3)

我通过将build.gradle(Module)中的变量更改为其版本来解决此错误, 例如,我更改了

implementation "androidx.room:room-rxjava2:$version"

TO

implementation "androidx.room:room-rxjava2:2.1.0-alpha03"

并解决:)

  • 将AndroidStudio更新到3.3稳定版后出现问题!版本

我使用的是Androidx,...但是在升级Android Studio之前一切正常。

答案 3 :(得分:3)

另一个原因是Jetifier试图将不需要的库进行喷射。 从Android gradle插件3.3.0-rc01和3.4.0-alpha04开始,有一种解决方法,可以通过将指定的库添加到gradle.properties处以逗号分隔的列表来禁用特定库:

android.jetifier.blacklist=byte-buddy,guava

找到解决方法here

答案 4 :(得分:1)

   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="@string/authorities"
        android:exported="false"
        android:grantUriPermissions="true"
        >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>

答案 5 :(得分:0)

遇到此问题时,我通过删除.gradle /下的所有文件来解决。然后我打开android studio并再次构建。它已经工作并解决了。

答案 6 :(得分:0)

我为解决这个问题所做的是File - Invalidate Caches/Restart

答案 7 :(得分:0)

我通过升级解决了该错误:

  • Android Studio版本3.5.1
  • 分级到5.1.1(从gradle-4.10.1开始)

    In gradle.wrapper.properties
    distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip