错误: 程序包androidx.fragment.app不存在 import android
错误: 程序包androidx.fragment.app不存在 import androidx.fragment.app.FragmentActivity; 这个是什么错?dependencies { //implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(path: ':libscan') //noinspection GradleCompatible implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.alibaba:fastjson:1.2.68' implementation 'androidx.test:monitor:1.4.0' implementation 'androidx.test:core:1.4.0' // implementation 'androidx.fragment:fragment:1.6.2' // 或最新版本 //implementation files('libs\\android.jar') compileOnly files('libs\\android.jar') implementation files('libs\\juniversalchardet-1.0.3.jar') implementation files('libs/lfilepickerlibrary-debug.aar') // implementation 'androidx.camera:camera-lifecycle:1.0.0' // implementation 'androidx.camera:camera-core:1.0.0' // implementation 'androidx.camera:camera-camera2:1.0.0' // implementation 'androidx.camera:camera-lifecycle:1.0.0' testImplementation 'junit:junit:4.12' //noinspection GradleCompatible implementation 'com.android.support:design:28.0.0' // implementation 'com.github.leonHua:LFilePicker:1.8.0' // implementation 'com.leon:lfilepickerlibrary:1.7.9' // implementation 'com.leon:lfilepickerlibrary:1.8.0' implementation 'androidx.test:core:1.4.0' implementation 'androidx.test:core-ktx:1.4.0' // androidTestImplementation 'androidx.test:core:1.4.0' // implementation 'net.sourceforge.streamsupport:streamsupport:1.7.1' } def static releaseTime() { return new Date().format("yyyyMMdd", TimeZone.getTimeZone("GMT+8")) } gradle文件是这样的会报错。那么 gradle文件这样就不报错dependencies { //implementation 'com.android.support:support-v4:24.2.1' // implementation 'com.android.support:support-v4:28.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.alibaba:fastjson:1.2.68' //implementation 'androidx.lifecycle:lifecycle-common-jvm:2.8.7' compileOnly files('libs\\framework.jar') implementation files('libs\\juniversalchardet-1.0.3.jar') // implementation 'androidx.core:core:1.6.0' // implementation project(':sdklibrary') // Java language implementation }?
你这个错误:
错误: 程序包androidx.fragment.app不存在
import androidx.fragment.app.FragmentActivity;
说明:你的项目中没有引入 androidx.fragment
所属库,但你又用了 FragmentActivity
。
✅ 问题分析
你当前 build.gradle
文件中,混用了 android.support
和 androidx
,导致编译环境不一致。
-
androidx.fragment.app.FragmentActivity
是 AndroidX 体系的; -
com.android.support:appcompat-v7:28.0.0
是老的support
体系; -
两者不能混用,否则就会报找不到类。
✅ 正确做法(根据你想要的方案)
✅ 如果你要使用 AndroidX(推荐):
请使用如下 build.gradle
配置(清理掉所有 com.android.support:*
):
android {compileSdkVersion 33defaultConfig {applicationId "your.package.name"minSdkVersion 21targetSdkVersion 33versionCode 1versionName "1.0"// 启用 AndroidXmultiDexEnabled true}buildFeatures {viewBinding true}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}// AndroidX 开关(仅旧项目升级时需要)buildProperties {android.useAndroidX=trueandroid.enableJetifier=true}
}dependencies {implementation 'androidx.appcompat:appcompat:1.3.1'implementation 'androidx.constraintlayout:constraintlayout:2.1.4'implementation 'androidx.legacy:legacy-support-v4:1.0.0'implementation 'com.google.android.material:material:1.4.0'implementation 'androidx.fragment:fragment:1.6.2' // ✅ 引入 FragmentActivityimplementation 'com.alibaba:fastjson:1.2.68'compileOnly files('libs/framework.jar')implementation files('libs/juniversalchardet-1.0.3.jar')testImplementation 'junit:junit:4.13.2'
}
✅ 如果你仍想坚持使用 support库(不推荐):
-
删掉所有
androidx.*
的依赖; -
保留:
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
但此方式已经是旧架构(Google 早已弃用),不建议继续使用。
✅ 建议结论
你上面最后一个不报错的配置,其实是已经 完全迁移到 AndroidX 的方式:
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.fragment:fragment:1.6.2'
那你需要保证:
-
所有 Java 文件中只使用
androidx.*
类; -
不要混用
com.android.support.*
。
🧹 额外建议:清理旧缓存和强制同步
执行以下操作确保稳定:
./gradlew clean
./gradlew build --refresh-dependencies
如你还需要 build.gradle
自动判断是否使用 support 还是 androidx、