1#!/bin/bash
2#
3# Copyright (C) 2014 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#
17
18# Please change NDK_BUILD to point to the appropriate ndk-build in NDK. It's recommended to
19# use the NDK with maximum backward compatibility, such as the NDK bundle in Android SDK.
20NDK_BUILD="$HOME/Android/android-ndk-r16b/ndk-build"
21
22function generateCopyRightComment() {
23  local year="$1"
24  local isAndroidManifest="$2"
25  local lineComment='#'
26  local copyrightStart=""
27  local copyrightEnd=""
28  local commentStart=""
29  local commentEnd=""
30  if [[ -n "$isAndroidManifest" ]]; then
31    lineComment=""
32    copyrightStart=$'<!--\n'
33    copyrightEnd=$'\n-->'
34    commentStart='<!--'
35    commentEnd='-->'
36  fi
37
38  copyrightInMk=$(
39    cat <<COPYRIGHT_COMMENT
40${copyrightStart}${lineComment} Copyright (C) ${year} The Android Open Source Project
41${lineComment}
42${lineComment} Licensed under the Apache License, Version 2.0 (the "License");
43${lineComment} you may not use this file except in compliance with the License.
44${lineComment} You may obtain a copy of the License at
45${lineComment}
46${lineComment}      http://www.apache.org/licenses/LICENSE-2.0
47${lineComment}
48${lineComment} Unless required by applicable law or agreed to in writing, software
49${lineComment} distributed under the License is distributed on an "AS IS" BASIS,
50${lineComment} WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51${lineComment} See the License for the specific language governing permissions and
52${lineComment} limitations under the License.${copyrightEnd}
53
54${commentStart}${lineComment} Automatically generated file from build_libs.sh.${commentEnd}
55${commentStart}${lineComment} DO NOT MODIFY THIS FILE.${commentEnd}
56
57COPYRIGHT_COMMENT
58  )
59  echo "${copyrightInMk}"
60}
61
62function generateLibsAndroidMk {
63  local targetFile=$1
64  local copyrightInMk=$(generateCopyRightComment "2015")
65  (
66    cat <<LIBS_ANDROID_MK
67${copyrightInMk}
68include \$(call all-subdir-makefiles)
69LIBS_ANDROID_MK
70  ) >"${targetFile}"
71
72}
73
74function generateAndroidManifest {
75  local targetFile="$1"
76  local arch="$2"
77  local splitNamePart="$3"
78  (
79    cat <<ANDROIDMANIFEST
80<?xml version="1.0" encoding="utf-8"?>
81<!-- Automatically generated file from build_libs.sh. -->
82<!-- DO NOT MODIFY THIS FILE. -->
83<manifest xmlns:android="http://schemas.android.com/apk/res/android"
84        package="com.android.cts.splitapp"
85        split="lib${splitNamePart}_${arch}">
86    <application android:hasCode="false" />
87</manifest>
88ANDROIDMANIFEST
89  ) >"${targetFile}"
90
91}
92
93function generateModuleForContentPartialMk {
94  local arch="$1"
95  local packagePartialName="$2"
96  local rawDir="$3"
97  local aaptRevisionFlags="$4"
98
99  localPackage=$(
100    cat <<MODULE_CONTENT_FOR_PARTIAL_MK
101
102include \$(CLEAR_VARS)
103
104LOCAL_PACKAGE_NAME := CtsSplitApp${packagePartialName}_${arch}
105LOCAL_SDK_VERSION := current
106
107LOCAL_JAVA_RESOURCE_DIRS := ${rawDir}
108
109# tag this module as a cts test artifact
110LOCAL_COMPATIBILITY_SUITE := cts general-tests
111
112LOCAL_CERTIFICATE := cts/hostsidetests/appsecurity/certs/cts-testkey1
113LOCAL_AAPT_FLAGS := --version-code 100 --replace-version${aaptRevisionFlags}
114
115include \$(BUILD_CTS_SUPPORT_PACKAGE)
116MODULE_CONTENT_FOR_PARTIAL_MK
117  )
118  echo "${localPackage}"
119}
120
121function generateAndroidMk() {
122  local targetFile="$1"
123  local arch="$2"
124  local copyrightInMk=$(generateCopyRightComment "2014")
125  local baseSplitMkModule=$(generateModuleForContentPartialMk "${arch}" "" "raw" "")
126  local revisionSplitMkModule=$(generateModuleForContentPartialMk "${arch}" "_revision12" \
127      "raw_revision" " --revision-code 12")
128
129  (
130    cat <<LIBS_ARCH_ANDROID_MK
131#
132${copyrightInMk}
133LOCAL_PATH := \$(call my-dir)
134${baseSplitMkModule}
135${revisionSplitMkModule}
136LIBS_ARCH_ANDROID_MK
137  ) >"${targetFile}"
138}
139
140# Go build everything
141rm -rf libs
142cd jni/
143$NDK_BUILD clean
144$NDK_BUILD
145cd ../
146
147for arch in $(ls libs/); do
148  (
149    mkdir -p tmp/$arch/raw/lib/$arch/
150    mv libs/$arch/* tmp/$arch/raw/lib/$arch/
151
152    # The library file name in the new revision apk should have the same file name with base apk.
153    mkdir -p tmp/$arch/raw_revision/lib/$arch/
154    mv tmp/$arch/raw/lib/$arch/libsplitappjni_revision.so \
155      tmp/$arch/raw_revision/lib/$arch/libsplitappjni.so
156
157    archWithoutDash="${arch//[^a-zA-Z0-9_]/_}"
158    generateAndroidManifest "tmp/$arch/AndroidManifest.xml" "${archWithoutDash}" ""
159
160    generateAndroidMk "tmp/$arch/Android.mk" "$arch"
161  )
162done
163
164generateLibsAndroidMk "tmp/Android.mk"
165
166rm -rf libs
167rm -rf obj
168
169mv tmp libs
170