错误:包com.android.annotations不存在

问题描述 投票:7回答:5

我有以下课程

import com.android.annotations.NonNullByDefault;

@NonNullByDefault
public final class Log {
    ...
}

这是我的build.gradle文件(省略了一些部分)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '24.0.1'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 2
        versionName "0.2"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

}

dependencies {    
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:support-annotations:25.0.0'
    compile 'com.android.support:design:25.0.0'
}

在Android Studio中,没有针对我的课程发出警告

enter image description here

但是,当我尝试构建并运行我的应用程序时,我从gradle中收到此错误

Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug]
Warning:[options] bootstrap class path not set in conjunction with -source 1.7
/home/puter/git-repos/TaskManager3/app/src/main/java/com/treemetrics/taskmanager3/util/Log.java
Error:(3, 31) error: package com.android.annotations does not exist
Error:(7, 2) error: cannot find symbol class NonNullByDefault
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 21.021 secs
Information:3 errors
Information:1 warning
Information:See complete output in console
android android-studio gradle android-gradle android-support-library
5个回答
18
投票

为React Native自动修复所有android到androidx的问题,(先决条件npx)

在ProjectFolder / android / gradle.properties的gradle.properties文件中将以下两个标志添加到true

android.useAndroidX=true
android.enableJetifier=true

执行

npm install --save-dev jetifier
npx jetify
npx react-native run-android

在package.json中,将以下内容添加到脚本中

  "postinstall" : "npx jetify"

更多信息在https://github.com/mikehardy/jetifier

更新:现在内置于react-native 0.60中。如果您迁移到react-native 0.60,则不需要此步骤。 - https://facebook.github.io/react-native/blog/2019/07/03/version-60#androidx-support


7
投票

打开gradle.properties并使用以下代码:

android.useAndroidX=false
android.enableJetifier=false

或者U也可以使用这些依赖项:

implementation 'androidx.appcompat:appcompat:1.0.2'
 implementation 'androidx.annotation:annotation:1.0.2'

6
投票

在gradle中使用实现androidx.appcompat:appcompat:1.0.2然后

在类导入中将import android.support.annotation.Nullable;更改为import androidx.annotation.NonNull;


2
投票

注释来自support's library包装的android.support.annotation

作为另一种选择,您可以使用@NonNull注释,表示参数,字段或方法返回值永远不能为空。 它是从import android.support.annotation.NonNull;进口的


2
投票

你可以找到heresupport-annotationslibrary的官方javadoc。

错误:(3,31)错误:包com.android.annotations不存在

正如您所看到的,所有类都在同一个包中android.support.annotation而不是com.android.annotations

错误:(7,2)错误:找不到符号类NonNullByDefault

此类包中也不存在类NonNullByDefault


1
投票

在我的情况下,我不得不使用

import androidx.annotation...

代替

import android.annotation...

我迁移到AndroidX并忘了改变它。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.