def copyApk(channel):
    gameName = common.getGameName()
    gameType = common.getGameType()
    # copy2 同时复制文件权限
    dirapk = common.GetProjectOutPutApp() + "/apk"

    if common.AppForPad(False):
        dirapk += "/heng"
        gameName += "_hd"
    else:
        dirapk += "/shu"

    if not os.path.exists(dirapk):
        os.makedirs(dirapk)

    shutil.copy2(
        common.getAndroidProjectApk(),
        dirapk + "/" + gameType + "_" + gameName + "_" + channel + ".apk")
    # sys.setdefaultencoding("utf-8")

    print("脚本名:", sys.argv[0])
    cmdPath = common.cur_file_dir()
    count = len(sys.argv)
    isHD = False

    for i in range(1, count):
        print("参数", i, sys.argv[i])
        if i == 1:
            cmdPath = sys.argv[i]

        if i == 2:
            if sys.argv[i] == "hd":
                isHD = True

    print("cmdPath=" + cmdPath)
    common.SetCmdPath(cmdPath)
    gameName = common.getGameName()
    gameType = common.getGameType()
    print("gameName=" + gameName)
    print("gameType=" + gameType)
    android_studio_dir = common.GetRootDirAndroidStudio()
    print("android_studio_dir=" + android_studio_dir)
    # python 里无法直接执行cd目录,想要用chdir改变当前的工作目录
    os.chdir(android_studio_dir)
    buildApk()
    copyApk("")

    print("apk_build sucess")
def DoCopy():
    gameName = common.getGameName()
    gameType = common.getGameType()
    print(gameName)
    print(gameType)

    resDataName = common.getGameName()#sys.argv[1]
    gameResName = getGameResName()

    gameResCommonRoot = common.GetResourceDataRoot()+"/GameResCommon"+"/"+gameResName
    gameResRoot = common.GetResourceDataRoot()+"/"+gameType+"/"+gameName+"/GameRes"
    flag = os.path.exists(gameResRoot)
    if not flag:
        #目录不存在的话到gamerescommon里copy
        gameResRoot = gameResCommonRoot

    gameDataCommonRoot = common.GetResourceDataRoot()+"/GameDataCommon"
    gameDataRoot = common.GetResourceDataRoot()+"/"+gameType+"/"+gameName+"/GameData"

    streamingAssetsUnity = common.GetRootProjectUnity()+"/Assets/StreamingAssets"
    rootAndroidStudio =common.GetRootDirAndroidStudio()
    rootiOSXcode =common.GetRootDirXcode()

    # copy GameRes 游戏图片等资源
    dirname = "GameRes"

    # unity editor 
    dir1 = gameResRoot
    dir2 = streamingAssetsUnity+"/"+dirname
    flag = os.path.exists(dir2)
    if flag:
        shutil.rmtree(dir2)
    shutil.copytree(dir1,dir2)

    # ios
    # dir2 = rootiOSXcode+"/Data/Raw/"+dirname
    # flag = os.path.exists(dir2)
    # if flag:
    #     shutil.rmtree(dir2)
    # shutil.copytree(dir1,dir2)

   # android asset
    dir2 = rootAndroidStudio+"/src/main/assets/"+dirname
    flag = os.path.exists(dir2)
    if flag:
        shutil.rmtree(dir2)
    shutil.copytree(dir1,dir2)


 # copy GameData 游戏配置等数据 
    dirname = "GameData"

    # unity editor 
    dir1 = gameDataRoot
    dir2 = streamingAssetsUnity+"/"+dirname
    flag = os.path.exists(dir2)
    if flag:
        shutil.rmtree(dir2)
    shutil.copytree(dir1,dir2)

    # ios
    # dir2 = rootiOSXcode+"/Data/Raw/"+dirname
    # flag = os.path.exists(dir2)
    # if flag:
    #     shutil.rmtree(dir2)
    # shutil.copytree(dir1,dir2)

   # android asset
    dir2 = rootAndroidStudio+"/src/main/assets/"+dirname
    flag = os.path.exists(dir2)
    if flag:
        shutil.rmtree(dir2)
    shutil.copytree(dir1,dir2)


    dirname = "GameData/common"

    # unity editor 
    dir1 = gameDataCommonRoot
    dir2 = streamingAssetsUnity+"/"+dirname
    flag = os.path.exists(dir2)
    if flag:
        shutil.rmtree(dir2)
    shutil.copytree(dir1,dir2)

    # ios
    # dir2 = rootiOSXcode+"/Data/Raw/"+dirname
    # flag = os.path.exists(dir2)
    # if flag:
    #     shutil.rmtree(dir2)
    # shutil.copytree(dir1,dir2)

   # android asset
    dir2 = rootAndroidStudio+"/src/main/assets/"+dirname
    flag = os.path.exists(dir2)
    if flag:
        shutil.rmtree(dir2)
    shutil.copytree(dir1,dir2)

    CopyConfigDataToAndroid()
