使用API​​ 28和“ androidx.appcompat”库项目时,找不到“ AppCompatActivity”符号

Inz*_* IT 9 android android-appcompat gradle android-studio androidx

我将构建和目标版本更新为28(Pie),并替换了相关的依赖项。现在我的项目说在上找不到符号AppCompatActivity。我试图

  • 清洁项目
  • 重建项目
  • 无效的缓存/重启

但是结果是一样的。而且,当我在活动类中的extends关键字之后尝试Ctrl+时Space,没有任何"AppCompatActivity建议。我试图调查它是否存在于libraries文件夹中,是否存在于文件夹中。

在此处输入图片说明

现在,我应该怎么做才能使其正常工作?如果库有任何变化/替代,androidx请告诉我。这是我的完整build.gradle文件

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.invogen.messagingapp"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
//    implementation 'com.android.support:appcompat-v7:28.0.0'
//    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//    implementation 'com.android.support:design:28.0.0'
//    implementation 'com.android.support:support-v4:28.0.0'

    // Libs for newer API 28
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0-alpha01'
    implementation 'androidx.cardview:cardview:1.0.0'


    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    // Libs for Firebase Functionality
    implementation 'com.google.firebase:firebase-core:16.0.5'
//    implementation 'com.google.firebase:firebase-database:16.0.4'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
    implementation 'com.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-storage:16.0.4'

    // Lib for Firebase UI Elements
    implementation 'com.firebaseui:firebase-ui-database:4.2.1'

    // Libs for QR Code
    implementation 'com.google.zxing:core:3.2.1'
    implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'

    // Lib for Circle Image View (Profile Image)
    implementation 'de.hdodenhof:circleimageview:2.2.0'

    // Lib for Loading Images
    implementation 'com.squareup.picasso:picasso:2.71828'

    //Lib for Cropping Images
    api 'com.theartofdev.edmodo:android-image-cropper:2.8.+'


}
apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)

其他一些帖子建议在Manifest文件中添加以下两个参数

android:appComponentFactory="anystrings be placeholder"
tools:replace="android:appComponentFactory"
Run Code Online (Sandbox Code Playgroud)

但是由于这两行项目同步出现多个错误,Android Studio说

编译失败;有关详细信息,请参见编译器错误输出。

如果我必须为问题添加更多细节,请告诉我。

iam*_*cxl 16

您应该替换目标类。

例如。

import android.support.v7.app.AppCompatActivity;
Run Code Online (Sandbox Code Playgroud)

替换为:

import androidx.appcompat.app.AppCompatActivity;
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案可以解决此问题,但确实需要进行上述gradle更新:'api'com.google.android.material:material:1.0.0'`。对于像我这样在AndroidX的*第1天​​工作的人-我没有意识到这是原始问题中列出的关键细节,这是解决方案的一部分。感谢:/sf/answers/3775580021/ (2认同)

Inz*_* IT 5

编辑:现在您可以轻松地将项目迁移到androidx,只需Refactor => Migrate to Androidx从菜单栏单击即可。 在此处输入图片说明

以前我做如下。
With Clean and buildRebuild projectandroid studio不会清除未使用的导入(例如从中导入),android.support.v7因此我从所有活动中手动将其全部删除。现在,android studio会AppCompatActivity从正确的库中建议androidx.appcompat

希望如此对别人有帮助。


小智 5

在你的 gradle 属性中添加以下行:

android.useAndroidX=true android.enableJetifier=true

这会将您的项目升级到 Android X。