添加Google AdMob依赖后项目编译失败,迁移AndroidX报错求助
Hey there, let's work through these AndroidX migration and AdMob issues step by step—they're super common when making this transition, so you're not alone!
These errors happen mostly due to mismatched AndroidX dependency versions or missing required libraries. Let's tackle them one by one.
1. Missing alpha Attribute in System Color Resources
The old AppCompat version you're using (1.0.0) is outdated and doesn't support the alpha attribute in modern color resources. Upgrading your AndroidX dependencies to consistent, newer versions will fix this.
Solution:
- Update your
androidx.appcompat:appcompatdependency to a stable, recent version, and align other AndroidX library versions to match. For example:
Changeimplementation 'androidx.appcompat:appcompat:1.0.0'toimplementation 'androidx.appcompat:appcompat:1.6.1' - Update other AndroidX libraries like CardView and RecyclerView to compatible versions:
androidx.cardview:cardview:1.2.1androidx.recyclerview:recyclerview:1.3.2
2. Missing fontProvider Attributes in Custom Font Files
These attributes are part of the AndroidX Core library, which you're currently missing. Also, double-check your font XML files—fontProvider is only for remote fonts (like Google Fonts fetched via a provider), not local font files.
Solutions:
- Add the AndroidX Core dependency to your
build.gradle:implementation 'androidx.core:core:1.10.1' - Fix local font XML files: If you're using local fonts, replace your current font XML structure with this (pointing to your actual font file):
If you intended to use remote fonts, make sure you’ve set up the font provider correctly (but this is rarely needed for local assets).<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/abril_fatface_regular" /> <!-- Replace with your local font file name --> </font-family>
AndroidX changed Fragment APIs significantly—here's how to update your code:
1. getActivity().getFragmentManager() Not Recognized
In AndroidX, Fragment's fragment manager methods were renamed. Inside a Fragment, use getParentFragmentManager() instead of getActivity().getFragmentManager().
Before:
getActivity().getFragmentManager()
After:
getParentFragmentManager()
(If you need the activity's fragment manager from within an Activity, use getSupportFragmentManager() instead.)
2. @Nonnull Annotation Not Recognized
AndroidX uses @NonNull instead of the old @Nonnull annotation from the support library. You'll also need to add the AndroidX Annotation dependency to access it.
Solutions:
- Add the Annotation dependency:
implementation 'androidx.annotation:annotation:1.6.0' - Replace
@Nonnullwith@NonNullin your code, and import the correct class:import androidx.annotation.NonNull;
Here's a cleaned-up, version-aligned build.gradle file that fixes most compatibility issues:
apply plugin: 'com.android.application' android { compileSdkVersion 33 // Upgrade to a modern SDK version for better compatibility defaultConfig { applicationId "com.samex.android.stopsmokingnow" minSdkVersion 16 targetSdkVersion 33 // Match compileSdkVersion for best practices versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.cardview:cardview:1.2.1' implementation 'com.daimajia.easing:library:2.0@aar' implementation 'com.daimajia.androidanimations:library:2.3@aar' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.recyclerview:recyclerview:1.3.2' implementation 'androidx.core:core:1.10.1' implementation 'androidx.annotation:annotation:1.6.0' implementation 'com.google.android.gms:play-services-ads:22.4.0' // Upgrade AdMob to latest stable version testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test:runner:1.5.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
- Ensure your
gradle.propertiesfile has these AndroidX migration flags (critical for Jetifier to convert old support libraries):android.useAndroidX=true android.enableJetifier=true - Clean and rebuild your project: Go to
Build > Clean Project, thenBuild > Rebuild Projectto clear cached errors. - Double-check layout files for old support library references (e.g., replace
android.support.v7.widget.RecyclerViewwithandroidx.recyclerview.widget.RecyclerView).
内容的提问来源于stack exchange,提问作者Bata Kos