def getGameResName():
    name = common.getGameName()
    idx = name.rfind('_')
    s_len=len(name)
    game = name[idx+1:s_len]
    return game
def updateName(isHd, isAuto):
    appinfoOld = AppInfoOld(isHd)
    if appinfoOld.IsOldVersion():
        # 转换
        ConvertOld2New(isHd, appinfoOld)

    rootConfig = common.GetProjectConfigApp()
    strHD = "HD"

    project_ios = rootConfig + "/ios/project"
    project_android = rootConfig + "/android/project"

    if isHd:
        project_ios = rootConfig + "/ios/project_hd"
        project_android = rootConfig + "/android/project_hd"

    # android
    file_name_cn_android = project_android + "/res/values/strings.xml"
    file_name_en_android = project_android + "/res/values-en/strings.xml"
    file_AndroidManifest = project_android + "/xml/AndroidManifest.xml"
    file_AndroidManifest_GP = project_android + "/xml_gp/AndroidManifest.xml"

    file_google_service_android = project_android + "/config/google-services.json"

    # ios
    file_name_cn_ios = project_ios + "/appname/zh-Hans.lproj/InfoPlist.strings"
    file_name_en_ios = project_ios + "/appname/en.lproj/InfoPlist.strings"
    file_info_plist_ios = project_ios + "/Info.plist"

    # loadJson
    data = loadJson(isHd)

    isOld = IsOldVersion(data)
    global versionCode

    if not isOld:
        appname = data["appname"]

    if isOld:
        APP_NAME_CN_ANDROID = data["APP_NAME_CN_ANDROID"]
        APP_NAME_EN_ANDROID = data["APP_NAME_EN_ANDROID"]
        APP_NAME_CN_IOS = data["APP_NAME_CN_IOS"]
        APP_NAME_EN_IOS = data["APP_NAME_EN_IOS"]
        PACKAGE_ANDROID = data["PACKAGE_ANDROID"]
        PACKAGE_IOS = data["PACKAGE_IOS"]
        versionCode = data["APPVERSION_CODE_ANDROID"]
        APPVERSION_IOS = data["APPVERSION_IOS"]
        appid_huawei = GetConfigDataAppId(source.ANDROID, source.HUAWEI, isHd)
    else:
        APP_NAME_CN_ANDROID = appname[source.ANDROID]["cn"]
        APP_NAME_EN_ANDROID = appname[source.ANDROID]["en"]
        APP_NAME_CN_IOS = appname[source.IOS]["cn"]
        APP_NAME_EN_IOS = appname[source.IOS]["en"]
        PACKAGE_ANDROID = data["apppackage"][source.ANDROID]["default"]
        PACKAGE_IOS = data["apppackage"][source.IOS]["default"]
        versionCode = data["appversion"][source.ANDROID]["code"]
        APPVERSION_IOS = data["appversion"][source.IOS]["value"]

        #appid
        appid_ios = GetJsonAppId(data, source.APPSTORE)
        appid_taptap = GetJsonAppId(data, source.TAPTAP)
        appid_huawei = GetJsonAppId(data, source.HUAWEI)
        SetConfigDataAppId(source.IOS, source.APPSTORE, appid_ios, isHd)
        SetConfigDataAppId(source.ANDROID, source.TAPTAP, appid_taptap, isHd)
        SetConfigDataAppId(source.ANDROID, source.HUAWEI, appid_huawei, isHd)
        UpdateLanguageName(APP_NAME_CN_ANDROID, APP_NAME_EN_ANDROID, isHd)

    # if data.has_key("PACKAGE_HD_ANDROID"):
    #     PACKAGE_HD_ANDROID = data["PACKAGE_HD_ANDROID"]

    if isAuto == True:
        autoPlusVersion(isHd, data)
        # 重新加载
        data = loadJson(isHd)

    APPVERSION_ANDROID = versionCodeToVersion()
    APPVERSION_CODE_ANDROID = versionCode

    # appversion.json
    if isAuto == False:
        src = common.GetProjectConfigDefault() + "/appinfo/appversion.json"
        dst = common.GetProjectConfigApp() + "/appinfo/appversion.json"
        flag = os.path.exists(dst)
        #
        if not isHd:
            shutil.copyfile(src, dst)

        strfile = common.GetFileString(dst)
        key = "_VERSION_ANDROID_"
        if isHd:
            key = "_VERSION_HD_ANDROID_"

        version_web = mainAppVersionHuawei.ParseVersion(appid_huawei)
        strfile = strfile.replace(key, version_web)
        common.saveString2File(strfile, dst)

        filepath = common.GetProjectConfigAppType() + "/appversion.json"
        flag = os.path.exists(filepath)
        strFileJson = "{}"
        if flag:
            strFileJson = common.GetFileString(filepath)
        dataRoot = json.loads(strFileJson)
        dataRoot[common.getGameName()] = json.loads(strfile)
        common.SaveJson(filepath, dataRoot)

    print(APP_NAME_CN_ANDROID)
    print(APP_NAME_EN_ANDROID)
    print(APP_NAME_CN_IOS)
    print(APP_NAME_EN_IOS)
    print(PACKAGE_ANDROID)

    print("android version:" + APPVERSION_ANDROID)
    print("ios version:" + APPVERSION_IOS)

    # android
    # name
    strStart = "app_name\">"
    strEnd = "<"
    # cn
    strOut = replaceStringOfFile(file_name_cn_android, strStart, strEnd,
                                 APP_NAME_CN_ANDROID)
    saveString2File(strOut, file_name_cn_android)
    # en
    strOut = replaceStringOfFile(file_name_en_android, strStart, strEnd,
                                 APP_NAME_EN_ANDROID)
    saveString2File(strOut, file_name_en_android)

    updateAndroidManifest(file_AndroidManifest, PACKAGE_ANDROID,
                          APPVERSION_ANDROID, APPVERSION_CODE_ANDROID, isHd)
    updateAndroidManifest(file_AndroidManifest_GP, PACKAGE_ANDROID,
                          APPVERSION_ANDROID, APPVERSION_CODE_ANDROID, isHd)

    # admob
    replaceGoogleServiceFile(file_google_service_android, PACKAGE_ANDROID)

    # ios

    #appname
    replaceFile(file_info_plist_ios, "_APP_NAME_", APP_NAME_CN_IOS)
    file_name_cn_ios = project_ios + "/appname/zh-Hans.lproj/InfoPlist.strings"
    file_name_en_ios = project_ios + "/appname/en.lproj/InfoPlist.strings"
    # cn
    replaceFile(file_name_cn_ios, "_APP_NAME_", APP_NAME_CN_IOS)
    # en
    replaceFile(file_name_en_ios, "_APP_NAME_", APP_NAME_EN_IOS)

    # package
    replaceFile(file_info_plist_ios, "_APP_PACKAGE_", PACKAGE_IOS)

    # version
    replaceFile(file_info_plist_ios, "_APP_VERSION_", APPVERSION_IOS)

    #admob appid
    appid = adconfig.GetCommonAppId(source.ADMOB, source.IOS, isHd)
    replaceFile(file_info_plist_ios, "_APP_ID_ADMOB_", appid)

    # CFBundleURLSchemes
    src = source.WEIBO
    appid = config.GetShareAppId(src, source.IOS, isHd)
    replaceXcodeUrlScheme(file_info_plist_ios, src, appid, 0)

    src = source.WEIXIN
    appid = config.GetShareAppId(src, source.IOS, isHd)
    replaceXcodeUrlScheme(file_info_plist_ios, src, appid, 0)

    src = source.QQ
    appid = config.GetShareAppId(src, source.IOS, isHd)
    replaceXcodeUrlScheme(file_info_plist_ios, src, appid, 0)
    replaceXcodeUrlScheme(file_info_plist_ios, src, appid, 1)

    # xiaomi aso keyword
    updateXiaoASOkeyword(data, isHd)

    # win
    updateNameWin(isHd, isAuto)
if __name__ == "__main__":

    # 设置为utf8编码
    # reload(sys)
    # sys.setdefaultencoding("utf-8")

    #入口参数:http://blog.csdn.net/intel80586/article/details/8545572
    cmdPath = common.cur_file_dir()
    count = len(sys.argv)
    for i in range(1, count):
        print("参数", i, sys.argv[i])
        if i == 1:
            cmdPath = sys.argv[i]

    common.SetCmdPath(cmdPath)
    gameName = common.getGameName()
    gameType = common.getGameType()
    print(gameName)
    print(gameType)
    # rootDir ="/Users/jaykie/sourcecode/cocos2dx/product/game/ertong"
    rootAndroidStudio = common.GetRootDirAndroidStudio()
    rootiOSXcode = common.GetRootDirXcode()
    xcodeProject = rootiOSXcode + "/Unity-iPhone.xcodeproj/project.pbxproj"
    resDataName = common.getGameName()  #sys.argv[1]
    iconDirName = sys.argv[2]
    # adDirName = sys.argv[3]

    project_ios = "ios/project"
    project_android = "android/project"

    isHD = False