Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat(native): let Android tablets rotate while phones stay portrait (#1159)

Zuyang Liu0a3236ad349c
diff --git a/app.config.js b/app.config.js
index ee73be10..053f53cf 100644
--- a/app.config.js
+++ b/app.config.js
@@ -1,15 +1,52 @@
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
+import configPlugins from 'expo/config-plugins.js'
+
+const { AndroidConfig, withAndroidManifest, withMainActivity } = configPlugins
// 本仓库根 package.json 是 `"type": "module"`,动态配置必须以 ESM 导出。
const rootDir = fileURLToPath(new URL('.', import.meta.url))
+const mainActivityClass = 'class MainActivity : ReactActivity() {'
+const superOnCreate = /^([ \t]*)super\.onCreate\(/m
+const orientationMembers = `
+ override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
+ super.onConfigurationChanged(newConfig)
+ applyScreenOrientation(newConfig)
+ }
+
+ private fun applyScreenOrientation(config: android.content.res.Configuration) {
+ requestedOrientation = if (config.smallestScreenWidthDp < 600) {
+ android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
+ } else {
+ android.content.pm.ActivityInfo.SCREEN_ORIENTATION_FULL_USER
+ }
+ }
+`
+
export default function appConfig({ config }) {
- return {
+ const withNotices = {
...config,
extra: {
...config.extra,
thirdPartyNotices: readFileSync(`${rootDir}THIRD_PARTY_NOTICES.md`, 'utf8'),
},
}
+ // Expo 的 `orientation` 由 iOS / Android 共用,Android 也没有 iPad 那样的平板专用键:
+ // 清单放开方向,再由 MainActivity 按最小宽度 600dp(与 Android 16 的大屏规则一致)把手机锁回竖屏。
+ const withManifest = withAndroidManifest(withNotices, (config) => {
+ AndroidConfig.Manifest.getMainActivityOrThrow(config.modResults).$['android:screenOrientation'] = 'fullUser'
+ return config
+ })
+ return withMainActivity(withManifest, (config) => {
+ const { contents } = config.modResults
+ if (contents.includes('applyScreenOrientation('))
+ return config
+ if (!contents.includes(mainActivityClass) || !superOnCreate.test(contents))
+ throw new Error('MainActivity.kt no longer matches the Expo template that the tablet orientation patch expects')
+ config.modResults.contents = contents
+ .replace(superOnCreate, '$1applyScreenOrientation(resources.configuration)\n$1super.onCreate(')
+ .replace(mainActivityClass, () => `${mainActivityClass}\n${orientationMembers}`)
+ return config
+ })
}
diff --git a/docs/specs/native.md b/docs/specs/native.md
index 5a088a2b..483929f5 100644
--- a/docs/specs/native.md
+++ b/docs/specs/native.md
@@ -33,7 +33,7 @@ pnpm typecheck:native # tsc --project tsconfig.native.json --noEmit
| 文件 | 作用 |
| --- | --- |
| `app.json` | Expo 静态配置:应用名 `DSH Mobile`、slug `dsh-bridge`、`expo.version`、Android 包名 `com.dshtauri.dshbridge`、`versionCode`、plugins、权限 |
-| `app.config.js` | Expo 动态配置(ESM):把 `THIRD_PARTY_NOTICES.md` 全文注入 `extra.thirdPartyNotices` |
+| `app.config.js` | Expo 动态配置(ESM):把 `THIRD_PARTY_NOTICES.md` 全文注入 `extra.thirdPartyNotices`;prebuild 时改写 Android 屏幕方向(见下) |
| `metro.config.js` | Metro 配置:`expo/metro-config.js` 全扩展名导入、`withUniwindConfig` 接入 CSS 入口、自定义 `resolver.resolveRequest` 钉住 `@/x`、`blockList` 屏蔽 `src-tauri` / `source` / `dist` 等大目录 |
| `src-native/entry.ts` | 移动端应用入口(只 `import 'expo-router/entry'`),由 `package.json` 的 `"main"` 指向 |
| `tsconfig.native.json` | 移动端类型检查工程,`extends: expo/tsconfig.base`,开启 `strict` 与 `noUncheckedIndexedAccess` |
@@ -48,6 +48,7 @@ pnpm typecheck:native # tsc --project tsconfig.native.json --noEmit
* `metro.config.js` 必须写完整的 `expo/metro-config.js`:仓库根 `package.json` 是 `"type": "module"`,Node 的 ESM 解析不做扩展名补齐,而 `expo` 包没有 `exports` 字段。
* 工程根 `tsconfig.json` 是桌面端配置(`"@/*": ["src/*"]`),而 Expo CLI 的 tsconfig paths 解析器读的正是工程根 tsconfig ⇒ native 的 `@/x` 会被错误解析到 `src/x`(同名模块还会静默串味)。因此 `metro.config.js` 在用户解析器里先行短路,把 `@/x` 钉到 `src-native/x`,与 `tsconfig.native.json` 的 paths 一致。
* `app.json` 的 `expo.version` **跟随桌面端版本**,Android `versionCode` 与之同步递增(由 §8.4 的 bumpp 补丁写入)。
+* **屏幕方向**:`app.json` 的 `orientation: "portrait"` 锁住 iPhone,iPad 由 `ios.infoPlist` 的 `UISupportedInterfaceOrientations~ipad` 放开四向。Android 没有平板专用键,`app.config.js` 在 prebuild 时把 MainActivity 的清单方向改为 `fullUser`,并在 `onCreate` / `onConfigurationChanged` 里按 `smallestScreenWidthDp < 600`(与 Android 16 大屏规则同一条线)把手机锁回竖屏;平板与展开的折叠屏随系统旋转设置。MainActivity 不再匹配 Expo 模板时 prebuild 直接报错,升级 Expo SDK 后需复核补丁锚点。
* `experiments` 开启 `typedRoutes` 与 `reactCompiler`,因此移动端同样**禁止**手写 `useCallback` / `useMemo`。
---
diff --git a/src-native/test/app-config.test.ts b/src-native/test/app-config.test.ts
index bee7965b..4d758feb 100644
--- a/src-native/test/app-config.test.ts
+++ b/src-native/test/app-config.test.ts
@@ -1,3 +1,4 @@
+import type { AndroidConfig, ExportedConfig } from 'expo/config-plugins'
import { readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
@@ -7,6 +8,39 @@ import { describe, expect, it } from 'vitest'
const projectRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../..')
const packageVersion = (JSON.parse(readFileSync(resolve(projectRoot, 'package.json'), 'utf8')) as { version: string }).version
+const mainActivityTemplate = `package com.dshtauri.dshbridge
+
+import android.os.Bundle
+
+import com.facebook.react.ReactActivity
+
+class MainActivity : ReactActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ setTheme(R.style.AppTheme);
+ super.onCreate(null)
+ }
+
+ override fun getMainComponentName(): String = "main"
+}
+`
+
+function moddedConfig() {
+ return getConfig(projectRoot, { isModdedConfig: true, skipSDKVersionRequirement: true }).exp as ExportedConfig
+}
+
+function modRequest(modName: string) {
+ return { projectRoot, platformProjectRoot: resolve(projectRoot, 'android'), modName, platform: 'android' as const, introspect: true }
+}
+
+async function patchMainActivity(contents: string) {
+ const config = moddedConfig()
+ const mod = config.mods?.android?.mainActivity
+ if (!mod)
+ throw new Error('app.config.js registers no Android mainActivity mod')
+ const result = await mod({ ...config, modRawConfig: config, modRequest: modRequest('mainActivity'), modResults: { path: 'MainActivity.kt', language: 'kt', contents } })
+ return result.modResults.contents
+}
+
describe('public Expo configuration packaged by expo-constants', () => {
it('embeds the complete third-party notice without changing Android identity', () => {
const { exp } = getConfig(projectRoot, { isPublicConfig: true, skipSDKVersionRequirement: true })
@@ -75,3 +109,65 @@ describe('public Expo configuration packaged by expo-constants', () => {
expect(eas.build?.production?.autoIncrement).toBe(true)
})
})
+
+describe('android orientation generated at prebuild', () => {
+ it('replaces the portrait lock that Expo writes on MainActivity with fullUser', async () => {
+ const config = moddedConfig()
+ const mod = config.mods?.android?.manifest
+ if (!mod)
+ throw new Error('app.config.js registers no Android manifest mod')
+ const manifest: AndroidConfig.Manifest.AndroidManifest = {
+ manifest: {
+ $: { 'xmlns:android': 'http://schemas.android.com/apk/res/android' },
+ queries: [],
+ application: [{
+ $: { 'android:name': '.MainApplication' },
+ activity: [{ $: { 'android:name': '.MainActivity', 'android:screenOrientation': 'portrait' } }],
+ }],
+ },
+ }
+
+ const result = await mod({ ...config, modRawConfig: config, modRequest: modRequest('manifest'), modResults: manifest })
+
+ expect(result.modResults.manifest.application?.[0]?.activity?.[0]?.$['android:screenOrientation']).toBe('fullUser')
+ })
+
+ it('locks MainActivity to portrait below a 600dp smallest width and to fullUser otherwise', async () => {
+ expect(await patchMainActivity(mainActivityTemplate)).toContain([
+ ' private fun applyScreenOrientation(config: android.content.res.Configuration) {',
+ ' requestedOrientation = if (config.smallestScreenWidthDp < 600) {',
+ ' android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT',
+ ' } else {',
+ ' android.content.pm.ActivityInfo.SCREEN_ORIENTATION_FULL_USER',
+ ' }',
+ ' }',
+ ].join('\n'))
+ })
+
+ it('applies the orientation before MainActivity calls super.onCreate', async () => {
+ expect(await patchMainActivity(mainActivityTemplate)).toContain(' applyScreenOrientation(resources.configuration)\n super.onCreate(null)\n')
+ })
+
+ it('re-applies the orientation when the configuration changes, such as a foldable unfolding', async () => {
+ expect(await patchMainActivity(mainActivityTemplate)).toContain([
+ 'class MainActivity : ReactActivity() {',
+ '',
+ ' override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {',
+ ' super.onConfigurationChanged(newConfig)',
+ ' applyScreenOrientation(newConfig)',
+ ' }',
+ ].join('\n'))
+ })
+
+ it('leaves an already patched MainActivity unchanged when prebuild runs again', async () => {
+ const patched = await patchMainActivity(mainActivityTemplate)
+
+ expect(await patchMainActivity(patched)).toBe(patched)
+ })
+
+ it('fails prebuild when MainActivity no longer matches the Expo template', async () => {
+ const changed = mainActivityTemplate.replace('class MainActivity : ReactActivity() {', 'class MainActivity : ReactActivity(), Runnable {')
+
+ await expect(patchMainActivity(changed)).rejects.toThrow('MainActivity.kt no longer matches the Expo template')
+ })
+})