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

Mos*_*ses 27 android gradle android-studio android-gradle-plugin react-native

我有以下课程

import com.android.annotations.NonNullByDefault;

@NonNullByDefault
public final class Log {
    ...
}
Run Code Online (Sandbox Code Playgroud)

这是我的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'
}
Run Code Online (Sandbox Code Playgroud)

在Android Studio中,我的课程没有警告

在此处输入图片说明

但是,当我尝试构建和运行我的应用程序时,我从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
Run Code Online (Sandbox Code Playgroud)

Pav*_*rre 55

要自动修复React Native的所有android至androidx问题,(前提条件npx)

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

android.useAndroidX=true
android.enableJetifier=true
Run Code Online (Sandbox Code Playgroud)

执行

npm install --save-dev jetifier
npx jetify
npx react-native run-android
Run Code Online (Sandbox Code Playgroud)

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

  "postinstall" : "npx jetify"
Run Code Online (Sandbox Code Playgroud)

更多信息请参见https://github.com/mikehardy/jetifier

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


Sar*_*hal 17

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

更改import android.support.annotation.Nullable;import androidx.annotation.NonNull;类导入

  • 好的,我在库的不同文件中有超过 100 个导入,该怎么办? (2认同)
  • @RajnishCoder 使用 jetifier 为你做这件事。 (2认同)

use*_*776 14

I had similar issues when migrating to androidx.

If by adding the below two lines to gradle.properties did not solve the issue

android.useAndroidX=true
android.enableJetifier=true
Run Code Online (Sandbox Code Playgroud)

Then try

  1. With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar (developer.android.com)

If you still run into issues with migration then manually try to replace the libraries which are causing the issue.

For example

If you have an issue with android.support.annotation.NonNull change it to androidx.annotation.NonNull as indicated in the below class mappings table.

Class Mappings

Maven Artifact Mappings


rib*_*bit 13

就我而言,我不得不使用

import androidx.annotation...
Run Code Online (Sandbox Code Playgroud)

代替

import android.annotation...
Run Code Online (Sandbox Code Playgroud)

我迁移到 AndroidX 并忘记更改它。


Beh*_*z.M 11

Open gradle.properties and use following code:

android.useAndroidX=false
android.enableJetifier=false

Run Code Online (Sandbox Code Playgroud)

or U can use these dependencies too:

implementation 'androidx.appcompat:appcompat:1.0.2'
 implementation 'androidx.annotation:annotation:1.0.2'
Run Code Online (Sandbox Code Playgroud)


小智 9

对于 Ionic,试试这个:

ionic cordova plugin add cordova-plugin-androidx
ionic cordova plugin add cordova-plugin-androidx-adapter
Run Code Online (Sandbox Code Playgroud)

出现错误是因为该应用程序未使用 androidX,但这些插件解决了错误。


Bal*_*aji 6

只需安装(它会自动修复)

npm install --save-dev jetifier <--------avoid if aleardy installed start from below lines
npx jetify
Run Code Online (Sandbox Code Playgroud)

然后运行

npx cap sync
Run Code Online (Sandbox Code Playgroud)

最后

  npx react-native run-android

  
Run Code Online (Sandbox Code Playgroud)


San*_*eet 3

注释来自support's library打包在android.support.annotation.

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


归档时间:

查看次数:

45894 次

最近记录:

6 年,2 月 前