aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/gen_test_apk_gradle_script.py
blob: 16f59531a2d8759318ec621cc4b38eb33c57810d (plain) (blame)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import argparse
import os
import tarfile
import xml.etree.ElementTree as etree

FLAT_DIRS_REPO_TEMPLATE='flatDir {{ dirs {dirs} }}\n'
MAVEN_REPO_TEMPLATE='maven {{ url "{repo}" }}\n'
KEYSTORE_TEMLATE='signingConfigs {{ debug {{ storeFile file("{keystore}") }} }}\n'

TEST_APK_TEMPLATE = """\
ext.jniLibsDirs = [
    {jni_libs_dirs}
]
ext.resDirs = [
    {res_dirs}
]
ext.javaDirs = [
    {java_dirs}
]
ext.bundles = [
    {bundles}
]

buildscript {{
//    repositories {{
//        jcenter()
//    }}

    repositories {{
        {maven_repos}
    }}

    dependencies {{
        classpath 'com.android.tools.build:gradle:3.5.3'
    }}
}}

apply plugin: 'com.android.application'

repositories {{
//     maven {{
//         url "http://maven.google.com/"
//     }}
//    maven {{
//        url "http://artifactory.yandex.net/artifactory/public/"
//    }}
//    flatDir {{
//        dirs System.env.PKG_ROOT + '/bundle'
//    }}

    {flat_dirs_repo}

    {maven_repos}
}}

dependencies {{
    for (bundle in bundles) {{
        compile("$bundle")
    }}
}}

android {{
    {keystore}

    compileSdkVersion 30
    buildToolsVersion "30.0.3"


    defaultConfig {{
        minSdkVersion 18
        targetSdkVersion 30
        applicationId "{app_id}"
    }}

    sourceSets {{
        main {{
            manifest.srcFile 'Manifest.xml'
            jniLibs.srcDirs = jniLibsDirs
            res.srcDirs = resDirs
            java.srcDirs = javaDirs
        }}
    }}

    applicationVariants.all {{ variant ->
        variant.outputs.each {{ output ->
            def fileName = "$projectDir/output/{app_id}.apk"
            output.outputFileName = new File(output.outputFile.parent, fileName).getName()
        }}
    }}

    dependencies {{
        implementation 'com.google.android.gms:play-services-location:16.0.0'
        implementation 'com.google.android.gms:play-services-gcm:16.0.0'
        implementation 'com.evernote:android-job:1.2.6'
        implementation 'androidx.annotation:annotation:1.1.0'
        implementation 'androidx.core:core:1.1.0'
    }}
}}
"""


def create_native_properties(output_dir, library_name):
    native_properties_file = os.path.join(output_dir, 'native_library_name.xml')
    resources = etree.Element('resources')
    name = etree.SubElement(resources, 'item', dict(name='native_library_name', type='string'))
    name.text = library_name
    etree.ElementTree(resources).write(native_properties_file, xml_declaration=True, encoding='utf-8')


def gen_build_script(args):
    def wrap(items):
        return ',\n    '.join('"{}"'.format(x) for x in items)

    bundles = []
    bundles_dirs = set(args.flat_repos)
    for bundle in args.bundles:
        dir_name, base_name = os.path.split(bundle)
        assert(len(dir_name) > 0 and len(base_name) > 0)
        name, ext = os.path.splitext(base_name)
        assert(len(name) > 0 and ext == '.aar')
        bundles_dirs.add(dir_name)
        bundles.append('com.yandex:{}@aar'.format(name))

    if len(bundles_dirs) > 0:
        flat_dirs_repo = FLAT_DIRS_REPO_TEMPLATE.format(dirs=wrap(bundles_dirs))
    else:
        flat_dirs_repo = ''

    maven_repos = ''.join(MAVEN_REPO_TEMPLATE.format(repo=repo) for repo in args.maven_repos)

    if args.keystore:
        keystore = KEYSTORE_TEMLATE.format(keystore=args.keystore)
    else:
        keystore = ''

    return TEST_APK_TEMPLATE.format(
        app_id=args.app_id,
        jni_libs_dirs=wrap(args.jni_libs_dirs),
        res_dirs=wrap(args.res_dirs),
        java_dirs=wrap(args.java_dirs),
        maven_repos=maven_repos,
        bundles=wrap(bundles),
        flat_dirs_repo=flat_dirs_repo,
        keystore=keystore,
    )


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--aars', nargs='*', default=[])
    parser.add_argument('--app-id', required=True)
    parser.add_argument('--assets-dirs', nargs='*', default=[])
    parser.add_argument('--bundles', nargs='*', default=[])
    parser.add_argument('--bundle-name', nargs='?', default=None)
    parser.add_argument('--java-dirs', nargs='*', default=[])
    parser.add_argument('--jni-libs-dirs', nargs='*', default=[])
    parser.add_argument('--library-name', required=True)
    parser.add_argument('--manifest', required=True)
    parser.add_argument('--flat-repos', nargs='*', default=[])
    parser.add_argument('--maven-repos', nargs='*', default=[])
    parser.add_argument('--output-dir', required=True)
    parser.add_argument('--peers', nargs='*', default=[])
    parser.add_argument('--keystore', default=None)
    parser.add_argument('--res-dirs', nargs='*', default=[])
    args = parser.parse_args()

    for index, jsrc in enumerate(filter(lambda x: x.endswith('.jsrc'), args.peers)):
        jsrc_dir = os.path.join(args.output_dir, 'jsrc_{}'.format(str(index)))
        os.makedirs(jsrc_dir)
        with tarfile.open(jsrc, 'r') as tar:
            tar.extractall(path=jsrc_dir)
            args.java_dirs.append(jsrc_dir)

    args.build_gradle = os.path.join(args.output_dir, 'build.gradle')
    args.settings_gradle = os.path.join(args.output_dir, 'settings.gradle')
    args.gradle_properties = os.path.join(args.output_dir, 'gradle.properties')

    content = gen_build_script(args)
    with open(args.build_gradle, 'w') as f:
        f.write(content)

    with open(args.gradle_properties, 'w') as f:
        f.write('''android.enableJetifier=true
        android.useAndroidX=true
        org.gradle.jvmargs=-Xmx8192m -XX:MaxPermSize=512m''')

    if args.bundle_name:
        with open(args.settings_gradle, 'w') as f:
            f.write('rootProject.name = "{}"'.format(args.bundle_name))

    values_dir = os.path.join(args.output_dir, 'res', 'values')
    os.makedirs(values_dir)
    create_native_properties(values_dir, args.library_name)