适配安卓7.0以上,java层读取应用数据目录下的方法;安卓8.0以上,添加应用允许安装未知来源apk的权限。
最近各大市场开始要求将安卓的api等级设置成28,即需要我们开发者完成适配。
对于unity这种非纯安卓的app,涉及到的升级改动可以说非常的少,一般只需要改改一些配置即可~
好吧,升级~,把AndroidManifest.xml中的targetSdkVersion改为28~完成,再把打包的java sdk改成sdk9,jdk改成1.8,完成~~
重新打包,把一些报错解决,完美出包。
试了一下,里面的功能测试通过,重新提审,ok,过审上架。
但是,很容易忽略了一些东西,游戏进行整包更新的时候,需要打开从服务器下载的保存在内部目录的apk文件,这里的读取在7.0以上需要为其添加相关的适配代码,7.0以下不需要相关的适配权限代码,如下:
public static void InstallApk(string path){
File file = new File(path);
if(!file.exists()) return;
Intent inten = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
}
在7.0以后需要通过FileProvider获得临时的读取权限,使用FIleProvider还需要在AndroidManifest.xml配置路径。
public static void InstallApk(string path){
File file = new File(path);
if(!file.exists()) return;
Intent inten = new Intent(Intent.ACTION_VIEW);
Uri uri = null;
if(Build.VERSIONSDK_INT >= Build.VERSION_CODES.N){
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//检查权限
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
}else{
uri = Uri.fromFile(file);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
<!-- 在AndroidManifest.xml 的 application 任意位置添加 定义FileProvider的配置 -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="填包名"+".provider"
android:exported="false"
android:grantUriPermission="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" /> <!-- 这里需要在这个路径下放这个文件 -->
</provider>
<!-- 根据配置新建的 provider_path.xml -->
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path>
name="external_files_path"
path="Download" />
<external-path
name="external_files"
path="." />
</paths>
在8.0以上,还需要得到安装未知来源apk的权限。
在AndroidManifest.xml中添加
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
最后还要记得引入android-support-v4.jar这个包,FileProvider依赖这个,不然在打jar包的时候会报错。