webapp热部署二三事

webapp热部署二三事

因为嫌使用APK升级太麻烦,所以想通过热部署的方式来更新应用内部的html等静态资源

一、webview与"content://"

首先,要通过热部署的方式来更新本地web资源的话,这些资源就不能放在assets文件夹中,亦即不能使用webView.load("file:///android_asset/index.html")来访问了。

那么,据我所知还有两种方式可以达到目的:

  1. 将资源放在SD卡中,使用webView.load("file:///mnt/sdcard/index.html")的方式,但是这种方式资源文件任何程序都能访问并修改,非常不安全,所以不采用;
  2. 将资源放在app的私有文件文件夹中(类似/data/user/0/com.your.app/files),然后使用ContentProvider来定义相关的访问协议来访问,像这样webView.load("content://自定义/index.html")

使用第二种方式需要实现ContentProvider抽象类,并重写ParcelFileDescriptor方法:
`

public static String base= "";
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    if("".equals(base)){
        throw new FileNotFoundException("base not init");
    }
    File target=new File(base,uri.getEncodedPath());
    return ParcelFileDescriptor.open(target, ParcelFileDescriptor.MODE_READ_ONLY);
}

`

其中,base需要这样赋值LocalHTMLProvider.base=getFilesDir().getAbsolutePath(),写得有点粗暴...(逃

并且,需要在AndroidManifest.xml中加入<provider android:authorities="自定义" android:name="your.ContentProviderImpl"/>

下载服务器资源至本地

可以通过getFilesDir()或者getDir()等方法获取本地路径,然后下载服务器资源,即可...


事实上,我在这里遇到的了麻烦。

开始的时候,我想借助SVNKit来直接从SVN服务器上克隆,但因为SVNKit的jar包过大(超过了64K方法数限制)而作罢。不过我觉得这也是一种思路,以后可以想办法减小jar包的大小来说试试,也不知道有没有其他的坑...

另一种思路是,将服务器上的资源打包,直接通过相关工具下载资源包,然后在本地解压。当然,版本控制需要自己来做_

posted @ 2016-12-16 15:43  天河流星  阅读(806)  评论(0编辑  收藏  举报