想想时间过得真快,有些东西放到手里也没啥用,放出来大家一起研究研究挺好。那这篇文章就分享下弹窗app的制作脚本吧。
提前声明下,代码可能有点乱,但应该是可以跑通的。对代码不做过多解释,大家有兴趣的自己研究研究改改,看的懂就看看,看不懂就算了。
附件中是模板文件,可以参考下。
脚本分3段,一段shell两段python
shell脚本
[Bash shell] 纯文本查看 复制代码#!/bin/bash
#$1: 程序的文件夹
#$2: 包名
function begin()
{
smali=$(findSmali $1)
echo $smali
echo '修改程序入口'
interface=`python changeEnter.py $1/AndroidManifest.xml`
inject $1 $smali $interface
sign $1
}
function findSmali()
{
smali=''
if [ ! -d $1/smali_classes3 ];then
if [ ! -d ./$1/smali_classes2 ];then
smali='smali_classes2'
else
smali='smali_classes3'
fi
else
smali='smali_classes4'
fi
if [ ! -d "$1/$smali" ];then
mkdir $1/$smali
fi
echo $smali
# return $smali
}
# $1 注入程序文件夹
# $2 注入程序smali文件位置
# $3 程序入口文件
function inject()
{
echo '开始注入代码'
if [ ! -d "$1/res/drawable" ];then
mkdir $1/res/drawable
fi
if [ ! -d "$1/res/layout" ];then
mkdir $1/res/layout
fi
cp ./template/*.png ./$1/res/drawable
cp ./template/color_cursor.xml ./$1/res/drawable
cp ./template/activity_card.xml ./$1/res/layout
if [ ! -d "$1/$2/com" ];then
mkdir $1/$2/com
fi
if [ ! -f "$1/res/values/ids.xml" ]; then
touch $1/res/values/ids.xml
echo "
" >> $1/res/values/ids.xml
fi
echo "$1/res/values/colors.xml"
if [ ! -f "./$1/res/values/colors.xml" ]; then
touch $1/res/values/colors.xml
echo "
" >> $1/res/values/colors.xml
fi
if [ ! -f "$1/res/values/styles.xml" ]; then
touch $1/res/values/styles.xml
echo "
" >> $1/res/values/styles.xml
fi
cp -r ./template/cutils ./$1/$2/com/
python editxml.py ./$1/res/values/ ./$1/$2/com/cutils/
sed -i "" "s#CMainInterfaceActivity#$3#g" ./$1/$2/com/cutils/FActivity.smali
# sed -i "" "s#CMainInterfaceActivity#$3#g" ./$1/$2/com/cutils/CActivity\$OnShareListener.smali
echo '注入代码成功'
}
#
function sign()
{
echo '开始打包并安装'
apktool b $1
rm -rf $1/build
mv $1/dist/$1.apk $1"_unsign.apk"
rm -rf $1/dist
jarsigner -verbose -digestalg SHA1 -sigalg MD5withRSA -keystore ./DaoKey.jks -storepass 密码1 -keypass 密码2 -signedjar $1.apk $1"_unsign.apk" Dao
}
apktool d $1.apk
begin $1
sign $1
rm -rf $2
rm -f $2"_unsign.apk"
rm -f $1$2.apk
mv $2".apk" $1
# sed -i "" "s#My/XuanAo/BaZiYi/#com/bby/BaZi/#g" *.smali
第二段 python: 修改apk的入口文件:
[Python] 纯文本查看 复制代码# -*- coding: utf-8 -*-
import sys
import os
import xmltodict
import collections
def change_interface(src):
with open(src,'r+') as file:
xml = file.read()
doc = xmltodict.parse(xml)
old_name = ''
old_note = collections.OrderedDict()
if isinstance(doc['manifest']['application']['activity'],dict):
note = doc['manifest']['application']['activity']
if isinstance(note['intent-filter'],list):
isinterface = is_interface_from_list(note['intent-filter'])
if isinterface:
old_name = note['@android:name']
else:
isinterface = is_interface_from_dict(note['intent-filter'])
if isinterface:
old_name = note['@android:name']
else:
for note in doc['manifest']['application']['activity']:
if note.has_key('intent-filter'):
if isinstance(note['intent-filter'],list):
isinterface = is_interface_from_list(note['intent-filter'])
if isinterface:
old_name = note['@android:name']
elif isinstance(note['intent-filter'],dict):
isinterface = is_interface_from_dict(note['intent-filter'])
if isinterface:
old_name = note['@android:name']
activity = collections.OrderedDict()
category = collections.OrderedDict()
action = collections.OrderedDict()
intent = collections.OrderedDict()
activity['@android:name'] = 'com.cutils.CActivity'
activity['@android:theme'] = '@style/CShareAppTheme'
category['@android:name'] = 'android.intent.category.LAUNCHER'
action['@android:name'] = 'android.intent.action.MAIN'
intent['action'] = action
intent['category'] = category
activity['intent-filter'] = intent
if isinstance(doc['manifest']['application']['activity'],list):
doc['manifest']['application']['activity'].insert(0,activity)
else:
signal = doc['manifest']['application']['activity']
activityList = [signal,activity];
doc['manifest']['application']['activity'] = activityList
# doc['manifest']['application']['activity'].insert(0,activity)
with open('./manifest_tmp.xml','w') as f:
f.writelines(xmltodict.unparse(doc))
return old_name
def is_interface_from_list(notes):
for note in notes:
if note.has_key('category') and isinstance(note['category'],list):
category = note['category']
for item in category:
if item['@android:name'].find('category.LAUNCHER') > 0:
category.remove(item)
return True
elif note.has_key('category') and isinstance(note['category'],dict):
if note['category']['@android:name'].find('category.LAUNCHER') > 0:
del note['category']
return True
return False
def is_interface_from_dict(note):
if note.has_key('category') and isinstance(note['category'],list):
category = note['category']
for item in category:
if item['@android:name'].find('category.LAUNCHER') > 0:
category.remove(item)
return True
elif note.has_key('category') and isinstance(note['category'],dict):
if note['category']['@android:name'].find('category.LAUNCHER') > 0:
del note['category']
return True
else:
return False
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf-8')
src = sys.argv[1]
old_interface = change_interface(src)
cmd = 'xmllint --format ./manifest_tmp.xml' + ' > ' +src
# cmd = 'xmllint --format ./manifest_tmp.xml > test.xml'
os.system(cmd)
print(old_interface.replace('.','\/'))
第三段 修改apk资源包中的一些属性文件
[Python] 纯文本查看 复制代码# -*- coding: utf-8 -*-
import sys
import os
import xmltodict
def insertitem(src,des):
try:
fsrc = open(src,'r')
linessrc = fsrc.readlines()
fdes = open(des,'r+')
linesdes = fdes.readlines()
for line in linessrc:
linesdes.insert(-1,line)
fdes.seek(0)
fdes.truncate()
fdes.writelines(linesdes)
fsrc.close()
fdes.close()
except Exception as e:
raise e
def insertxml(src,des):
try:
fsrc = open(src,'r')
linessrc = fsrc.readlines()
del linessrc[0]
del linessrc[0]
del linessrc[-1]
fdes = open(des,'r+')
linesdes = fdes.readlines()
for line in linessrc:
linesdes.insert(-1,line)
fdes.seek(0)
fdes.truncate()
fdes.writelines(linesdes)
fsrc.close()
fdes.close()
except Exception as e:
raise e
def getvalues(src):
with open (src,'r') as file:
xml = file.read()
doc = xmltodict.parse(xml)
dic = {'id':0,'color':0,'layout':0,'drawable':0,'style':0}
for note in doc['resources']['public']:
if note['@type'] == 'id' and note['@id'] > dic['id']:
dic['id'] = note['@id']
if note['@type'] == 'color' and note['@id'] > dic['color']:
dic['color'] = note['@id']
if note['@type'] == 'layout' and note['@id'] > dic['layout']:
dic['layout'] = note['@id']
if note['@type'] == 'drawable' and note['@id'] > dic['drawable']:
dic['drawable'] = note['@id']
if note['@type'] == 'style' and note['@id'] > dic['style']:
dic['style'] = note['@id']
return dic
def setvalues(src,base):
with open(src,'r+') as file:
xml = file.read()
doc = xmltodict.parse(xml)
i = 1
if base['id'] == 0:
base['id'] = '0x7f090000'
if base['color'] == 0:
base['color'] = '0x7f060000'
if base['layout'] == 0:
base['layout'] = '0x7f0b0000'
if base['drawable'] == 0:
base['drawable'] = '0x7f080000'
if base['style'] == 0:
base['style'] = '0x7f100000'
for note in doc['resources']['public']:
if note['@type'] == 'id':
note['@id'] = hex(int(base['id'],16) + i)
if note['@type'] == 'color':
note['@id']= hex(int(base['color'],16) + i)
if note['@type'] == 'layout':
note['@id']= hex(int(base['layout'],16) + i)
if note['@type'] == 'drawable':
note['@id']= hex(int(base['drawable'],16) + i)
if note['@type'] == 'style':
note['@id']= hex(int(base['style'],16) + i)
i = i + 1
file.seek(0)
file.truncate()
file.writelines(xmltodict.unparse(doc))
return doc
if __name__ == '__main__':
des = sys.argv[1]
desc = sys.argv[2]
insertitem('./template/ids.xml', des + 'ids.xml')
insertitem('./template/colors.xml', des + 'colors.xml')
insertitem('./template/style.xml', des + 'styles.xml')
base = getvalues(des + 'public.xml')
doc = setvalues('./template//public.xml',base)
os.system('xmllint --format ./template/public.xml > ./tmp.xml')
insertxml('tmp.xml',des + 'public.xml')
for note in doc['resources']['public']:
cmd = "sed -i \"\" \"s/" + note['@name'] + '/' + note['@id'] + '/g\" ' + desc +'CActivity.smali'
os.system(cmd)
os.system('rm -f tmp.xml')
最后:
如果点赞多的话后面把后台的源码也开源出来大家一起玩玩。
最后提醒大家不要干坏事。