由于今年AndroidQ的发布以及应用市场对sdk版本的要求,需要对目前公司维护的老旧的Android项目进行AndroidQ适配。
遇到的问题:
1、提升Gradle和SDK版本号
2、AndroidManifest.xml增加权限
3、动态获取权限
4、项目升级时AndroidQ解决方案
5、琐碎问题
解决方案:
1、提升Gradle和SDK版本号
project build.gradle:
提升classpath依赖的版本号
app build.gradle
compileSdkVersion 和 buildToolsVersion 提升到相应版本号
并增加:minSdkVersion
targetSdkVersion
versionCode
versionName 如图
2、AndroidManifest.xml增加权限
原有权限不需要进行修改,增加新的权限
3、动态获取权限
再首页进行动态申请权限(涉及到版本检测和升级功能)
private static final String[]PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_PHONE_STATE
};//所需权限数组
private static final int authRequestCode =1;
//申请权限
private void initpermission() {
if (Build.VERSION.SDK_INT >=23) {
if (!hasBasePhoneAuth()) {
this.requestPermissions(PERMISSIONS_STORAGE, authRequestCode);
}else {
if (isUpdate.equals("true")) {//确定更新
updateClick =false;
checkUpdate();
}
}
}else {
if (isUpdate.equals("true")) {//确定更新
updateClick =false;
checkUpdate();
}
}
}
//检查有权限
private boolean hasBasePhoneAuth() {
PackageManager pm =this.getPackageManager();
for (String auth :PERMISSIONS_STORAGE) {
if (pm.checkPermission(auth, this.getPackageName()) != PackageManager
.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
//申请权限回调
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case authRequestCode:
System.out.println("获取权限是否成功的数量:" + grantResults.length);
for (int ret : grantResults) {
if (ret ==0) {
continue;
}else {
return;
}
}
initpermission();
break;
}
}
4、项目升级时AndroidQ解决方案
AndroidManifest.xml的修改
android:name="android.support.v4.content.FileProvider" android:authorities="包名(applicationId).fileProvider" android:exported="false" android:grantUriPermissions="true"> android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
file_paths内容
name="my_files" path="mazaiting/"/> name="my_cache" path="mazaiting/"/> name="external-files-path" path="mazaiting/"/> name="name" path="mazaiting/" /> name="my_external_path" path="mazaiting/"/> name="files_root" path="Android/data/包名(applicationId)/"/> name="external_storage_root" path="."/> /** * 安装APK文件 */ private void installApk() { File apkfile =new File(UtilTool.APK_DOWNLOAD_PATH, apkName); if (!apkfile.exists()) { return; } //获取下载文件的Uri Intent intent =new Intent(Intent.ACTION_VIEW); //7.0 以上需要FileProvider进行设置 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri apkUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID +".fileProvider", apkfile); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.setDataAndType(apkUri, mContext.getContentResolver().getType(apkUri)); }else { intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive"); } mContext.startActivity(intent); } 升级到Android Q之后项目中获取imei的方法会进行报错 /** * 获取imei号 * * @param context * @return */ public static StringgetIMEI(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return null; } String imei = telephonyManager.getDeviceId(); return imei; } 百度地图 引用baidumapapi_v3_5_0.jar包中的方法时原有方法不能使用了 要引用另外一个 SDKInitializer.initialize(this);UpdateDataManager.java 中apk安装方法
5、琐碎问题
UtilTool.java
BaiduMap
以上是公司一些陈旧的项目,偶尔进行维护细节优化时需要进行Android Q适配的解决方案