修改minSdkVersion从25到26后APK无法打开但USB可运行求助
问题:minSdkVersion升级到26后APK无法启动,但调试模式正常运行
将Android项目的minSdkVersion从25修改为26后,生成的APK安装包无法正常打开,但通过USB调试安装运行应用一切正常。尝试多种方法未解决,求帮助。
以下是项目的Gradle构建配置:
apply plugin: 'com.android.application' buildscript { repositories { mavenCentral() } } repositories { mavenLocal() mavenCentral() maven {url "https://jitpack.io"} } android { namespace 'com.polidea.rxandroidble2.sample' compileSdkVersion 34 defaultConfig { applicationId "com.polidea.rxandroidble.sample" minSdkVersion 26 targetSdkVersion 34 versionCode 1 versionName "1.0" multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility = rootProject.ext.sourceCompatibilityVersion targetCompatibility = rootProject.ext.targetCompatibilityVersion } } configurations.all { resolutionStrategy.cacheChangingModulesFor 0, 'seconds' } dependencies { implementation 'com.github.GrenderG:Toasty:1.5.2' implementation project(path: ':rxandroidble') implementation rootProject.ext.libs.appcompat implementation rootProject.ext.libs.material_design implementation 'com.android.support:multidex:1.0.3' implementation 'androidx.camera:camera-core:1.3.2' implementation 'androidx.camera:camera-camera2:1.3.1' implementation 'androidx.camera:camera-extensions:1.3.1' implementation 'androidx.camera:camera-lifecycle:1.3.1' implementation 'androidx.camera:camera-video:1.3.1' implementation 'androidx.camera:camera-view:1.3.1' implementation 'com.jakewharton:butterknife:10.2.3' implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation "com.google.android.gms:play-services-location:21.1.0" implementation 'com.google.mlkit:vision-common:17.3.0' implementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.0' implementation 'com.google.android.gms:play-services-code-scanner:16.1.0' implementation 'com.google.android.gms:play-services-vision-common:19.1.3' //====== this package is disaster ========== implementation 'com.google.mlkit:text-recognition:16.0.0' implementation'com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.0@aar' implementation'com.google.mlkit:common:18.10.0@aar' implementation'com.google.android.gms:play-services-basement:18.3.0@aar' implementation'com.google.android.gms:play-services-vision:20.1.3' implementation'com.google.firebase:firebase-components:17.1.5' implementation 'org.apache.poi:poi-ooxml:5.1.0' implementation 'com.opencsv:opencsv:5.4' def room_version = "2.6.1" implementation "androidx.room:room-runtime:$room_version" // implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.1.2' //implementation 'androidx.appcompat:appcompat:1.6.1' // testImplementation 'junit:junit:4.13.2' // androidTestImplementation 'androidx.test.ext:junit:1.1.5' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3' implementation rootProject.ext.libs.rxandroid implementation rootProject.ext.libs.rxjava2 implementation rootProject.ext.libs.rxjava_binding implementation rootProject.ext.libs.rxjava_replayingshare }
解决措施
1. 修复MultiDex依赖冲突
当前使用的是旧版Support库的Multidex,与项目中的AndroidX依赖(如androidx.camera系列)不兼容,minSdk升级到26后需切换为AndroidX版本:
// 替换原有multidex依赖 implementation 'androidx.multidex:multidex:2.0.1'
同时确保自定义Application类继承MultiDexApplication,或在attachBaseContext方法中添加:
@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }
2. 清理重复依赖与适配Java版本
- 移除重复的ML Kit相关依赖,避免版本冲突:
// 删除以下两行重复依赖 implementation'com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.0@aar' implementation'com.google.android.gms:play-services-basement:18.3.0@aar' - 明确指定Java 8编译版本(minSdk26要求最低Java 8),替换原有的compileOptions配置:
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }
3. 检查签名与权限适配
- 确认正式APK的签名文件配置正确,调试与正式签名不一致可能导致组件加载失败;
- 检查
AndroidManifest.xml中API26+的权限适配:比如位置权限需动态申请,后台定位需添加ACCESS_BACKGROUND_LOCATION权限,前台服务需声明android:foregroundServiceType。
4. 抓取启动日志定位根源
安装正式APK后,通过adb命令抓取错误日志:
adb logcat *:E
尝试打开应用,查看输出的错误信息(如ClassNotFoundException、IllegalStateException等),根据日志精准定位问题。
5. 排查ProGuard配置
虽然minifyEnabled false,但默认混淆规则仍可能影响代码,可暂时关闭资源压缩测试:
release { minifyEnabled false shrinkResources false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
若问题解决,再逐步排查proguard-rules.pro中的错误规则。
内容的提问来源于stack exchange,提问作者mmd karim
相关产品推荐
相关产品推荐

