使用Java常量时无法解析的参考

时间:2018-11-29 16:31:11

标签: java android android-studio kotlin android-gradle

当我尝试构建时,ide抛出错误:“未解决的参考:X”

X是(公共)类Java上的任何Java常量(公共静态最终值)

不能使用:

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

我尝试过:

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

这是Java和Kotlin的混合项目

我可以使用静态方法,例如:

public static Uri getUriForFile from public class FileProvider
Etc

错误行:

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

我正在使用:

等级配置

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

软件

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

1 个答案:

答案 0 :(得分:0)

谢谢 @yole @Rod_Algonquin 的帮助,与此同时,我在写问题并回答问题时,Rubber duck method帮助我, IDE向我抛出了一个错误“未解析的引用”,但问题是注释:

@RequiresApi(Build.VERSION_CODES.M)

因为:minSdkVersion 16

我正在使用 PermissionsHelper.kt ,我使用的是 Activity ,而不是 android.support.v4.app.Fragment ContextCompat 用于旧版本。

对不起,您没有发布代码,完整的摘要,我想做个简短的帖子,这是我的第一篇文章。

谢谢您的帮助。