使用python脚本构建apk

时间:2018-03-16 00:54:28

标签: android python-3.x gradlew

我正在尝试使用不同的调试模式创建一个Android应用程序。

DebugMode.java

public class DebugMode {

    // ------------------------------
    // Define
    // ------------------------------
    public static final boolean isStagingServer = true;
    public static final boolean isTestServer = false;
    public static final boolean isIgnoreMaintenance = false;

}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.textView);
        if (DebugMode.isTestServer && !DebugMode.isStagingServer && !DebugMode.isIgnoreMaintenance) {
            textView.setText("Dev Server");
        } else if (!DebugMode.isTestServer && !DebugMode.isStagingServer && !DebugMode.isIgnoreMaintenance) {
            textView.setText("Prod Server");
        } else if (!DebugMode.isTestServer && DebugMode.isStagingServer && !DebugMode.isIgnoreMaintenance) {
            textView.setText("Staging Server");
        }
    }
}

现在因为我有三种不同的调试模式,在android studio中构建并获取那些不同的发布版本需要很长时间。

所以不是使用android studio生成三个版本的apk,我只是创建了一个1版本的apk并使用python脚本更改了DebugMode

build.py

import subprocess
import shutil
import os
import sys
# import os.path

SETTING_FILE_PATH = "../app/src/main/java/com/project/jerrol/samplebuildscript/app/define/DebugMode.java"


def change_setting_file(args=[]):
    result = ""
    develop_val = args[0]

    if develop_val == "develop" or develop_val == "staging":
        print("change type: " + develop_val)
    else:
        print("change type: release")


    with open(SETTING_FILE_PATH, encoding="utf8") as f:
        for line in f.readlines():
            result += change_mode(line, develop_val)


    write_file = open(SETTING_FILE_PATH, "w", encoding="utf8")
    write_file.write(result)
    write_file.close()


def change_mode(line="", build_type="release"):
    new_line = line
    if build_type == "develop":
        if "isTestServer" in line:
            new_line = line.replace("false", "true")
        elif "isStagingServer" in line:
            new_line = line.replace("true", "false")
    elif build_type == "staging":
        if "isTestServer" in line:
            new_line = line.replace("true", "false")
        elif "isStagingServer" in line:
            new_line = line.replace("false", "true")            
    else:
        new_line = line.replace("true", "false")

    return new_line


def build_cmd():
    cmd = "../gradlew.bat clean assembleRelease -Pandroid.injected.signing.store.file=samplebuildscript.jks -Pandroid.injected.signing.store.password=123456 -Pandroid.injected.signing.key.alias=sample-key -Pandroid.injected.signing.key.password=123456"

    subprocess.call([cmd], shell=True)


if __name__ == '__main__':

    change_setting_file(sys.argv[1:])

    build_name = sys.argv[1]
    build_cmd()
    # move
    apk_path = "../app/release/app-release.apk"
    shutil.move(apk_path, "../app-" + build_name +  ".apk")

因此在运行脚本后,结果是

build.py output

The Generated APK

我认为它构建成功,但是当我安装apk时。 它显示错误

  

解析错误:解析包时出现问题。

我忘记了什么吗?或者我不能使用python在DebugMode.java做什么?

希望有人会帮忙,谢谢

0 个答案:

没有答案