最近做项目有个需求是要Android应用内强更包体。Google一波直接选了一个android原生开源项目
GitHub - yangchong211/YCUpdateApp: 轻量级版本更新弹窗,弹窗上支持更新进度条,可以设置普通更新或者强制更新。解决8.0以上通知栏不显示问题,解决7.0以上安装apk异常,下载完成则会自动提示安装;下载异常,失败,错误等状态,支持重启下载任务;还支持自定义下载路径。代码量少,调用简单……
但是也是需要接入到Unity中,趟了一些小坑记录下实现步骤
步骤1
Unity接入插件,两种方式:
(1)mainTemplateGradle中直接引用,这种方式简单省事
implementation 'cn.yc:YCUpdateLib:1.1.0'
(2)下载源码后自己打出AAR文件给项目引用。这周种方式可以自定义修改内容,比如更新面板的样式,为了满足策划,我选择的是这种:
步骤2
引用该插件所需的依赖:在mainTemplateGradle中加入
api 'com.liulishuo.filedownloader:library:1.7.5'
这是一个坑,Github使用说明中并没有写这点,一直报错闪退,找了半天看源码才找到需要引用这么个东西
步骤3
写个Java调用更新的页面,我这写了个UpadateActivity,该Activity的唤起调用由MainActivity唤起
UpdateActivity:
package com.match.fruits;import android.Manifest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.ycbjie.ycupdatelib.*;public class UpdateActivity extends AppCompatActivity {//private static final String TAG = AppConst.TAG_PRE + "GameMainActivity";@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);desc = getIntent().getStringExtra("Desc");url = getIntent().getStringExtra("Url");packageName = getIntent().getStringExtra("PackageName");//translucentActivity(this);UpdateAPK();}private String desc;private String url;private String packageName;private static final String apkName = "Game";// private static final String firstUrl = "https://xpchy-1301864052.cos.ap-beijing.myqcloud.com/fruitmatch/KuaiShou-0906-1.0.4(1).apk";private static final String[] mPermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};public void UpdateAPK(){PermissionUtils.init(this);boolean granted = PermissionUtils.isGranted(mPermission);if(!granted){PermissionUtils permission = PermissionUtils.permission(mPermission);permission.callback(new PermissionUtils.SimpleCallback() {@Overridepublic void onGranted() {}@Overridepublic void onDenied() {PermissionUtils.openAppSettings();Toast.makeText(UpdateActivity.this,"请允许更新权限",Toast.LENGTH_SHORT).show();}});permission.request();}//设置自定义下载文件路径// AppUpdateUtils.APP_UPDATE_DOWN_APK_PATH = "apk" + File.separator + "downApk";UpdateFragment.showFragment(UpdateActivity.this,true, url, apkName, desc, packageName ,null);}}
MainActivity或其他总控代码中调用唤起强更功能的代码,该函数由Unity中触发:
public void UpdateAPK(String url, String desc, String packageName){Intent intent = new Intent(GameMainActivity.this, UpdateActivity.class);intent.putExtra("Desc", desc);intent.putExtra("Url", url);intent.putExtra("PackageName", packageName);startActivity(intent);}
步骤4
配置UpadteActivity:
为了该Activity透明能看到游戏背景(Unity主Activity),所以自己写了个Style,在Style.xml中加入:
<style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:windowNoTitle">true</item><item name="android:windowActionBar">false</item><item name="android:windowFullscreen">true</item><item name="android:windowContentOverlay">@null</item><item name="android:windowBackground">@android:color/transparent</item><item name="android:colorBackgroundCacheHint">@null</item><item name="android:windowIsTranslucent">true</item></style>
Androidmanifet中加入该Activity并引用Style的透明theme
注意一定要用的是Theme.AppCompt,否则会报错崩溃,参考以下解答:填坑
android - How to fix: "You need to use a Theme.AppCompat theme (or descendant) with this activity" - Stack Overflow
Android how to get AppCompat.Translucent type theme with support actionbar? - Stack Overflow
步骤5
填坑报错:Using WebView from more than one process at once with the same data directory is not supported
在主Application中加入(如果自己没自定义Application,那就放在总控代码里,反正核心是要设置webview)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {String processName = getProcessName(this);String packageName = this.getPackageName();if (!packageName.equals(processName)) {WebView.setDataDirectorySuffix(processName);}}private String getProcessName(Context context) {if (context == null) return null;ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {if (processInfo.pid == android.os.Process.myPid()) {return processInfo.processName;}}return null;}
问题参考以下解答:
Using WebView from more than one process at once with the same data directory is not supported_幻影浪子-CSDN博客
步骤6
写Unity代码,调用Java层中的UpdateAPK(String url, String desc, String packageName)函数实现更新包,Unity中C#代码:
public void UpdateAPK(string url, string desc, string packageName){AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");jo.Call("UpdateAPK", url, desc, packageName);}
步骤7
把aar和UpdateActivity等文件放到Unity中的Plugins/Android对应目录下,出包测试,接入成功
总结:坑还是挺多,得挨个趟坑,后续抽时间整理个Demo传到Github上