返回顶部
a

android-native-dev Android原生开发

Android native application development and UI design guide. Covers Material Design 3, Kotlin/Compose development, project configuration, accessibility, and build troubleshooting. Read this before Android native application development.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
277
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

android-native-dev

1. 项目场景评估

在开始开发之前,评估当前项目状态:

场景特征方法
空目录无文件存在需要完整初始化,包括 Gradle Wrapper
已有 Gradle Wrapper
存在 gradlew 和 gradle/wrapper/ 目录 | 直接使用 ./gradlew 进行构建 |
| Android Studio 项目 | 完整的项目结构,可能缺少 Wrapper | 检查 Wrapper,如有需要运行 gradle wrapper |
| 不完整的项目 | 存在部分文件 | 检查缺失文件,完成配置 |

关键原则

  • - 在编写业务逻辑之前,确保 ./gradlew assembleDebug 能够成功执行
  • 如果缺少 gradle.properties,请先创建它并配置 AndroidX

1.1 必需文件清单

MyApp/
├── gradle.properties # 配置 AndroidX 和其他设置
├── settings.gradle.kts
├── build.gradle.kts # 根级别
├── gradle/wrapper/
│ └── gradle-wrapper.properties
├── app/
│ ├── build.gradle.kts # 模块级别
│ └── src/main/
│ ├── AndroidManifest.xml
│ ├── java/com/example/myapp/
│ │ └── MainActivity.kt
│ └── res/
│ ├── values/
│ │ ├── strings.xml
│ │ ├── colors.xml
│ │ └── themes.xml
│ └── mipmap-*/ # 应用图标



2. 项目配置

2.1 gradle.properties

properties

必需配置


android.useAndroidX=true
android.enableJetifier=true

构建优化

org.gradle.parallel=true kotlin.code.style=official

JVM 内存设置(根据项目大小调整)

小型项目:2048m,中型:4096m,大型:8192m+

org.gradle.jvmargs=-Xmx4096m -Dfile.encoding=UTF-8

注意:如果在构建过程中遇到 OutOfMemoryError,请增加 -Xmx 的值。依赖较多的大型项目可能需要 8GB 或更多内存。

2.2 依赖声明标准

kotlin
dependencies {
// 使用 BOM 管理 Compose 版本
implementation(platform(androidx.compose:compose-bom:2024.02.00))
implementation(androidx.compose.ui:ui)
implementation(androidx.compose.material3:material3)

// Activity & ViewModel
implementation(androidx.activity:activity-compose:1.8.2)
implementation(androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0)
}

2.3 构建变体与产品风味

产品风味允许您创建应用的不同版本(例如,免费/付费,开发/预发布/生产)。

在 app/build.gradle.kts 中的配置

