|
| 1 | +/* |
| 2 | + * Copyright 2019 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import groovy.xml.MarkupBuilder |
| 18 | + |
| 19 | +plugins { |
| 20 | + id 'com.android.application' |
| 21 | + |
| 22 | +} |
| 23 | + |
| 24 | +def twaManifest = [ |
| 25 | + applicationId: 'com.bryankok.radically.twa', |
| 26 | + hostName: 'radically.bryankok.com', // The domain being opened in the TWA. |
| 27 | + launchUrl: '/search?utm_source=android_twa', // The start path for the TWA. Must be relative to the domain. |
| 28 | + name: 'Radically CJK Search Engine', // The application name. |
| 29 | + launcherName: 'Radically', // The name shown on the Android Launcher. |
| 30 | + themeColor: '#FFFFFF', // The color used for the status bar. |
| 31 | + navigationColor: '#000000', // The color used for the navigation bar. |
| 32 | + navigationColorDark: '#000000', // The color used for the dark navbar. |
| 33 | + navigationDividerColor: '#000000', // The navbar divider color. |
| 34 | + navigationDividerColorDark: '#000000', // The dark navbar divider color. |
| 35 | + backgroundColor: '#016154', // The color used for the splash screen background. |
| 36 | + enableNotifications: true, // Set to true to enable notification delegation. |
| 37 | + // Every shortcut must include the following fields: |
| 38 | + // - name: String that will show up in the shortcut. |
| 39 | + // - short_name: Shorter string used if |name| is too long. |
| 40 | + // - url: Absolute path of the URL to launch the app with (e.g '/create'). |
| 41 | + // - icon: Name of the resource in the drawable folder to use as an icon. |
| 42 | + shortcuts: [], |
| 43 | + // The duration of fade out animation in milliseconds to be played when removing splash screen. |
| 44 | + splashScreenFadeOutDuration: 300, |
| 45 | + generatorApp: 'bubblewrap-cli', // Application that generated the Android Project |
| 46 | + // The fallback strategy for when Trusted Web Activity is not avilable. Possible values are |
| 47 | + // 'customtabs' and 'webview'. |
| 48 | + fallbackType: 'customtabs', |
| 49 | + enableSiteSettingsShortcut: 'true', |
| 50 | + orientation: 'default', |
| 51 | +] |
| 52 | + |
| 53 | +android { |
| 54 | + compileSdkVersion 30 |
| 55 | + defaultConfig { |
| 56 | + applicationId "com.bryankok.radically.twa" |
| 57 | + minSdkVersion 19 |
| 58 | + targetSdkVersion 30 |
| 59 | + versionCode 2 |
| 60 | + versionName "2" |
| 61 | + |
| 62 | + // The name for the application |
| 63 | + resValue "string", "appName", twaManifest.name |
| 64 | + |
| 65 | + // The name for the application on the Android Launcher |
| 66 | + resValue "string", "launcherName", twaManifest.launcherName |
| 67 | + |
| 68 | + // The URL that will be used when launching the TWA from the Android Launcher |
| 69 | + def launchUrl = "https://" + twaManifest.hostName + twaManifest.launchUrl |
| 70 | + resValue "string", "launchUrl", launchUrl |
| 71 | + |
| 72 | + |
| 73 | + // The URL the Web Manifest for the Progressive Web App that the TWA points to. This |
| 74 | + // is used by Chrome OS to open the Web version of the PWA instead of the TWA, as it |
| 75 | + // will probably give a better user experience for non-mobile devices. |
| 76 | + resValue "string", "webManifestUrl", 'https://radically.bryankok.com/manifest.json' |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + // The hostname is used when building the intent-filter, so the TWA is able to |
| 82 | + // handle Intents to open https://svgomg.firebaseapp.com. |
| 83 | + resValue "string", "hostName", twaManifest.hostName |
| 84 | + |
| 85 | + // This attribute sets the status bar color for the TWA. It can be either set here or in |
| 86 | + // `res/values/colors.xml`. Setting in both places is an error and the app will not |
| 87 | + // compile. If not set, the status bar color defaults to #FFFFFF - white. |
| 88 | + resValue "color", "colorPrimary", twaManifest.themeColor |
| 89 | + |
| 90 | + // This attribute sets the navigation bar color for the TWA. It can be either set here or |
| 91 | + // in `res/values/colors.xml`. Setting in both places is an error and the app will not |
| 92 | + // compile. If not set, the navigation bar color defaults to #FFFFFF - white. |
| 93 | + resValue "color", "navigationColor", twaManifest.navigationColor |
| 94 | + |
| 95 | + // This attribute sets the dark navigation bar color for the TWA. It can be either set here |
| 96 | + // or in `res/values/colors.xml`. Setting in both places is an error and the app will not |
| 97 | + // compile. If not set, the navigation bar color defaults to #000000 - black. |
| 98 | + resValue "color", "navigationColorDark", twaManifest.navigationColorDark |
| 99 | + |
| 100 | + // This attribute sets the navbar divider color for the TWA. It can be either |
| 101 | + // set here or in `res/values/colors.xml`. Setting in both places is an error and the app |
| 102 | + // will not compile. If not set, the divider color defaults to #00000000 - transparent. |
| 103 | + resValue "color", "navigationDividerColor", twaManifest.navigationDividerColor |
| 104 | + |
| 105 | + // This attribute sets the dark navbar divider color for the TWA. It can be either |
| 106 | + // set here or in `res/values/colors.xml`. Setting in both places is an error and the |
| 107 | + //app will not compile. If not set, the divider color defaults to #000000 - black. |
| 108 | + resValue "color", "navigationDividerColorDark", twaManifest.navigationDividerColorDark |
| 109 | + |
| 110 | + // Sets the color for the background used for the splash screen when launching the |
| 111 | + // Trusted Web Activity. |
| 112 | + resValue "color", "backgroundColor", twaManifest.backgroundColor |
| 113 | + |
| 114 | + // Defines a provider authority fot the Splash Screen |
| 115 | + resValue "string", "providerAuthority", twaManifest.applicationId + '.fileprovider' |
| 116 | + |
| 117 | + // The enableNotification resource is used to enable or disable the |
| 118 | + // TrustedWebActivityService, by changing the android:enabled and android:exported |
| 119 | + // attributes |
| 120 | + resValue "bool", "enableNotification", twaManifest.enableNotifications.toString() |
| 121 | + |
| 122 | + twaManifest.shortcuts.eachWithIndex { shortcut, index -> |
| 123 | + resValue "string", "shortcut_name_$index", "$shortcut.name" |
| 124 | + resValue "string", "shortcut_short_name_$index", "$shortcut.short_name" |
| 125 | + } |
| 126 | + |
| 127 | + // The splashScreenFadeOutDuration resource is used to set the duration of fade out animation in milliseconds |
| 128 | + // to be played when removing splash screen. The default is 0 (no animation). |
| 129 | + resValue "integer", "splashScreenFadeOutDuration", twaManifest.splashScreenFadeOutDuration.toString() |
| 130 | + |
| 131 | + resValue "string", "generatorApp", twaManifest.generatorApp |
| 132 | + |
| 133 | + resValue "string", "fallbackType", twaManifest.fallbackType |
| 134 | + |
| 135 | + resValue "bool", "enableSiteSettingsShortcut", twaManifest.enableSiteSettingsShortcut |
| 136 | + resValue "string", "orientation", twaManifest.orientation |
| 137 | + } |
| 138 | + buildTypes { |
| 139 | + release { |
| 140 | + minifyEnabled true |
| 141 | + } |
| 142 | + } |
| 143 | + compileOptions { |
| 144 | + sourceCompatibility JavaVersion.VERSION_1_8 |
| 145 | + targetCompatibility JavaVersion.VERSION_1_8 |
| 146 | + } |
| 147 | + lintOptions { |
| 148 | + checkReleaseBuilds false |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +task generateShorcutsFile { |
| 155 | + assert twaManifest.shortcuts.size() < 5, "You can have at most 4 shortcuts." |
| 156 | + twaManifest.shortcuts.eachWithIndex { s, i -> |
| 157 | + assert s.name != null, 'Missing `name` in shortcut #' + i |
| 158 | + assert s.short_name != null, 'Missing `short_name` in shortcut #' + i |
| 159 | + assert s.url != null, 'Missing `icon` in shortcut #' + i |
| 160 | + assert s.icon != null, 'Missing `url` in shortcut #' + i |
| 161 | + } |
| 162 | + |
| 163 | + def shortcutsFile = new File("$projectDir/src/main/res/xml", "shortcuts.xml") |
| 164 | + |
| 165 | + def xmlWriter = new StringWriter() |
| 166 | + def xmlMarkup = new MarkupBuilder(new IndentPrinter(xmlWriter, " ", true)) |
| 167 | + |
| 168 | + xmlMarkup |
| 169 | + .'shortcuts'('xmlns:android': 'http://schemas.android.com/apk/res/android') { |
| 170 | + twaManifest.shortcuts.eachWithIndex { s, i -> |
| 171 | + 'shortcut'( |
| 172 | + 'android:shortcutId': 'shortcut' + i, |
| 173 | + 'android:enabled': 'true', |
| 174 | + 'android:icon': '@drawable/' + s.icon, |
| 175 | + 'android:shortcutShortLabel': '@string/shortcut_short_name_' + i, |
| 176 | + 'android:shortcutLongLabel': '@string/shortcut_name_' + i) { |
| 177 | + 'intent'( |
| 178 | + 'android:action': 'android.intent.action.MAIN', |
| 179 | + 'android:targetPackage': twaManifest.applicationId, |
| 180 | + 'android:targetClass': twaManifest.applicationId + '.LauncherActivity', |
| 181 | + 'android:data': s.url) |
| 182 | + 'categories'('android:name': 'android.intent.category.LAUNCHER') |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + shortcutsFile.text = xmlWriter.toString() + '\n' |
| 187 | +} |
| 188 | + |
| 189 | +preBuild.dependsOn(generateShorcutsFile) |
| 190 | + |
| 191 | +repositories { |
| 192 | + |
| 193 | +} |
| 194 | + |
| 195 | +dependencies { |
| 196 | + implementation fileTree(include: ['*.jar'], dir: 'libs') |
| 197 | + |
| 198 | + implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.2.1' |
| 199 | + |
| 200 | +} |
0 commit comments