[Android][My Two Cents] Rename R.string.xxx and R.layout.yyy

kokchai
2 min readMay 9, 2020

Look at the obfuscated code below:

public final void m5789a() {
C3100c0 c0Var = new C3100c0();
c0Var.mo18394a(3, true);
c0Var.mo18402b(2);
c0Var.mo18403b(C3130w.m13418a(C3130w.f16352a, R.string.xmlstring_app_name, 0, 2, null));
c0Var.mo18399a(C3130w.m13418a(C3130w.f16352a, R.string.xmlstring_message, 0, 2, null));
c0Var.mo18406c((int) R.drawable.icon_white);
C1093d.m8134b(c0Var, 9052, null, 2, null);
}
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
.... View inflate = getLayoutInflater().inflate(R.layout.layout_text_password, null); ....
}

What’s wrong of the code ?

  • All are the string id are exposed
  • The layout id is exposed

How to hide them ?

This is My Two Cents way, a Python script to rename all of them before compiling:

import os

root = "/Users/xxx/projects/App/" # your app path

map = {
'xmlstring_app_name':'knsgh', # string id
'xmlstring_message' :'labgy'
}

map_layout = {
'layout_text_password':'snhgy' # layout id
}

def update_files(map_of_name, map_of_layout):

file_of_layout = files_id_dir('{}'.format(root))
for file in file_of_layout:
for key in map_of_layout.keys():
if file.endswith('{}.xml'.format(key)):
os.rename(r'{}'.format(file), file[:-len('{}.xml'.format(key))] + map_of_layout[key] + '.xml')

fileList = files_id_dir('{}'.format(root))
for file in fileList:
if file.endswith('.kt') or file.endswith('.java') or file.endswith('.xml'):
# print(file)
try:
f = open(file, 'r')
lines = f.readlines()
f.close();

f = open(file, 'w')
for line in lines:
line1 = line
for key in map_of_name.keys():
line1 = line1.replace(key, map_of_name[key])

for key in map_of_layout.keys():
line1 = line1.replace(key, map_of_layout[key])

f.write(line1)
f.close()
except:
print('{} is failed to process'.format(file));

def files_id_dir(dirName):
list_of_files = os.listdir(dirName)
files = list()
for entry in list_of_files:
fullPath = os.path.join(dirName, entry)
if os.path.isdir(fullPath):
files = files + files_id_dir(fullPath)
else:
files.append(fullPath)

return files

update_files(map, map_layout)

Conclusions:

  • Running this script can rename all of the variable, SO PLEASE MAKE SURE YOU DID BACKUP YOUR SOURCE !!!!
  • Update root, map and map_layout accordingly.
  • Decompile the code to ensure all variables are renamed.
  • Anyways, this is just My Two Cents !!

Used in:

--

--