project-template.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import path from "node:path";
  2. type TemplateInput = {
  3. compileSdk: number;
  4. minSdk: number;
  5. name: string;
  6. pkg: string;
  7. };
  8. type TemplateFile = {
  9. body: (input: TemplateInput) => string;
  10. path: string;
  11. };
  12. export function templateFiles(root: string, pkg: string, sdk?: string): TemplateFile[] {
  13. const pkgDir = pkg.replaceAll(".", "/");
  14. return [
  15. { path: path.join(root, "settings.gradle.kts"), body: settings },
  16. { path: path.join(root, "build.gradle.kts"), body: rootBuild },
  17. { path: path.join(root, "gradle.properties"), body: gradleProperties },
  18. ...(sdk
  19. ? [{ path: path.join(root, "local.properties"), body: () => localProperties(sdk) }]
  20. : []),
  21. { path: path.join(root, "gradle", "libs.versions.toml"), body: versions },
  22. { path: path.join(root, "app", "build.gradle.kts"), body: appBuild },
  23. { path: path.join(root, "app", "src", "main", "AndroidManifest.xml"), body: manifest },
  24. { path: path.join(root, "app", "src", "main", "res", "values", "strings.xml"), body: strings },
  25. { path: path.join(root, "app", "src", "main", "res", "values", "styles.xml"), body: styles },
  26. {
  27. path: path.join(root, "app", "src", "main", "java", pkgDir, "MainActivity.kt"),
  28. body: activity,
  29. },
  30. ];
  31. }
  32. function settings(input: TemplateInput) {
  33. return `pluginManagement {
  34. repositories {
  35. google()
  36. mavenCentral()
  37. gradlePluginPortal()
  38. }
  39. }
  40. dependencyResolutionManagement {
  41. repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
  42. repositories {
  43. google()
  44. mavenCentral()
  45. }
  46. }
  47. rootProject.name = "${input.name}"
  48. include(":app")
  49. `;
  50. }
  51. function rootBuild() {
  52. return `plugins {
  53. alias(libs.plugins.android.application) apply false
  54. alias(libs.plugins.kotlin.android) apply false
  55. alias(libs.plugins.kotlin.compose) apply false
  56. }
  57. `;
  58. }
  59. function gradleProperties() {
  60. return `android.useAndroidX=true
  61. android.nonTransitiveRClass=true
  62. org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
  63. `;
  64. }
  65. function localProperties(sdk: string) {
  66. return `sdk.dir=${sdk.replaceAll("\\", "\\\\")}
  67. `;
  68. }
  69. function versions() {
  70. return `[versions]
  71. agp = "8.7.3"
  72. kotlin = "2.0.21"
  73. coreKtx = "1.15.0"
  74. activityCompose = "1.9.3"
  75. composeBom = "2024.12.01"
  76. [libraries]
  77. androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
  78. androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
  79. androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
  80. androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" }
  81. androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
  82. androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" }
  83. [plugins]
  84. android-application = { id = "com.android.application", version.ref = "agp" }
  85. kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
  86. kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
  87. `;
  88. }
  89. function appBuild(input: TemplateInput) {
  90. return `plugins {
  91. alias(libs.plugins.android.application)
  92. alias(libs.plugins.kotlin.android)
  93. alias(libs.plugins.kotlin.compose)
  94. }
  95. android {
  96. namespace = "${input.pkg}"
  97. compileSdk = ${input.compileSdk}
  98. defaultConfig {
  99. applicationId = "${input.pkg}"
  100. minSdk = ${input.minSdk}
  101. targetSdk = ${input.compileSdk}
  102. versionCode = 1
  103. versionName = "1.0"
  104. }
  105. }
  106. dependencies {
  107. implementation(libs.androidx.core.ktx)
  108. implementation(libs.androidx.activity.compose)
  109. implementation(platform(libs.androidx.compose.bom))
  110. implementation(libs.androidx.compose.ui)
  111. implementation(libs.androidx.compose.ui.tooling.preview)
  112. implementation(libs.androidx.compose.material3)
  113. }
  114. `;
  115. }
  116. function manifest() {
  117. return `<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  118. <application
  119. android:allowBackup="true"
  120. android:label="@string/app_name"
  121. android:supportsRtl="true"
  122. android:theme="@style/AppTheme">
  123. <activity
  124. android:name=".MainActivity"
  125. android:exported="true">
  126. <intent-filter>
  127. <action android:name="android.intent.action.MAIN" />
  128. <category android:name="android.intent.category.LAUNCHER" />
  129. </intent-filter>
  130. </activity>
  131. </application>
  132. </manifest>
  133. `;
  134. }
  135. function strings(input: TemplateInput) {
  136. return `<resources>
  137. <string name="app_name">${input.name}</string>
  138. </resources>
  139. `;
  140. }
  141. function styles() {
  142. return `<resources>
  143. <style name="AppTheme" parent="android:style/Theme.Material.Light.NoActionBar" />
  144. </resources>
  145. `;
  146. }
  147. function activity(input: TemplateInput) {
  148. return `package ${input.pkg}
  149. import android.os.Bundle
  150. import androidx.activity.ComponentActivity
  151. import androidx.activity.compose.setContent
  152. import androidx.compose.foundation.layout.Arrangement
  153. import androidx.compose.foundation.layout.Column
  154. import androidx.compose.foundation.layout.fillMaxSize
  155. import androidx.compose.foundation.layout.padding
  156. import androidx.compose.material3.Button
  157. import androidx.compose.material3.MaterialTheme
  158. import androidx.compose.material3.Surface
  159. import androidx.compose.material3.Text
  160. import androidx.compose.runtime.Composable
  161. import androidx.compose.runtime.getValue
  162. import androidx.compose.runtime.mutableIntStateOf
  163. import androidx.compose.runtime.remember
  164. import androidx.compose.runtime.setValue
  165. import androidx.compose.ui.Alignment
  166. import androidx.compose.ui.Modifier
  167. import androidx.compose.ui.semantics.contentDescription
  168. import androidx.compose.ui.semantics.semantics
  169. import androidx.compose.ui.unit.dp
  170. class MainActivity : ComponentActivity() {
  171. override fun onCreate(savedInstanceState: Bundle?) {
  172. super.onCreate(savedInstanceState)
  173. setContent {
  174. App()
  175. }
  176. }
  177. }
  178. @Composable
  179. fun App() {
  180. var count by remember { mutableIntStateOf(0) }
  181. MaterialTheme {
  182. Surface(modifier = Modifier.fillMaxSize()) {
  183. Column(
  184. modifier = Modifier
  185. .fillMaxSize()
  186. .padding(24.dp),
  187. horizontalAlignment = Alignment.CenterHorizontally,
  188. verticalArrangement = Arrangement.Center,
  189. ) {
  190. Text(
  191. text = "Hello from Android",
  192. style = MaterialTheme.typography.headlineMedium,
  193. )
  194. Text(
  195. text = "Count: $count",
  196. modifier = Modifier.semantics { contentDescription = "countLabel" },
  197. style = MaterialTheme.typography.titleLarge,
  198. )
  199. Button(
  200. onClick = { count += 1 },
  201. modifier = Modifier.semantics { contentDescription = "incrementButton" },
  202. ) {
  203. Text("Increment")
  204. }
  205. }
  206. }
  207. }
  208. }
  209. `;
  210. }