简体   繁体   中英

can't import android.support.v4.app notificationcompat in android studio?

I was learning android notification tutorial using this link but when I started using

 Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_1_ID);

this code, I can't import android.support.v4.app. NotificationCompat, how to do that? It only imports

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.Notification;

  1. Do I need to add something in build.gradle?
  2. I am using Android Studio 2021.1.1, is that the problem?

androidx is new support library for the replacement v4.support library. And androidx is recommended. I suggest you to use search filter to find tutorial later than 2020~2021.

However you can still use v4.support library by checking "Use legacy android.support library" option while creating a project.

在此处输入图像描述

While this is not enough, because latest build tools will not support deprecated libraries, you have to keep compleSdkVersion 28 or below. You are also required to downgrade your Android Gradle Plugin to 3.6.4 and gradle wrapper to 6.0

Make sure these lines are not present in gradle.properties

android.useAndroidX=true
android.enableJetifier=true

Now you can follow the above tutorial.

Use

android.arch.core:core
android.arch.core:common

instead of 

androidx.arch.core:core-common
androidx.arch.core:core

Here is the full list of equivalent artifacts of support library to androidx

I do not recommend you to follow above tutorial find latest tutorial. Tech is rapidly changing, following an outdated tutorial might slow your learning process and keep you behind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM