Android 学习笔记
项目创建遇到的问题
1.Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all artifacts for configuration ':classpath'.
在Gradle在解析依赖项时遇到问题。Gradle默认去国外服务器下载,需指明国内镜像源。
2.ERROR: Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Failed to transform artifact 'activity.aar (androidx.activity:activity:1.8.0)' to match attributes {artifactType = jar}.
依赖版本不兼容。aar向jar转换失败,旧版AGP缺少对新版AndroidX的支持。
3.Invalid keystore format
Android构建系统尝试读取签名密钥库(keystore)时,发现文件格式不正确或已损坏。
生成项目的keystore文件。
keytool -genkey -v -keystore singlenews.keystore -alias singlenews -keyalg RSA -keysize 2048 -validity 3650
输入密钥库口令:singlenews
再次输入新口令:singlenews
您的名字与姓氏是什么:z
您的组织单位名称是什么:z
您的组织名称是什么:z
您所在的城市或区域名称是什么:z
您所在的省市自治区名称是什么:z
该单位的双字母国家 地区代码是什么:zz
验证keystore是否创建成功:keytool -list -v -keystore singlenews.keystore -storepass singlenews
Android Studio:在Android Studio中,通过Build → Generate Signed Bundle/APK使用此keystore
已成功解决了 keystore 和 JDK 配置问题,现在进入更核心的 Android 开发主题。
Gradle 配置与构建系统
1. Gradle 基础概念
-
为什么需要 Gradle?
- 替代 Ant/Maven 的现代化构建工具
- 自动化编译、测试、打包流程
- 依赖管理(自动下载库)
- 多模块项目支持
-
关键文件:
project/ ├── build.gradle # 项目级配置 └── app/ ├── build.gradle # 模块级配置 └── src/ # 源代码目录
2. 深入分析 build.gradle
项目级 build.gradle:
// 项目级 build.gradle
buildscript {
repositories {
google() // Google 仓库
mavenCentral() // Maven 中央仓库
maven { url 'https://maven.aliyun.com/repository/google' } // 解决问题1
maven { url 'https://maven.aliyun.com/repository/public' } // 解决问题1
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1' // Android Gradle Plugin
}
}
allprojects {
repositories {
google()
mavenCentral()
maven { url 'https://maven.aliyun.com/repository/google' } // 解决问题1
maven { url 'https://maven.aliyun.com/repository/public' } // 解决问题1
}
}
模块级 build.gradle(app 模块):
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.singlenews"
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
signingConfigs { // 解决问题3
debug {
storeFile file('singlenews.keystore')
storePassword "singlenews"
keyAlias "singlenews"
keyPassword "singlenews"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// 将androidx依赖版本降级 // 解决问题2
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
// implementation 'androidx.appcompat:appcompat:1.7.1'
// implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
// androidTestImplementation 'androidx.test.ext:junit:1.3.0'
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.7.0'
}
3. 关键配置详解
| 配置项 | 说明 | 常见值 |
|---|---|---|
compileSdkVersion | 编译时使用的 Android SDK 版本 | 30 |
minSdkVersion | 支持的最低 Android 版本 | 17 (Android 4.0) |
targetSdkVersion | 应用针对的 Android 版本 | 30 (推荐与 compileSdk 相同) |
versionCode | 应用内部版本号(整数) | 1, 2, 3... |
versionName | 用户可见版本号 | "1.0", "1.1" |
signingConfigs | 签名配置 | debug/release |
4. 动手实践:修复构建问题
-
修改 app/build.gradle:
signingConfigs { debug { storeFile file('singlenews.keystore') storePassword "singlenews" keyAlias "singlenews" keyPassword "singlenews" } } -
在 Android Studio 中:
- 点击
File→Invalidate Caches / Restart - 选择
Invalidate and Restart
- 点击
5. 构建流程解析
graph LR
A[源代码] --> B(编译)
B --> C[打包 APK]
C --> D[签名]
D --> E[安装到设备]
- 编译:将 Java/Kotlin 代码 → 字节码
- 打包:资源文件 + 字节码 → APK
- 签名:使用 keystore 签名 APK(防止篡改)
- 安装:将 APK 推送到设备
6. 常见 Gradle 问题速查
| 问题 | 解决方案 |
|---|---|
Could not find method | 检查 build.gradle 中的 dependencies 版本 |
Failed to resolve | 确保 repositories 包含 google() 和 mavenCentral() |
Gradle sync failed | 点击 File → Invalidate Caches / Restart |
Keystore not found | 使用默认 debug.keystore 或正确配置路径 |
🌟 学习小贴士
- 不要手动修改
build.gradle:使用 Android Studio 的 GUI 生成器(Build → Generate Signed Bundle/APK) - 版本匹配:确保
buildToolsVersion与compileSdkVersion一致 - 仓库优先级:
google()必须在mavenCentral()之前