그래도 문자열 / app_name을 플레이버별로 어떻게 다르게 만들 수 있습니까?
업데이트를 작성하고 싶었지만 소스를 패치하는 Python 스크립트를 사용한다는 원래 답변보다 크다는 것을 깨달았습니다.
Python 스크립트에는 디렉토리 이름 인 매개 변수가 있습니다. 이 디렉토리에는 유형별 자산, 실행기 아이콘과 같은 리소스, Python 사전이있는 properties.txt 파일이 포함되어 있습니다.
{ 'someBoolean' : True
, 'someParam' : 'none'
, 'appTitle' : '@string/x_app_name_xyz'
}
파이썬 스크립트를로드하는 파일에서 사전을 사이에 값을 대체 <string name="app_name">하고 </string>의 값에 의해 properties['appTitle'].
아래 코드는있는 그대로 /있는 그대로 제공됩니다.
for strings_xml in glob.glob("res/values*/strings.xml"):
fileReplace(strings_xml,'<string name="app_name">',properties['appTitle'],'</string>',oldtextpattern=r"[a-zA-Z0-9_/@\- ]+")
하나 이상의 이러한 파일에서 속성을 읽으려면 :
with open(filename1) as f:
properties = eval(f.read())
with open(filename2) as f:
properties.update(eval(f.read()))
fileReplace 함수는 다음과 같습니다.
really = True
def fileReplace(fname,before,newtext,after,oldtextpattern=r"[\w.]+",mandatory=True):
with open(fname, 'r+') as f:
read_data = f.read()
pattern = r"("+re.escape(before)+r")"+oldtextpattern+"("+re.escape(after)+r")"
replacement = r"\g<1>"+newtext+r"\g<2>"
new_data,replacements_made = re.subn(pattern,replacement,read_data,flags=re.MULTILINE)
if replacements_made and really:
f.seek(0)
f.truncate()
f.write(new_data)
if verbose:
print "patching ",fname," (",replacements_made," occurrence" + ("s" if 1!=replacements_made else ""),")",newtext,("-- no changes" if new_data==read_data else "-- ***CHANGED***")
elif replacements_made:
print fname,":"
print new_data
elif mandatory:
raise Exception("cannot patch the file: "+fname+" with ["+newtext+"] instead of '"+before+"{"+oldtextpattern+"}"+after+"'")
스크립트의 첫 번째 줄은 다음과 같습니다.
#!/usr/bin/python
# coding: utf-8
import sys
import os
import re
import os.path
import shutil
import argparse
import string
import glob
from myutils import copytreeover