You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加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!

一、Fixing Android Resource Linking Failures

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:appcompat dependency to a stable, recent version, and align other AndroidX library versions to match. For example:
    Change implementation 'androidx.appcompat:appcompat:1.0.0' to implementation 'androidx.appcompat:appcompat:1.6.1'
  • Update other AndroidX libraries like CardView and RecyclerView to compatible versions:
    • androidx.cardview:cardview:1.2.1
    • androidx.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:

  1. Add the AndroidX Core dependency to your build.gradle:
    implementation 'androidx.core:core:1.10.1'
    
  2. 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):
    <?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>
    
    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).
二、Fixing Fragment Code Issues

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:

  1. Add the Annotation dependency:
    implementation 'androidx.annotation:annotation:1.6.0'
    
  2. Replace @Nonnull with @NonNull in your code, and import the correct class:
    import androidx.annotation.NonNull;
    
三、Updated Gradle Configuration

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'
}
四、Final Checks
  • Ensure your gradle.properties file 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, then Build > Rebuild Project to clear cached errors.
  • Double-check layout files for old support library references (e.g., replace android.support.v7.widget.RecyclerView with androidx.recyclerview.widget.RecyclerView).

内容的提问来源于stack exchange,提问作者Bata Kos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 23:52:39