最近項目升級到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,完美解決問題。