File size: 2,361 Bytes
cce8120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from __future__ import annotations

import os
import subprocess
from pathlib import Path


ANDROID_HOME = os.environ.get("ANDROID_HOME", "")
BUILD_TOOLS = os.environ.get("ANDROID_BUILD_TOOLS", "35.0.0")
JAVA_HOME = os.environ.get("JAVA_HOME", "")


class APKWorkflow:

    def __init__(self, project: str):
        self.project = Path(project).resolve()

    def run(self):
        return {
            "validate": self.validate(),
            "compile_resources": self.compile_resources(),
            "compile_java": self.compile_java(),
            "dex": self.dex(),
            "package": self.package(),
            "zipalign": self.zipalign(),
            "sign": self.sign()
        }

    def validate(self):
        manifest = self.project / "AndroidManifest.xml"

        if not manifest.exists():
            return {
                "ok": False,
                "error": "AndroidManifest.xml missing"
            }

        return {"ok": True}

    def compile_resources(self):

        aapt2 = Path(ANDROID_HOME) / "build-tools" / BUILD_TOOLS / "aapt2"

        if not aapt2.exists():
            return {
                "ok": False,
                "error": "aapt2 missing"
            }

        return {"ok": True}

    def compile_java(self):

        javac = "javac"

        try:
            subprocess.run(
                [javac, "-version"],
                check=True,
                capture_output=True
            )
            return {"ok": True}
        except Exception as e:
            return {
                "ok": False,
                "error": str(e)
            }

    def dex(self):

        d8 = Path(ANDROID_HOME) / "build-tools" / BUILD_TOOLS / "d8"

        return {
            "ok": d8.exists()
        }

    def package(self):

        aapt2 = Path(ANDROID_HOME) / "build-tools" / BUILD_TOOLS / "aapt2"

        return {
            "ok": aapt2.exists()
        }

    def zipalign(self):

        exe = Path(ANDROID_HOME) / "build-tools" / BUILD_TOOLS / "zipalign"

        return {
            "ok": exe.exists()
        }

    def sign(self):

        exe = Path(ANDROID_HOME) / "build-tools" / BUILD_TOOLS / "apksigner"

        return {
            "ok": exe.exists()
        }


if __name__ == "__main__":

    import json

    wf = APKWorkflow("android_project")

    print(json.dumps(wf.run(), indent=2))