Unresolved Reference when use Java Constants


#1

When i tried to build, ide throw error: "Unresolved reference: X "

X is any Java constant (public static final) on (public) class Java

Can’t use:

  • android.os.Environment.DIRECTORY_DOCUMENTS
  • Build.VERSION_CODES.M
  • Etc

I have tried:

  • Invalidate Cache in Android Studio
  • gradlew cleanBuildCache
  • Clean / Rebuild
  • Clone project on other folder,
  • Etc

It’s a mixed project, Java & Kotlin

I can use static methods, like:

  • public static Uri getUriForFile from public class FileProvider
  • Etc

Error line:

  • 10:38:23.053 [ERROR] [org.gradle.api.Task] e: C:\Users\User\AndroidStudioProjects\MyAndroidProject\app\src\main\kotlin\com\myapp\mypackage\android\PermissionsHelper.kt: (67, 66): Unresolved reference: M

I’m using:

Gradle Configuration

  • ext.kotlin_version = ‘1.3.10’
  • classpath ‘com.android.tools.build:gradle:3.2.1’
  • androidExtensions { experimental = true }

Software

  • Windows 10 Pro 10.0.17134 N/D Compilation 17134 x64

  • Android Studio 3.2.1 Build #Al-181-5540.7.32.5056338,build on Octubre 8, 2018

  • java version “1.8.0_162”
    Java™ SE Runtime Environment (build 1.8.0_162-b12)
    Java HotSpot™ 64-Bit Server VM (build 25.162-b12, mixed mode)

Stacktrace:

10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception.
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong:
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Execution failed for task ':app:compileDebugKotlin'.
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Compilation error. See log for more details
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try:
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]  Run with --scan to get full insights.
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
10:38:23.076 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Exception is:
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:compileDebugKotlin'.
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:103)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:73)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.SkipCachedTaskExecuter.execute(SkipCachedTaskExecuter.java:103)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:59)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.ResolveBuildCacheKeyExecuter.execute(ResolveBuildCacheKeyExecuter.java:66)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
10:38:23.077 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 	at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter

#2

Meanwhile i was writing the questions, and answering the questions, the Rubber duck method help me, the IDE was throwing me a error “Unresolved reference”, but the problem was the annotation:

@RequiresApi(Build.VERSION_CODES.M)

Because: minSdkVersion 16

I was working on PermissionsHelper.kt, I was using Activity instead of android.support.v4.app.Fragment or ContextCompat for old versions.

Sorry for not post the code, and the complete gradle, i wanted to do a short post, this is my fist post.

Thanks