安装Android Studio
安装需要 上网 ,我这挺顺利的,就是在官网下载安装包,一路 Next,大概连下载总共半个小时。
第一个应用
参考官方教程:https://developer.android.com/codelabs/basic-android-kotlin-compose-first-app
设置 Gradle 国内源
参考:
今天终于把Android Studio依赖国内阿里云镜像搞定 - 知乎 (zhihu.com)
Gradle 配置国内镜像 - june.js - 博客园 (cnblogs.com)
配置gradle国内源,C:\Users\alex\.gradle\init.gradle文件内容:
allprojects{
repositories {
def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'
def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('http://repo1.maven.org/maven2')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://jcenter.bintray.com/') || url.startsWith('http://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
remove repo
}
}
}
maven {
url ALIYUN_REPOSITORY_URL
url ALIYUN_JCENTER_URL
}
}
buildscript{
repositories {
def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/public'
def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/jcenter'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('http://repo1.maven.org/maven2')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://jcenter.bintray.com/') || url.startsWith('http://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
remove repo
}
}
}
maven {
url ALIYUN_REPOSITORY_URL
url ALIYUN_JCENTER_URL
}
}
}
}
连接夜神模拟器
- 在夜神模拟器安装目录下,执行以下命令:
.\nox_adb.exe connect 127.0.0.1:62001
在模拟器中能成功安装应用,无法启动,报错:
$ adb shell am start -n "com.alex.greetingcard/com.alex.greetingcard.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Unexpected error while executing: am start -n "com.alex.greetingcard/com.alex.greetingcard.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Error while Launching activity
Failed to launch an application on all devices
- 先看是否模拟设备消失
参考:
Failed to launch emulator 和 Failed to install the app解决方法_error failed to launch emulator._Blooming_x的博客-CSDN博客
夜神模拟器adb调试频繁断开解决办法_qq910689331的博客-CSDN博客_adb经常掉线
解决方法:
3. 找到 Android SDK安装路径(C:\Users\XXX\AppData\Local\Android\Sdk\platform-tools)复制这三个到 夜神安装路径(D:\Program Files\Nox\bin)覆盖粘贴;
- 将夜神安装路径下的nox_adb.exe备份为nox_adb_bak.exe,将C:\Users\XXX\AppData\Local\Android\Sdk\platform-tools\adb.exe 多复制一份过来改名成
nox_adb.exe - 在命令行再次启动夜神模拟器adb,就不会掉线了,需要将 adb 路径放到环境变量Path下(C:\Users\XXX\AppData\Local\Android\Sdk\platform-tools)
连接真机调试
参考官方教程:https://developer.android.com/codelabs/basic-android-kotlin-compose-connect-device
- 真机开启开发者模式,一般是在 设置 中多点击几次版本号,在开发者设置中,开启USB调试
- 在 Android Studio 中安装 Google USB Driver
- 用USB线连接PC和手机
通过 Wi-Fi 在 Android 设备上运行应用,参考上面官方CodeLabs