升级Gradle与SDK后Android 13设备应用卡顿求助
Android 13设备卡顿排查(Gradle/SDK升级后)
原本基于Gradle 7.2、compile SDK 32构建的应用,升级至Gradle 8.0.1、compile SDK 34后,Android 12设备运行正常,但Android 13设备出现卡顿现象。以下是相关配置与登录核心逻辑:
app模块build.gradle
android { compileSdk 34 defaultConfig { applicationId "com.iqnext.irlearner" minSdk 26 targetSdk 34 versionCode 1 versionName "1.2.0.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } namespace 'com.iqnext.irlearner' }
项目根目录build.gradle
plugins { id 'com.android.application' version '8.1.0' apply false id 'com.android.library' version '8.1.0' apply false } task clean(type: Delete) { delete rootProject.buildDir }
AndroidManifest.xml(应用节点)
<application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher_round" android:label="@string/app_name" android:networkSecurityConfig="@xml/network_security_config" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Material3.DayNight.NoActionBar" android:requestLegacyExternalStorage="true" android:enableOnBackInvokedCallback="true"> <activity ...Activities </activity> </application>
LoginActivity核心代码(onCreate方法)
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLayout = findViewById(R.id.main_layout); progress = new ProgressDialog(this); progress.dismiss(); progress.cancel(); setContentView(R.layout.activity_login); loginBtn=findViewById(R.id.login); user = findViewById(R.id.username); pass = findViewById(R.id.password); url = findViewById(R.id.url); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.showSoftInput(user, 0); loginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { UserLogin userLogin= new UserLogin(user.getText().toString().trim(), pass.getText().toString().trim()); RetrofitClient apiClient = new RetrofitClient(); Retrofit retrofit = apiClient.getClient(getApplicationContext()); Api apiInterface = retrofit.create(Api.class); progress.setTitle("Loading"); progress.setMessage("Logging You In..."); progress.setCancelable(false); // disable dismiss by tapping outside of the dialog progress.show(); Call<IQnextResponse<LoginResponse>> call = apiInterface.loginToServer(userLogin); call.enqueue(new Callback<IQnextResponse<LoginResponse>> () { @Override public void onResponse(Call<IQnextResponse<LoginResponse>> call, Response<IQnextResponse<LoginResponse>> response) { if(response.isSuccessful() && response.body() != null) { if(response.body().getSuccess() != null && response.body().getSuccess().getData() != null) { int returnCode = response.body().getSuccess().getReturnCode(); if(returnCode==222){ String msg = response.body().getSuccess().getData().getToken(); navigateToDashboard(response, userLogin.getUsername()); } else if(returnCode==223){ //TODO: Navigate to OTP Screen String msg = response.body().getSuccess().getData().getToken(); navigateToOTP(response, userLogin); } } else { if(response.body().getError() != null && response.body().getError().getMessage() != null) { Log.d("LOGIN_FAILED",response.body().getError().getMessage()); Toast.makeText(getApplicationContext(), response.body().getError().getMessage(), Toast.LENGTH_LONG).show(); } } } progress.dismiss(); progress.cancel(); } @Override public void onFailure(Call<IQnextResponse<LoginResponse>> call, Throwable t) { Toast.makeText(getApplicationContext(), "Something Went Wrong", Toast.LENGTH_LONG).show(); progress.dismiss(); progress.cancel(); } }); } }); url.isClickable(); url.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { configureURL(); } }); askPermission(); }
卡顿排查方向与修复建议
1. UI线程操作优化
- 检查
askPermission()方法:Android 13权限申请逻辑有变更,若该方法在主线程同步处理权限校验/申请,会阻塞UI导致卡顿。建议改为异步回调模式,避免主线程等待。 - Retrofit实例复用:每次点击登录都创建
RetrofitClient新实例,会重复初始化网络组件,增加内存开销与对象创建耗时。改为单例模式复用Retrofit实例。
2. Android 13特定适配
- 通知权限处理:targetSdk 34后,Android 13强制要求
POST_NOTIFICATIONS权限,若应用未授权时频繁触发Toast/通知,会触发系统限流导致卡顿。在应用启动或登录前完成权限申请。 - 后台启动限制:Android 13对后台启动Activity限制更严格,
navigateToDashboard/navigateToOTP需确保在主线程执行,且跳转逻辑符合后台启动规则(如使用PendingIntent时设置正确的FLAG_IMMUTABLE/FLAG_MUTABLE)。
3. Gradle配置对齐
当前Gradle插件版本为8.1.0,但Gradle版本是8.0.1,版本不匹配可能导致编译优化失效。将Gradle版本升级至8.1.0,与插件版本保持一致。
4. 代码细节优化
ProgressDialog重复调用dismiss()和cancel(),可能引发UI绘制冲突,保留dismiss()即可。- 开启R8优化:当前release版本
minifyEnabled false,建议开启minifyEnabled true并配置R8规则,移除无用代码,降低运行时内存占用。
5. 性能工具定位
使用Android Studio Profiler工具,在Android 13设备上录制CPU/内存轨迹,查看卡顿发生时的线程状态,精准定位阻塞点(如UI线程是否被网络IO、文件操作占用)。
内容的提问来源于stack exchange,提问作者0x4E43
相关产品推荐
相关产品推荐

