|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import errno |
| 5 | +import shutil |
| 6 | +import os.path |
| 7 | +import sys |
| 8 | +from xml.dom.minidom import parse, parseString |
| 9 | + |
| 10 | +def main(): |
| 11 | + parser = argparse.ArgumentParser( |
| 12 | + description="Adjust post build android script") |
| 13 | + parser.add_argument('assets_path', |
| 14 | + help="path to the assets folder of unity3d") |
| 15 | + # pre build must be set manually |
| 16 | + parser.add_argument('--pre-build', action="store_true", |
| 17 | + help="used to check and change the AndroidManifest.xml to conform to the Adjust SDK") |
| 18 | + exit_code = 0 |
| 19 | + with open('AdjustPostBuildAndroidLog.txt','w') as fileLog: |
| 20 | + # log function with file injected |
| 21 | + LogFunc = LogInput(fileLog) |
| 22 | + |
| 23 | + # get the path of the android plugin folder |
| 24 | + android_plugin_path, pre_build = parse_input(LogFunc, parser) |
| 25 | + |
| 26 | + # try to open an existing manifest file |
| 27 | + try: |
| 28 | + manifest_path = os.path.join(android_plugin_path + "AndroidManifest.xml") |
| 29 | + edited_xml = None |
| 30 | + with open(manifest_path,'r+') as mf: |
| 31 | + check_dic = check_manifest(LogFunc, mf) |
| 32 | + # check if manifest has all changes needed |
| 33 | + all_check = check_dic["has_adjust_receiver"] and \ |
| 34 | + check_dic["has_internet_permission"] and \ |
| 35 | + check_dic["has_wifi_permission"] |
| 36 | + # edit manifest if has any change missing |
| 37 | + if not all_check: |
| 38 | + # warn unity if it was pos-build, if something is missing |
| 39 | + if not pre_build: |
| 40 | + LogFunc("Android manifest used in unity did not " + \ |
| 41 | + "had all the changes adjust SDK needs. " + \ |
| 42 | + "Please build again the package.") |
| 43 | + edited_xml = edit_manifest(LogFunc, mf, check_dic) |
| 44 | + # write changed xml |
| 45 | + if edited_xml: |
| 46 | + with open(manifest_path,'w+') as mf: |
| 47 | + edited_xml.writexml(mf) |
| 48 | + exit_code = 1 |
| 49 | + except IOError as ioe: |
| 50 | + # if it does not exist |
| 51 | + if ioe.errno == errno.ENOENT: |
| 52 | + # warn unity that needed manifest wasn't used |
| 53 | + if not pre_build: |
| 54 | + LogFunc("Used default Android manifest file from " + \ |
| 55 | + "unity. Please build again the package to " + |
| 56 | + "include the changes for adjust SDK") |
| 57 | + copy_adjust_manifest(LogFunc, android_plugin_path) |
| 58 | + exit_code = 1 |
| 59 | + else: |
| 60 | + LogFunc(ioe) |
| 61 | + except Exception as e: |
| 62 | + LogFunc(e) |
| 63 | + |
| 64 | + # exit with return code for unity |
| 65 | + sys.exit(exit_code) |
| 66 | + |
| 67 | +def edit_manifest(Log, manifest_file, check_dic): |
| 68 | + manifest_xml = check_dic["manifest_xml"] |
| 69 | + |
| 70 | + # add the adjust install referrer to the application element |
| 71 | + if not check_dic["has_adjust_receiver"]: |
| 72 | + receiver_string = """<?xml version="1.0" ?> |
| 73 | + <receiver |
| 74 | + xmlns:android="http://schemas.android.com/apk/res/android" |
| 75 | + android:name="com.adjust.sdk.ReferrerReceiver" |
| 76 | + android:exported="true" > |
| 77 | + <intent-filter> |
| 78 | + <action android:name="com.android.vending.INSTALL_REFERRER" /> |
| 79 | + </intent-filter> |
| 80 | + </receiver> |
| 81 | + """ |
| 82 | + receiver_xml = parseString(receiver_string) |
| 83 | + receiver_xml.documentElement.removeAttribute("xmlns:android") |
| 84 | + |
| 85 | + for app_element in manifest_xml.getElementsByTagName("application"): |
| 86 | + app_element.appendChild(receiver_xml.documentElement) |
| 87 | + Log("added adjust install referrer receiver") |
| 88 | + |
| 89 | + # add the internet permission to the manifest element |
| 90 | + if not check_dic["has_internet_permission"]: |
| 91 | + ip_element = manifest_xml.createElement("uses-permission") |
| 92 | + ip_element.setAttribute("android:name", "android.permission.INTERNET") |
| 93 | + manifest_xml.documentElement.appendChild(ip_element) |
| 94 | + Log("added internet permission") |
| 95 | + |
| 96 | + # add the access wifi state permission to the manifest element |
| 97 | + if not check_dic["has_wifi_permission"]: |
| 98 | + ip_element = manifest_xml.createElement("uses-permission") |
| 99 | + ip_element.setAttribute("android:name", "android.permission.ACCESS_WIFI_STATE") |
| 100 | + manifest_xml.documentElement.appendChild(ip_element) |
| 101 | + Log("added access wifi permission") |
| 102 | + |
| 103 | + #Log(manifest_xml.toxml()) |
| 104 | + return manifest_xml |
| 105 | + |
| 106 | +def check_manifest(Log, manifest_file): |
| 107 | + |
| 108 | + manifest_xml = parse(manifest_file) |
| 109 | + #Log(manifest_xml.toxml()) |
| 110 | + |
| 111 | + has_adjust_receiver = has_element_attr(manifest_xml, |
| 112 | + "receiver", "android:name", "com.adjust.sdk.ReferrerReceiver") |
| 113 | + Log("has adjust install referrer receiver?: {0}", has_adjust_receiver) |
| 114 | + |
| 115 | + has_internet_permission = has_element_attr(manifest_xml, |
| 116 | + "uses-permission", "android:name", "android.permission.INTERNET") |
| 117 | + Log("has internet permission?: {0}", has_internet_permission) |
| 118 | + |
| 119 | + has_wifi_permission = has_element_attr(manifest_xml, |
| 120 | + "uses-permission", "android:name", "android.permission.ACCESS_WIFI_STATE") |
| 121 | + Log("has wifi permission?: {0}", has_wifi_permission) |
| 122 | + |
| 123 | + return {"manifest_xml" : manifest_xml, |
| 124 | + "has_adjust_receiver" : has_adjust_receiver, |
| 125 | + "has_internet_permission" : has_internet_permission, |
| 126 | + "has_wifi_permission" : has_wifi_permission} |
| 127 | + |
| 128 | +def has_element_attr(xml_dom, tag_name, attr_name, attr_value): |
| 129 | + for node in xml_dom.getElementsByTagName(tag_name): |
| 130 | + attr_dom = node.getAttribute(attr_name) |
| 131 | + if attr_dom == attr_value: |
| 132 | + return True |
| 133 | + return False |
| 134 | + |
| 135 | +def copy_adjust_manifest(Log, android_plugin_path): |
| 136 | + adjust_manifest_path = os.path.join(android_plugin_path, "AdjustAndroidManifest.xml") |
| 137 | + new_manifest_path = os.path.join(android_plugin_path, "AndroidManifest.xml") |
| 138 | + try: |
| 139 | + shutil.copyfile(adjust_manifest_path, new_manifest_path) |
| 140 | + except Exception as e: |
| 141 | + Log(e) |
| 142 | + else: |
| 143 | + Log("Manifest copied from {0} to {1}", |
| 144 | + adjust_manifest_path, new_manifest_path) |
| 145 | + |
| 146 | +def LogInput(writeObject): |
| 147 | + def Log(message, *args): |
| 148 | + messageNLine = str(message) + "\n" |
| 149 | + writeObject.write(messageNLine.format(*args)) |
| 150 | + return Log |
| 151 | + |
| 152 | +def parse_input(Log, parser): |
| 153 | + args = parser.parse_args() |
| 154 | + assets_path = args.assets_path |
| 155 | + |
| 156 | + android_plugin_path = os.path.join(assets_path, "Plugins/Android/") |
| 157 | + Log("Android plugin path: {0}", android_plugin_path) |
| 158 | + |
| 159 | + return android_plugin_path, args.pre_build |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + main() |
0 commit comments