1#!/bin/bash
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17set -e
18
19PATH_TO_FRAMEWORK_RES=${ANDROID_BUILD_TOP}/prebuilts/sdk/current/public/android.jar
20TEST_CERT_PK8=${ANDROID_BUILD_TOP}/cts/hostsidetests/appsecurity/certs/cts-testkey1.pk8
21TEST_CERT_PEM=${ANDROID_BUILD_TOP}/cts/hostsidetests/appsecurity/certs/cts-testkey1.x509.pem
22
23aapt2 compile --dir res -o compiled.flata
24
25# Basic Q apk
26aapt2 link \
27    -I $PATH_TO_FRAMEWORK_RES \
28    --manifest AndroidManifest.xml \
29    --target-sdk-version 29 \
30    -o basic_q.apk \
31    compiled.flata
32
33# Basic R apk
34aapt2 link \
35    -I $PATH_TO_FRAMEWORK_RES \
36    --manifest AndroidManifest.xml \
37    --target-sdk-version 30 \
38    -o basic_r.apk \
39    compiled.flata
40
41# The resources.arc is the same for both versions of the basic apk
42unzip basic_q.apk resources.arsc
43
44# Generate an apk with a compressed resources.arsc targeting Q apk
45rm -f compressed_Q.apk
46cp basic_q.apk compressed_Q.apk
47zip -j compressed_Q.apk resources.arsc
48
49# Generate an apk with a compressed resources.arsc targeting R apk
50rm -f compressed_R.apk
51cp basic_r.apk compressed_R.apk
52zip -j compressed_R.apk resources.arsc
53
54# Create a 3 byte file that is used to ensure the resources.arsc will not be aligned on a 4-byte
55# boundary. The length of the file name (2) + the length of the file (3) should be odd and the file
56# should be stored in tha APK without any extra filed data to ensure the resources.arsc will be on
57# an odd byte boundary.
58dd if=/dev/zero of=aa bs=3 count=1
59
60# Generate an apk with an unaligned resources.arsc targeting Q
61rm -f unaligned_Q.apk
62cp basic_q.apk unaligned_Q.apk
63zip unaligned_Q.apk -d resources.arsc
64zip unaligned_Q.apk -0 -X aa
65zip unaligned_Q.apk -0 -X resources.arsc
66
67# Generate an apk with an unaligned resources.arsc targeting R
68rm -f unaligned_R.apk
69cp basic_r.apk unaligned_R.apk
70zip unaligned_R.apk -d resources.arsc
71zip unaligned_R.apk -0 -X aa
72zip unaligned_R.apk -0 -X resources.arsc
73
74# Sign all of the APKs
75apksigner sign --key $TEST_CERT_PK8 --cert $TEST_CERT_PEM  compressed_Q.apk
76apksigner sign --key $TEST_CERT_PK8 --cert $TEST_CERT_PEM  compressed_R.apk
77apksigner sign --key $TEST_CERT_PK8 --cert $TEST_CERT_PEM  unaligned_Q.apk
78apksigner sign --key $TEST_CERT_PK8 --cert $TEST_CERT_PEM  unaligned_R.apk
79
80# Make sure everything is tidy
81rm aa
82rm resources.arsc
83rm basic_q.apk
84rm basic_r.apk
85rm compiled.flata