Android 10升级至Android 11后关于startActivity启动应用抛异常处理方法

最近项目升级到android11后,原先在android10上正常运行的DOU  apk在android11上全体趴窝,通过debug定位到在拉起app的地方抛出异常,导致DOU运行失败。

通过咨询其它人发现在Android11及以后所使用的uiautomator必须是2.2.0及以后的依赖库才行,sync now同步完成后发现所有代码中一片红,没事一个一个改吧。

首先在app目录下将build.gradle中的依赖改过来,如下是android11上且与高通可用的依赖库:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.1.0'
    implementation 'androidx.navigation:navigation-ui:2.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    testImplementation 'org.testng:testng:6.9.6'
}

以下是之前android10上面的依赖库:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
//    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

通过上面两个对比发现,依赖的库变化很大,也就意味着在代码中改动的地方会比较多。

 

以下是完整的app里面build.gradle中的配置,相比以前android10中改动地方(红色标记)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.dou.jjyy"
        minSdkVersion 26
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildToolsVersion '29.0.1'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.1.0'
    implementation 'androidx.navigation:navigation-ui:2.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    testImplementation 'org.testng:testng:6.9.6'
}

 

最后将所有代码中报错的地方依次修改完成,最后再重新编译打包安装运行,可以正常拉起app,完美解决问题。
posted @ 2021-12-01 10:06  iSZ  阅读(1083)  评论(0编辑  收藏  举报