@nativue/core 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +33 -8
- package/README.md +2 -1
- package/android/build.gradle.kts +4 -0
- package/android/nativue.settings.gradle.kts +2 -0
- package/android/src/main/java/dev/nativue/Bridge.kt +118 -12
- package/android/src/main/java/dev/nativue/DownloadBanner.kt +45 -0
- package/android/src/main/java/dev/nativue/NativueActivity.kt +26 -5
- package/android/src/main/java/dev/nativue/Permissions.kt +107 -0
- package/android/src/main/java/dev/nativue/Plugins.kt +98 -15
- package/cli/android-project.mjs +377 -0
- package/cli/app-config.mjs +515 -0
- package/cli/config.d.mts +162 -3
- package/cli/hot-client.js +192 -0
- package/cli/ios-project.mjs +652 -0
- package/cli/link.mjs +66 -71
- package/cli/nativue.mjs +262 -58
- package/cli/plugins.mjs +236 -0
- package/cli/prebuild.mjs +458 -0
- package/cli/run.mjs +2 -2
- package/cli/serialize.mjs +168 -0
- package/cli/symbolicate.mjs +26 -14
- package/ios/AppLifecycle.swift +165 -0
- package/ios/Bridge.swift +110 -9
- package/ios/DownloadBanner.swift +38 -0
- package/ios/NativueView.swift +24 -1
- package/ios/Permissions.swift +19 -0
- package/ios/Plugins.swift +24 -0
- package/package.json +3 -2
- package/src/globals.ts +8 -1
- package/src/native-events.ts +99 -0
- package/src/native-functions.ts +16 -2
- package/src/permissions.ts +57 -0
- package/src/renderer.ts +6 -1
- package/src/router.ts +31 -1
- package/src/types.ts +6 -0
- package/template/android/app/src/debug/AndroidManifest.xml +4 -0
- package/template/android/app/src/main/java/MainActivity.kt +5 -0
- package/template/android/app/src/main/res/drawable-night-xxxhdpi/splash_logo.png +0 -0
- package/template/android/app/src/main/res/drawable-xxxhdpi/splash_logo.png +0 -0
- package/template/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml +6 -0
- package/template/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/template/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png +0 -0
- package/template/android/app/src/main/res/values/colors.xml +4 -0
- package/template/android/app/src/main/res/values/styles.xml +15 -0
- package/template/android/app/src/main/res/values-night/styles.xml +5 -0
- package/template/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/template/android/gradle/wrapper/gradle-wrapper.properties +9 -0
- package/template/android/gradlew +248 -0
- package/template/android/gradlew.bat +112 -0
- package/template/ios/App/App.swift +18 -0
- package/template/ios/App/Assets.xcassets/AppIcon.appiconset/Contents.json +1 -0
- package/template/ios/App/Assets.xcassets/AppIcon.appiconset/icon.png +0 -0
- package/template/ios/App/Assets.xcassets/Contents.json +1 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/Contents.json +11 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@1x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@2x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo-dark@3x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@1x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@2x.png +0 -0
- package/template/ios/App/Assets.xcassets/LaunchLogo.imageset/logo@3x.png +0 -0
- package/template/ios/App.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +15 -0
|
@@ -2,6 +2,7 @@ package dev.nativue
|
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import android.view.View
|
|
5
|
+
import androidx.activity.ComponentActivity
|
|
5
6
|
import java.util.concurrent.atomic.AtomicBoolean
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -16,33 +17,106 @@ interface NativuePlugin {
|
|
|
16
17
|
/** sendEvent("change", payload) reaches the element's @change; event names are lowercase. */
|
|
17
18
|
typealias SendEvent = (eventName: String, payload: Any?) -> Unit
|
|
18
19
|
|
|
19
|
-
/**
|
|
20
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Settles an async function's JavaScript Promise; only the first call counts. reject's code is the
|
|
22
|
+
* JavaScript Error's `code`. Codes nativue's packages share: ERR_PERMISSION_DENIED, ERR_UNAVAILABLE,
|
|
23
|
+
* ERR_CANCELED, ERR_INVALID_ARGUMENT.
|
|
24
|
+
*/
|
|
25
|
+
class Promise internal constructor(
|
|
26
|
+
private val settle: (value: Any?, errorMessage: String?, errorCode: String?) -> Unit,
|
|
27
|
+
) {
|
|
21
28
|
private val settled = AtomicBoolean(false)
|
|
22
29
|
|
|
23
30
|
fun resolve(value: Any? = null) {
|
|
24
|
-
if (settled.compareAndSet(false, true)) settle(value, null)
|
|
31
|
+
if (settled.compareAndSet(false, true)) settle(value, null, null)
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
fun reject(errorMessage: String) {
|
|
28
|
-
if (settled.compareAndSet(false, true)) settle(null, errorMessage)
|
|
35
|
+
if (settled.compareAndSet(false, true)) settle(null, errorMessage, null)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** e.g. reject(code = "ERR_CANCELED", errorMessage = "The picker was closed") */
|
|
39
|
+
fun reject(code: String, errorMessage: String) {
|
|
40
|
+
if (settled.compareAndSet(false, true)) settle(null, errorMessage, code)
|
|
29
41
|
}
|
|
30
42
|
}
|
|
31
43
|
|
|
44
|
+
/** Thrown by an async function: rejects its Promise with this code, as Promise.reject(code, message). */
|
|
45
|
+
class PluginException(val code: String, message: String) : Exception(message)
|
|
46
|
+
|
|
32
47
|
/** Called on the JavaScript thread; settles `promise` from any thread, when the work is done. */
|
|
33
48
|
typealias AsyncFunction = (arguments: List<Any?>, promise: Promise) -> Unit
|
|
34
49
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
50
|
+
interface ActivityListener {
|
|
51
|
+
/** Register activity-result launchers here, every time and in the same order, so results survive a recreation. */
|
|
52
|
+
fun onActivityCreated(activity: ComponentActivity) {}
|
|
53
|
+
|
|
54
|
+
fun onActivityDestroyed(activity: ComponentActivity) {}
|
|
55
|
+
|
|
56
|
+
/** A URL the app was opened with, before JavaScript's onOpenURL and the router. true: taken. */
|
|
57
|
+
fun open(url: String): Boolean = false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** One for the whole process, so a service (a push with the app closed) reaches the plugins too. */
|
|
61
|
+
class PluginRegistry private constructor(context: Context) {
|
|
62
|
+
val applicationContext: Context = context.applicationContext
|
|
63
|
+
|
|
64
|
+
/** Never keep it: it's replaced when the activity is recreated. */
|
|
65
|
+
@Volatile
|
|
66
|
+
var activity: ComponentActivity? = null
|
|
67
|
+
private set
|
|
68
|
+
|
|
69
|
+
/** Plugins from before 0.14 cast it to Activity. */
|
|
70
|
+
@Deprecated("Use applicationContext, or activity / requireActivity() for the activity.")
|
|
71
|
+
val context: Context
|
|
72
|
+
get() = activity ?: applicationContext
|
|
73
|
+
|
|
74
|
+
/** Set by the Bridge: calls JavaScript's listeners. */
|
|
75
|
+
internal var sendToJavaScript: (eventName: String, payload: Any?) -> Unit = { _, _ -> }
|
|
76
|
+
internal var functions = mutableMapOf<String, NativeFunction>()
|
|
77
|
+
private set
|
|
78
|
+
internal var asyncFunctions = mutableMapOf<String, AsyncFunction>()
|
|
79
|
+
private set
|
|
80
|
+
internal var viewMakers = mutableMapOf<String, (SendEvent) -> View>()
|
|
81
|
+
private set
|
|
82
|
+
private val plugins = pluginClassNames(applicationContext).map { className ->
|
|
83
|
+
Class.forName(className).getDeclaredConstructor().newInstance() as NativuePlugin
|
|
84
|
+
}
|
|
85
|
+
private val activityListeners = plugins.filterIsInstance<ActivityListener>()
|
|
86
|
+
|
|
87
|
+
val permissions = Permissions(this)
|
|
88
|
+
|
|
89
|
+
fun requireActivity(): ComponentActivity =
|
|
90
|
+
activity ?: throw PluginException("ERR_UNAVAILABLE", "The app has no activity open")
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Plugins register again for each activity, as when the registry was the activity's: those that
|
|
94
|
+
* keep what register read (the activity's window) get the new one.
|
|
95
|
+
*/
|
|
96
|
+
internal fun activityCreated(activity: ComponentActivity) {
|
|
97
|
+
this.activity = activity
|
|
98
|
+
permissions.activityCreated(activity)
|
|
99
|
+
functions = mutableMapOf()
|
|
100
|
+
asyncFunctions = mutableMapOf()
|
|
101
|
+
viewMakers = mutableMapOf()
|
|
102
|
+
plugins.forEach { it.register(this) }
|
|
103
|
+
activityListeners.forEach { it.onActivityCreated(activity) }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
internal fun activityDestroyed(activity: ComponentActivity) {
|
|
107
|
+
if (this.activity === activity) this.activity = null
|
|
108
|
+
permissions.activityDestroyed(activity)
|
|
109
|
+
activityListeners.forEach { it.onActivityDestroyed(activity) }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
internal fun open(url: String): Boolean = activityListeners.any { it.open(url) }
|
|
113
|
+
|
|
114
|
+
companion object {
|
|
115
|
+
@Volatile
|
|
116
|
+
private var instance: PluginRegistry? = null
|
|
117
|
+
|
|
118
|
+
fun get(context: Context): PluginRegistry =
|
|
119
|
+
instance ?: synchronized(this) { instance ?: PluginRegistry(context).also { instance = it } }
|
|
46
120
|
}
|
|
47
121
|
|
|
48
122
|
/** Runs on the JavaScript thread and returns synchronously: post to the main thread for views. */
|
|
@@ -55,6 +129,15 @@ class PluginRegistry internal constructor(val context: Context) {
|
|
|
55
129
|
asyncFunctions[name] = function
|
|
56
130
|
}
|
|
57
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Reaches JavaScript's addNativeListener(eventName) listeners, from any thread, e.g.
|
|
134
|
+
* sendEvent("notifications.received", payload). Dropped while no JavaScript runs: keep what must
|
|
135
|
+
* outlive it (a push token) and let JavaScript read it with a function.
|
|
136
|
+
*/
|
|
137
|
+
fun sendEvent(eventName: String, payload: Any? = null) {
|
|
138
|
+
sendToJavaScript(eventName, payload)
|
|
139
|
+
}
|
|
140
|
+
|
|
58
141
|
/** `type` is the element's tag, e.g. "MapView" for h('MapView'). Created on the main thread. */
|
|
59
142
|
fun addView(type: String, makeView: (SendEvent) -> View) {
|
|
60
143
|
viewMakers[type] = makeView
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
// The Android project files prebuild builds from the config, instead of copying them from the
|
|
2
|
+
// template: the app's manifest, Gradle files and strings, its resources, and the icon and splash
|
|
3
|
+
// images. A null file removes the template's.
|
|
4
|
+
import { readFileSync } from 'node:fs'
|
|
5
|
+
import { join, relative, resolve } from 'node:path'
|
|
6
|
+
import { toPosixPath } from './link.mjs'
|
|
7
|
+
import { appResources, emptyGradle, gradleProblems, resourceFile } from './app-config.mjs'
|
|
8
|
+
import { applyMods } from './plugins.mjs'
|
|
9
|
+
import { androidString, xml } from './serialize.mjs'
|
|
10
|
+
|
|
11
|
+
// Changes the activity handles itself, so they don't recreate it and restart JS.
|
|
12
|
+
const CONFIG_CHANGES = [
|
|
13
|
+
'orientation',
|
|
14
|
+
'screenSize',
|
|
15
|
+
'smallestScreenSize',
|
|
16
|
+
'screenLayout',
|
|
17
|
+
'keyboard',
|
|
18
|
+
'keyboardHidden',
|
|
19
|
+
'uiMode',
|
|
20
|
+
'fontScale',
|
|
21
|
+
'locale',
|
|
22
|
+
'layoutDirection',
|
|
23
|
+
]
|
|
24
|
+
const ANDROID_PLUGIN_VERSION = '9.5.1'
|
|
25
|
+
const ANDROID_NAMESPACE = 'http://schemas.android.com/apk/res/android'
|
|
26
|
+
const TOOLS_NAMESPACE = 'http://schemas.android.com/tools'
|
|
27
|
+
// The splash icon without a background is 288dp; the logo, 120dp as on iOS, is inset in it. A
|
|
28
|
+
// fraction, since Android scales the icon to its own size.
|
|
29
|
+
const SPLASH_LOGO_INSET = `${(((288 - 120) / 2 / 288) * 100).toFixed(2)}%`
|
|
30
|
+
|
|
31
|
+
/** Inside a Kotlin "string": no escapes or $templates. */
|
|
32
|
+
function kotlinString(text) {
|
|
33
|
+
return `"${text.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('$', '\\$')}"`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function element(tag, attributes = {}, children = []) {
|
|
37
|
+
return { tag, attributes, children }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function intentFilter(action, categories, children = []) {
|
|
41
|
+
return element('intent-filter', {}, [
|
|
42
|
+
element('action', { 'android:name': action }),
|
|
43
|
+
...categories.map((category) => element('category', { 'android:name': category })),
|
|
44
|
+
...children,
|
|
45
|
+
])
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The base manifest, every element a plugin's manifest mod may change. */
|
|
49
|
+
export function nativueManifest(app) {
|
|
50
|
+
const mainActivity = element(
|
|
51
|
+
'activity',
|
|
52
|
+
{
|
|
53
|
+
'android:name': '.MainActivity',
|
|
54
|
+
'android:configChanges': CONFIG_CHANGES.join('|'),
|
|
55
|
+
'android:exported': 'true',
|
|
56
|
+
'android:launchMode': 'singleTask',
|
|
57
|
+
'android:windowSoftInputMode': 'adjustResize',
|
|
58
|
+
},
|
|
59
|
+
[
|
|
60
|
+
intentFilter('android.intent.action.MAIN', ['android.intent.category.LAUNCHER']),
|
|
61
|
+
intentFilter(
|
|
62
|
+
'android.intent.action.VIEW',
|
|
63
|
+
['android.intent.category.DEFAULT', 'android.intent.category.BROWSABLE'],
|
|
64
|
+
app.schemes.map((scheme) => element('data', { 'android:scheme': scheme })),
|
|
65
|
+
),
|
|
66
|
+
],
|
|
67
|
+
)
|
|
68
|
+
const application = element(
|
|
69
|
+
'application',
|
|
70
|
+
{
|
|
71
|
+
'android:icon': '@mipmap/ic_launcher',
|
|
72
|
+
'android:label': '@string/app_name',
|
|
73
|
+
'android:theme': '@style/AppTheme',
|
|
74
|
+
},
|
|
75
|
+
[mainActivity],
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
return element(
|
|
79
|
+
'manifest',
|
|
80
|
+
{ 'xmlns:android': ANDROID_NAMESPACE, 'xmlns:tools': TOOLS_NAMESPACE },
|
|
81
|
+
[application],
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isPermission(child, names) {
|
|
86
|
+
return child.tag === 'uses-permission' && names.includes(child.attributes['android:name'])
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* nativue's manifest, then the plugins' mods, then the app's permissions. A blocked permission is
|
|
91
|
+
* removed here and, with tools:node="remove", from the packages' manifests Gradle merges in.
|
|
92
|
+
*/
|
|
93
|
+
function manifest(app, plugins, context) {
|
|
94
|
+
const document = applyMods(plugins, 'android', 'manifest', nativueManifest(app), context)
|
|
95
|
+
const blocked = app.android.blockedPermissions ?? []
|
|
96
|
+
const requested = (app.android.permissions ?? []).filter((name) => !blocked.includes(name))
|
|
97
|
+
const permissions = [
|
|
98
|
+
...requested.map((name) => element('uses-permission', { 'android:name': name })),
|
|
99
|
+
...blocked.map((name) =>
|
|
100
|
+
element('uses-permission', { 'android:name': name, 'tools:node': 'remove' }),
|
|
101
|
+
),
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
...document,
|
|
106
|
+
children: [
|
|
107
|
+
...permissions,
|
|
108
|
+
// The app's entry replaces a plugin's of the same name, whatever attributes it had.
|
|
109
|
+
...document.children.filter((child) => !isPermission(child, [...requested, ...blocked])),
|
|
110
|
+
],
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The plugins' strings, then the app's name. */
|
|
115
|
+
function strings(app, plugins, context) {
|
|
116
|
+
const values = { ...applyMods(plugins, 'android', 'strings', {}, context), app_name: app.name }
|
|
117
|
+
|
|
118
|
+
return element(
|
|
119
|
+
'resources',
|
|
120
|
+
{},
|
|
121
|
+
Object.entries(values).map(([name, text]) => ({
|
|
122
|
+
tag: 'string',
|
|
123
|
+
attributes: { name },
|
|
124
|
+
text: androidString(String(text)),
|
|
125
|
+
})),
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Release signing from a keystore outside android/, its passwords never written to a file. */
|
|
130
|
+
function signingGradle(signing, appDirectory) {
|
|
131
|
+
if (!signing) return { top: '', android: '' }
|
|
132
|
+
|
|
133
|
+
const storeFile = toPosixPath(
|
|
134
|
+
relative(join(appDirectory, 'android', 'app'), resolve(appDirectory, signing.storeFile)),
|
|
135
|
+
)
|
|
136
|
+
const secrets = [signing.storePasswordEnv, signing.keyPasswordEnv]
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
top: `
|
|
140
|
+
// Release signing's passwords: a Gradle property (~/.gradle/gradle.properties, which Android Studio
|
|
141
|
+
// reads too) or an environment variable (CI). Debug builds don't need them.
|
|
142
|
+
fun secret(name: String): String? =
|
|
143
|
+
providers.gradleProperty(name).orElse(providers.environmentVariable(name)).orNull
|
|
144
|
+
|
|
145
|
+
val missingSigningSecrets = listOf(${secrets.map((name) => `"${name}"`).join(', ')}).filter { secret(it) == null }
|
|
146
|
+
|
|
147
|
+
// Stops before building, with the names; any other release build fails at signing.
|
|
148
|
+
if (missingSigningSecrets.isNotEmpty() && gradle.startParameter.taskNames.any { it.contains("Release") }) {
|
|
149
|
+
throw GradleException(
|
|
150
|
+
${kotlinString(`A release build is signed with ${signing.storeFile}, and needs `)} +
|
|
151
|
+
missingSigningSecrets.joinToString() +
|
|
152
|
+
": set them as environment variables, or in ~/.gradle/gradle.properties.",
|
|
153
|
+
)
|
|
154
|
+
}
|
|
155
|
+
`,
|
|
156
|
+
android: `
|
|
157
|
+
signingConfigs {
|
|
158
|
+
create("release") {
|
|
159
|
+
storeFile = file(${kotlinString(storeFile)})
|
|
160
|
+
storePassword = secret("${signing.storePasswordEnv}")
|
|
161
|
+
keyAlias = ${kotlinString(signing.keyAlias)}
|
|
162
|
+
keyPassword = secret("${signing.keyPasswordEnv}")
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
buildTypes {
|
|
167
|
+
getByName("release") {
|
|
168
|
+
signingConfig = signingConfigs.getByName("release")
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
`,
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function unique(values) {
|
|
176
|
+
return [...new Set(values)]
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The plugins' gradle mods, then the app's android.gradle: its values win, lists are joined. */
|
|
180
|
+
function gradleModel(app, plugins, context) {
|
|
181
|
+
const fromPlugins = applyMods(plugins, 'android', 'gradle', emptyGradle(), context)
|
|
182
|
+
const problems = gradleProblems(fromPlugins, "the plugins' gradle mods' result").filter(Boolean)
|
|
183
|
+
|
|
184
|
+
if (problems.length > 0) throw new Error(problems.join('\n'))
|
|
185
|
+
|
|
186
|
+
const ownGradle = app.android.gradle ?? {}
|
|
187
|
+
const own = Object.fromEntries(
|
|
188
|
+
Object.entries(emptyGradle()).map(([key, empty]) => [key, ownGradle[key] ?? empty]),
|
|
189
|
+
)
|
|
190
|
+
const gradlePlugins = [...fromPlugins.plugins, ...own.plugins]
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
plugins: [...new Map(gradlePlugins.map((plugin) => [plugin.id, plugin])).values()],
|
|
194
|
+
dependencies: unique([...fromPlugins.dependencies, ...own.dependencies]),
|
|
195
|
+
manifestPlaceholders: { ...fromPlugins.manifestPlaceholders, ...own.manifestPlaceholders },
|
|
196
|
+
repositories: unique([...fromPlugins.repositories, ...own.repositories]),
|
|
197
|
+
properties: { ...fromPlugins.properties, ...own.properties },
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function lines(values, indent) {
|
|
202
|
+
return values.map((value) => `${indent}${value}\n`).join('')
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function appGradle(app, appDirectory, gradle) {
|
|
206
|
+
const signing = signingGradle(app.android.signing, appDirectory)
|
|
207
|
+
const pluginIds = gradle.plugins.map(({ id }) => `id(${kotlinString(id)})`)
|
|
208
|
+
const placeholders = Object.entries(gradle.manifestPlaceholders).map(
|
|
209
|
+
([key, value]) => `manifestPlaceholders[${kotlinString(key)}] = ${kotlinString(value)}`,
|
|
210
|
+
)
|
|
211
|
+
const dependencies = gradle.dependencies.map(
|
|
212
|
+
(dependency) => `implementation(${kotlinString(dependency)})`,
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
return `plugins {
|
|
216
|
+
id("com.android.application")
|
|
217
|
+
${lines(pluginIds, ' ')}}
|
|
218
|
+
${signing.top}
|
|
219
|
+
android {
|
|
220
|
+
namespace = "${app.android.package}"
|
|
221
|
+
compileSdk = 37
|
|
222
|
+
|
|
223
|
+
defaultConfig {
|
|
224
|
+
applicationId = "${app.android.package}"
|
|
225
|
+
minSdk = 24
|
|
226
|
+
targetSdk = 36
|
|
227
|
+
versionCode = ${app.buildNumber}
|
|
228
|
+
versionName = "${app.version}"
|
|
229
|
+
${lines(placeholders, ' ')} }
|
|
230
|
+
${signing.android}
|
|
231
|
+
compileOptions {
|
|
232
|
+
sourceCompatibility = JavaVersion.VERSION_17
|
|
233
|
+
targetCompatibility = JavaVersion.VERSION_17
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
dependencies {
|
|
238
|
+
implementation(project(":nativue"))
|
|
239
|
+
${lines(dependencies, ' ')}}
|
|
240
|
+
`
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** The root build file: the version of every Gradle plugin the app applies. */
|
|
244
|
+
function rootGradle(gradle) {
|
|
245
|
+
const versioned = gradle.plugins
|
|
246
|
+
.filter(({ version }) => version)
|
|
247
|
+
.map(
|
|
248
|
+
({ id, version }) => `id(${kotlinString(id)}) version ${kotlinString(version)} apply false`,
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
return `plugins {
|
|
252
|
+
id("com.android.application") version "${ANDROID_PLUGIN_VERSION}" apply false
|
|
253
|
+
id("com.android.library") version "${ANDROID_PLUGIN_VERSION}" apply false
|
|
254
|
+
${lines(versioned, ' ')}}
|
|
255
|
+
`
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function settingsGradle(gradle, corePath) {
|
|
259
|
+
const repositories = lines(
|
|
260
|
+
gradle.repositories.map((url) => `maven(${kotlinString(url)})`),
|
|
261
|
+
' ',
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
return `pluginManagement {
|
|
265
|
+
repositories {
|
|
266
|
+
google()
|
|
267
|
+
mavenCentral()
|
|
268
|
+
gradlePluginPortal()
|
|
269
|
+
${repositories} }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
dependencyResolutionManagement {
|
|
273
|
+
repositories {
|
|
274
|
+
google()
|
|
275
|
+
mavenCentral()
|
|
276
|
+
${repositories} }
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
rootProject.name = "App"
|
|
280
|
+
include(":app")
|
|
281
|
+
include(":nativue")
|
|
282
|
+
project(":nativue").projectDir = file("${corePath}/android")
|
|
283
|
+
// The Android side of the app's packages (see the file)
|
|
284
|
+
apply(from = "${corePath}/android/nativue.settings.gradle.kts")
|
|
285
|
+
`
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function gradleProperties(gradle) {
|
|
289
|
+
const properties = Object.entries(gradle.properties).map(([key, value]) => `${key}=${value}`)
|
|
290
|
+
|
|
291
|
+
return `org.gradle.jvmargs=-Xmx4g -Dfile.encoding=UTF-8\n${lines(properties, '')}`
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** res/<type>/<name>.<extension>, or assets/<name>. */
|
|
295
|
+
function resourceFiles(resources, readAppFile) {
|
|
296
|
+
return Object.fromEntries(
|
|
297
|
+
resources
|
|
298
|
+
.filter((resource) => resource.android)
|
|
299
|
+
.map((resource) => [
|
|
300
|
+
`app/src/main/${resourceFile(resource)}`,
|
|
301
|
+
{ content: readAppFile(resource.source) },
|
|
302
|
+
]),
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** The template's logo, or the app's centered at 120dp in the splash icon. */
|
|
307
|
+
function splashFiles(splash, readAppFile) {
|
|
308
|
+
if (!splash.image) return {}
|
|
309
|
+
|
|
310
|
+
const icon = element('inset', {
|
|
311
|
+
'xmlns:android': ANDROID_NAMESPACE,
|
|
312
|
+
'android:drawable': '@drawable/splash_image',
|
|
313
|
+
'android:inset': SPLASH_LOGO_INSET,
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
return {
|
|
317
|
+
'app/src/main/res/drawable-xxxhdpi/splash_logo.png': null,
|
|
318
|
+
'app/src/main/res/drawable-night-xxxhdpi/splash_logo.png': null,
|
|
319
|
+
'app/src/main/res/drawable/splash_logo.xml': xml(icon),
|
|
320
|
+
// 360px at 3x is the logo's 120dp.
|
|
321
|
+
'app/src/main/res/drawable-xxhdpi/splash_image.png': { content: readAppFile(splash.image) },
|
|
322
|
+
'app/src/main/res/drawable-night-xxhdpi/splash_image.png': {
|
|
323
|
+
content: readAppFile(splash.darkImage),
|
|
324
|
+
},
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** The legacy icon, and the adaptive one Android 8+ shows; without it, the legacy icon everywhere. */
|
|
329
|
+
function iconFiles(app, readAppFile) {
|
|
330
|
+
const adaptiveIcon = app.android.adaptiveIcon
|
|
331
|
+
const legacy = app.icon
|
|
332
|
+
? { 'app/src/main/res/mipmap-xxxhdpi/ic_launcher.png': { content: readAppFile(app.icon) } }
|
|
333
|
+
: {}
|
|
334
|
+
|
|
335
|
+
if (adaptiveIcon) {
|
|
336
|
+
// No <monochrome>: Android 13's themed icon would tint the whole colored foreground.
|
|
337
|
+
const icon = element('adaptive-icon', { 'xmlns:android': ANDROID_NAMESPACE }, [
|
|
338
|
+
element('background', { 'android:drawable': '@color/ic_launcher_background' }),
|
|
339
|
+
element('foreground', { 'android:drawable': '@mipmap/ic_launcher_foreground' }),
|
|
340
|
+
])
|
|
341
|
+
|
|
342
|
+
return {
|
|
343
|
+
...legacy,
|
|
344
|
+
'app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml': xml(icon),
|
|
345
|
+
'app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png': {
|
|
346
|
+
content: readAppFile(adaptiveIcon.foregroundImage),
|
|
347
|
+
},
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (!app.icon) return {}
|
|
352
|
+
|
|
353
|
+
return {
|
|
354
|
+
...legacy,
|
|
355
|
+
'app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml': null,
|
|
356
|
+
'app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png': null,
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Paths relative to android/. */
|
|
361
|
+
export function androidProjectFiles(app, { appDirectory, corePath, plugins = [] }) {
|
|
362
|
+
const readAppFile = (path) => readFileSync(resolve(appDirectory, path))
|
|
363
|
+
const context = { config: app, projectRoot: appDirectory }
|
|
364
|
+
const gradle = gradleModel(app, plugins, context)
|
|
365
|
+
|
|
366
|
+
return {
|
|
367
|
+
'build.gradle.kts': rootGradle(gradle),
|
|
368
|
+
'settings.gradle.kts': settingsGradle(gradle, corePath),
|
|
369
|
+
'gradle.properties': gradleProperties(gradle),
|
|
370
|
+
'app/build.gradle.kts': appGradle(app, appDirectory, gradle),
|
|
371
|
+
'app/src/main/AndroidManifest.xml': xml(manifest(app, plugins, context)),
|
|
372
|
+
'app/src/main/res/values/strings.xml': xml(strings(app, plugins, context)),
|
|
373
|
+
...iconFiles(app, readAppFile),
|
|
374
|
+
...splashFiles(app.splash, readAppFile),
|
|
375
|
+
...resourceFiles(appResources(app, plugins), readAppFile),
|
|
376
|
+
}
|
|
377
|
+
}
|