45fan.com - 路饭网

搜索: 您的位置主页 > 手机频道 > 手机教程 > 阅读资讯:如何实现安卓应用自动更新的功能?(2)

如何实现安卓应用自动更新的功能?(2)

2015-05-19 08:29:54 来源:www.45fan.com 【

而对应的models代码如下:

class Package(models.Model):

file = models.FileField(u'文件', upload_to=config.PACKAGE_UPLOAD_PATH)

package = models.CharField(u'包名', max_length=255, blank=True, default='')

version = models.IntegerField(u"版本号", blank=True, default=0, null=True)

channel = models.CharField(u"渠道", max_length=128, blank=True, default='')

status = models.IntegerField(u'更新状态', default=config.PACKAGE_STATUS_NOT_UPDATE,

choices=config.PACKAGE_UPDATE_STATUS)

info = models.TextField(u'通知信息', blank=True, null=True)

os = models.CharField(u'操作系统', max_length=64, default=config.PACKAGE_CLIENT_UNKNOW,

choices=config.PACKAGE_CLIENT_OS, blank=True, null=True)

def __unicode__(self):

_,name = os.path.split(self.file.name)

return name

class Meta:

unique_together = ('package', 'version', 'channel', 'os')

def save(self, * args, ** kwargs):

# 文件上传成功后,文件名会加上PACKAGE_UPLOAD_PATH路径

path,_ = os.path.split(self.file.name)

if not path:

if self.file.name.endswith('.apk'):

self.os = config.PACKAGE_CLIENT_ANDROID

path = os.path.join('/tmp', uuid.uuid4().hex + self.file.name)

# logger.error('path: %s', path)

with open(path, 'wb+') as destination:

for chunk in self.file.chunks():

destination.write(chunk)

info = parse_apk_info(path)

os.remove(path)

self.package = info.get('package', '')

self.version = info.get('version', 0)

self.channel = info.get('channel', '')

elif self.file.name.endswith('ipa'):

self.os = config.PACKAGE_CLIENT_IOS

super(self.__class__, self).save(*args, ** kwargs)

def display_filename(self):

_,name = os.path.split(self.file.name)

return name

display_filename.short_description = u"文件"

3. APK文件解析

def parse_apk_info(apk_path, tmp_dir='/tmp'):

"""

获取包名、版本、渠道:

{'version': '17', 'channel': 'CN_MAIN', 'package': ‘com.fff.xxx'}

:param apk_path:

:return:

"""

from bs4 import BeautifulSoup

import os

import shutil

import uuid

abs_apk_path = os.path.abspath(apk_path)

dst_dir = os.path.join(tmp_dir, uuid.uuid4().hex)

jar_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'apktool.jar'))

cmd = 'java -jar %s d %s %s' % (jar_path, abs_apk_path, dst_dir)

if isinstance(cmd, unicode):

cmd = cmd.encode('utf8')

# 执行

os.system(cmd)

manifest_path = os.path.join(dst_dir, 'AndroidManifest.xml')

result = dict()

with open(manifest_path, 'r') as f:

soup = BeautifulSoup(f.read())

result.update(

version=soup.manifest.attrs.get('android:versioncode'),

package=soup.manifest.attrs.get('package'),

)

channel_soup = soup.find('meta-data', attrs={'android:name': 'UMENG_CHANNEL'})

if channel_soup:

result['channel'] = channel_soup.attrs['android:value']

shutil.rmtree(dst_dir)

return result

当然,正如大家所看到的,我们需要依赖于 apktool.jar 这个文件,具体大家可以在网上下载。


本文地址:http://www.45fan.com/sjjc/11149.html
Tags: 安卓 自动更新
编辑:路饭网
相关文章列表
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部