kotlin
android {
// 定义风味维度
flavorDimensions += environment

productFlavors {
create(dev) {
dimension = environment
applicationIdSuffix = .dev
versionNameSuffix = -dev

// 每个风味的不同配置值
buildConfigField(String, APIBASEURL, \https://dev-api.example.com\)
buildConfigField(Boolean, ENABLE_LOGGING, true)

// 不同的资源
resValue(string, app_name, MyApp Dev)
}

create(staging) {
dimension = environment
applicationIdSuffix = .staging
versionNameSuffix = -staging

buildConfigField(String, APIBASEURL, \https://staging-api.example.com\)
buildConfigField(Boolean, ENABLE_LOGGING, true)
resValue(string, app_name, MyApp Staging)
}

create(prod) {
dimension = environment
// 生产版本无后缀

buildConfigField(String, APIBASEURL, \https://api.example.com\)
buildConfigField(Boolean, ENABLE_LOGGING, false)
resValue(string, app_name, MyApp)
}
}

buildTypes {
debug {
isDebuggable = true
isMinifyEnabled = false
}
release {
isDebuggable = false
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile(proguard-android-optimize.txt), proguard-rules.pro)
}
}
}

构建变体命名:{flavor}{BuildType} → 例如,devDebug,prodRelease

Gradle 构建命令

bash

列出所有可用的构建变体


./gradlew tasks --group=build

构建特定变体(风味 + 构建类型)

./gradlew assembleDevDebug # Dev 风味,Debug 构建 ./gradlew assembleStagingDebug # Staging 风味,Debug 构建 ./gradlew assembleProdRelease # Prod 风味,Release 构建

构建特定风味的所有变体

./gradlew assembleDev # 所有 Dev 变体(debug + release) ./gradlew assembleProd # 所有 Prod 变体

构建特定构建类型的所有变体

./gradlew assembleDebug # 所有风味,Debug 构建 ./gradlew assembleRelease # 所有风味,Release 构建

将特定变体安装到设备

./gradlew installDevDebug ./gradlew installProdRelease

在一个命令中构建并安装

./gradlew installDevDebug && adb shell am start -n com.example.myapp.dev/.MainActivity

在代码中访问 BuildConfig

注意:从 AGP 8.0 开始,默认不再生成 BuildConfig。您必须在 build.gradle.kts 中显式启用它:
kotlin
android {
buildFeatures {
buildConfig = true
}
}

kotlin

// 在代码中使用构建配置值 val apiUrl = BuildConfig.APIBASEURL val isLoggingEnabled = BuildConfig.ENABLE_LOGGING

if (BuildConfig.DEBUG) {
// 仅调试代码
}

风味特定的源集

app/src/
├── main/ # 所有风味共享的代码
├── dev/ # 仅 Dev 的代码和资源
│ ├── java/
│ └── res/
├── staging/ # 仅 Staging 的代码和资源
├── prod/ # 仅 Prod 的代码和资源
├── debug/ # Debug 构建类型代码
└── release/ # Release 构建类型代码

多个风味维度(例如,environment + tier):

kotlin
android {
flavorDimensions += listOf(environment, tier)

productFlavors {
create(dev) { dimension = environment }
create(prod) { dimension = environment }

create(free) { dimension = tier }
create(paid) { dimension = tier }
}
}
// 结果:devFreeDebug, devPaidDebug, prodFreeRelease 等



3. Kotlin 开发标准

3.1 命名约定

类型约定示例
类/接口PascalCaseUserRepository,MainActivity
函数/变量
camelCase | getUserName(),isLoading | | 常量 | SCREAMINGSNAKE | MAXRETRY_COUNT | | 包 | 小写 | com.example.myapp | | Composable | PascalCase | @Composable fun UserCard() |

3.2 代码标准(重要)

空安全
kotlin
// ❌ 避免:非空断言 !!(可能崩溃)
val name = user!!.name

// ✅ 推荐:安全调用 + 默认值
val name = user?.name ?: Unknown

// ✅ 推荐:let 处理
user?.let { processUser(it) }

异常处理
kotlin
// ❌ 避免:在业务层随意使用 try-catch 吞掉异常
fun loadData() {
try {
val data = api.fetch()
} catch (e: Exception) {
// 吞掉异常,难以调试
}
}

// ✅ 推荐:让异常传播,在适当的层处理
suspend fun loadData(): Result<

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 android-native-dev-1775875990 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 android-native-dev-1775875990 技能

通过命令行安装

skillhub install android-native-dev-1775875990

下载

⬇ 下载 android-native-dev v1.0.0(免费)

文件大小: 38.99 KB | 发布时间: 2026-4-12 09:00

v1.0.0 最新 2026-4-12 09:00
Initial release of the Android Native Application Development and UI Design Guide.

- Covers project setup, required files checklist, and configuration for new and existing projects.
- Details best practices for gradle.properties, dependency management, build variants, and product flavors.
- Explains use of flavor-specific source sets and commands for building/installing variants.
- Incorporates Kotlin development standards, including naming and null-safety conventions.
- References Material Design 3, Android official docs, Google Play Quality, and WCAG guidelines.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部