Flutter Engine
The Flutter Engine
native_activity_apk.py
Go to the documentation of this file.
1# Copyright 2013 The Flutter Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import argparse
6import os
7import subprocess
8import sys
9import zipfile
10
11
12def run_command_checked(command, env=None):
13 try:
14 env = env if env is not None else os.environ
15 subprocess.check_output(command, stderr=subprocess.STDOUT, text=True, env=env)
16 except subprocess.CalledProcessError as cpe:
17 print(cpe.output)
18 raise cpe
19
20
21def is_mac():
22 return sys.platform == 'darwin'
23
24
26 script_path = os.path.dirname(os.path.realpath(__file__))
27 if is_mac():
28 return os.path.join(
29 script_path, '..', '..', '..', '..', 'third_party', 'java', 'openjdk', 'Contents', 'Home'
30 )
31 return os.path.join(script_path, '..', '..', 'third_party', 'java', 'openjdk')
32
33
35 return os.path.join(java_home(), 'bin')
36
37
38def main():
39 parser = argparse.ArgumentParser()
40
41 parser.add_argument('--aapt2-bin', type=str, required=True, help='The path to the aapt2 binary.')
42 parser.add_argument(
43 '--zipalign-bin', type=str, required=True, help='The path to the zipalign binary.'
44 )
45 parser.add_argument(
46 '--apksigner-bin', type=str, required=True, help='The path to the apksigner binary.'
47 )
48 parser.add_argument(
49 '--android-manifest', type=str, required=True, help='The path to the AndroidManifest.xml.'
50 )
51 parser.add_argument('--android-jar', type=str, required=True, help='The path to android.jar.')
52 parser.add_argument('--output-path', type=str, required=True, help='The path to the output apk.')
53 parser.add_argument(
54 '--library', type=str, required=True, help='The path to the library to put in the apk.'
55 )
56 parser.add_argument(
57 '--keystore', type=str, required=True, help='The path to the debug keystore to sign the apk.'
58 )
59 parser.add_argument(
60 '--gen-dir', type=str, required=True, help='The directory for generated files.'
61 )
62 parser.add_argument(
63 '--android-abi', type=str, required=True, help='The android ABI of the library.'
64 )
65
66 args = parser.parse_args()
67
68 library_file = os.path.basename(args.library)
69 apk_name = os.path.basename(args.output_path)
70
71 unaligned_apk_path = os.path.join(args.gen_dir, '%s.unaligned' % apk_name)
72 unsigned_apk_path = os.path.join(args.gen_dir, '%s.unsigned' % apk_name)
73 apk_path = args.output_path
74
75 java_path = ':'.join([java_bin(), os.environ['PATH']])
76 env = dict(os.environ, PATH=java_path, JAVA_HOME=java_home())
77
78 # Create the skeleton of the APK using aapt2.
79 aapt2_command = [
80 args.aapt2_bin,
81 'link',
82 '-I',
83 args.android_jar,
84 '--manifest',
85 args.android_manifest,
86 '-o',
87 unaligned_apk_path,
88 ]
89 run_command_checked(aapt2_command, env=env)
90
91 # Stuff the library in the APK which is just a regular ZIP file. Libraries are not compressed.
92 with zipfile.ZipFile(unaligned_apk_path, 'a', compression=zipfile.ZIP_STORED) as zipf:
93 zipf.write(args.library, 'lib/%s/%s' % (args.android_abi, library_file))
94
95 # Align the dylib to a page boundary.
96 zipalign_command = [
97 args.zipalign_bin,
98 '-p', # Page align the dylib
99 '-f', # overwrite output if exists
100 '4', # 32-bit alignment
101 unaligned_apk_path,
102 unsigned_apk_path,
103 ]
104 run_command_checked(zipalign_command, env=env)
105
106 # Sign the APK.
107 apksigner_command = [
108 args.apksigner_bin, 'sign', '--ks', args.keystore, '--ks-pass', 'pass:android', '--out',
109 apk_path, unsigned_apk_path
110 ]
111 run_command_checked(apksigner_command, env=env)
112
113 # Remove the intermediates so the out directory isn't full of large files.
114 os.remove(unaligned_apk_path)
115 os.remove(unsigned_apk_path)
116
117 return 0
118
119
120if __name__ == '__main__':
121 sys.exit(main())
Definition: main.py:1
def run_command_checked(command, env=None)
def print(*args, **kwargs)
Definition: run_tests.py:49
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741