code

NotNull 또는 Nullable 가져 오기 및 Android Studio가 컴파일되지 않음

codestyles 2021. 1. 6. 08:21
반응형

NotNull 또는 Nullable 가져 오기 및 Android Studio가 컴파일되지 않음


@NotNull 또는 @Nullable 주석을 매개 변수에 추가하면 Android Studio는 자동으로 /lib/annotations.jar 추가 및 가져 오기를 도와줍니다.

import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable;

그러나 그 후에는 프로젝트가 컴파일되지 않습니다. 주석도 제거하지만 import 문을 유지하면 프로젝트가 여전히 컴파일되지 않습니다. 그러나 NotNull 및 Nullable에 대한 import 문을 제거하면 프로젝트가 컴파일됩니다 !

Android Studio는 일반적인 오류를 제공합니다.

Gradle: 
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Bugtester:compileDebug'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

gradlew compileDebugcmd에서 실행 하면 약간의 힌트가 제공됩니다.

:Bugtester:compileDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Bugtester:compileDebug'.
> Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

그래서 내 환경 변수를 확인했고 다음과 같이 설정되었습니다.

JAVA_HOME=C:\Program Files (x86)\Java\jre7
JDK_HOME=C:\Program Files\Java\jdk1.7.0_21\

누구든지 이것에 대한 아이디어가 있습니까? (저는 자바와 안드로이드 프로그래밍에 익숙하지 않습니다)


올바른 방법은 build.gradle 종속성에서 MavenCentral 저장소의 원래 JetBrains 라이브러리를 사용하는 것입니다 (이 예에서 사용 가능한 최신 버전).

dependencies {
    compile 'com.intellij:annotations:+@jar'
    ...
}

안드로이드 자체 @NonNull& @Nullable:

  • build.gradle에 다음을 추가하십시오 .

    dependencies {
        ...
        // For @Nullable/@NonNull
        compile 'com.android.support:support-annotations:+'
    }
    
  • 파일 / 설정프로젝트 설정검사로 이동하여 "nullable"을 검색합니다.

    에서 일정한 조건 및 예외@ NOTNULL / @ Null 허용 문제 , 클릭 구성 주석 과 안드로이드의 주석을 선택합니다.

    상수 조건 및 예외 에서 @Nullable 주석 제안… 을 확인 하거나 다른 옵션을 조정할 수도 있습니다.


@Nullable, @NonNull 등과 같은 Android 지원 주석을 사용하려면 프로젝트에서 Android 지원 주석 라이브러리를 가져와야합니다. 그냥 dependensies이 줄을 추가 Gradle을 파일에

dependencies { compile 'com.android.support:support-annotations:+' }

그리고 패키지 를 클래스로 가져옵니다 .
@Nullable 주석을 사용하는 경우 :

import android.support.annotation.Nullable;

@NonNull의 경우

import android.support.annotation.NonNull;

여기에서 더 많은 정보를 찾을 수 있습니다. Android 개발자


현재 Android API 또는 지원 라이브러리에는 NonNull / Nullable 주석이 없습니다. 또한 Gradle로 빌드 할 때 컴파일 클래스 경로에 없기 때문에 IntelliJ를 사용할 수 없습니다.

However, you can easily create your own. It's very simple:

@Documented
@Retention(RetentionPolicy.CLASS)
@Target({METHOD,PARAMETER,LOCAL_VARIABLE,FIELD})
public @interface NonNull {
}

Once this is down, you can configure IntelliJ (or Android Studio) to recognize this one (and the matching @Nullable) to be the default annotation used for Null-checks.

To do this, go in the IntelliJ preferences, under Inpections, and then find the @NotNull/@Nullable problems entry under Probable Bugs in the inspection tree.

Select the item, and in the bottom right you'll have a button to "Configure Annotations". This will allow you set your own annotations as the one intelliJ will use for this inspection.


In 2015, you would have used annotations from android.support.annotation package. I am just not sure when they added them as in the docs there isn't that typical sentence "Added in API level X" and I don't feel like reading blogs, etc. >]

import android.support.annotation.NonNull;

...

public void fooFighter(@NonNull Object honeyBunny){
   ...
}

I am facing that problem for gradle-5.1.1 - Android Studio 3.4 and the error like that - Compilation failed; see the compiler error output for details. Execution failed for task ':app:compileDebugJavaWithJavac'. etc. In this case I use

implementation 'org.jetbrains:annotations:16.0.2'

and above all error will be clear.


The best way is to use the maven dependency

  1. set the repository path, by adding

    repositories {
        maven {
            url "https://repository.jboss.org/nexus/content/repositories/thirdparty-releases"
        }
    }
    
  2. add the dependency

    dependencies {
        compile 'org.jetbrains:annotations:7.0.2'
    }
    
  3. PROFIT!


For Maven add dependency:

<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>16.0.1</version>
</dependency> 

ReferenceURL : https://stackoverflow.com/questions/16690914/importing-notnull-or-nullable-and-android-studio-wont-compile

반응형