最近遇到的一些 Android 小问题
1、as 打包错误:字节的 UTF-8 序列的字节 2 无效
在 gradle.properties
添加如下配置
org.gradle.jvmargs=-Dfile.encoding=UTF-8
2、as 打包错误:unexpected element <provider> found in <manifest><queries>
检查 AGP、gradle 版本,如果版本过低,需要升级版本,如 AGP 升级到 4.1.2、Gradle 升级到 6.5.1
3、as 打包错误:More than one file was found with OS independent path
build.gradle 添加如下配置:
android {
packagingOptions {
exclude 'META-INF/***'
}
}
4、as 打包错误:进程占用文件
Cannot lock file *** it has already been locked by this process.
使用进程占用解锁工具 Lockhunter 解除占用
该工具提供命令行接口,可以在 build.gradle 每次构建完成之后自动化执行解锁
project.gradle.addBuildListener(new BuildListener() {
@Override
void settingsEvaluated(Settings settings) {
}
@Override
void projectsLoaded(Gradle gradle) {
}
@Override
void projectsEvaluated(Gradle gradle) {
}
@Override
void buildFinished(BuildResult buildResult) {
try {
//解锁的文件或目录路径
def buildPath = project.getProjectDir().getAbsolutePath() + File.separator + "build"
invokeLockHunter(buildPath)
} catch (Exception e) {
println("invokeLockHunter e =" + e)
}
}
})
def invokeLockHunter(String filePath) {
if (filePath == null || filePath.isEmpty()) {
return
}
if (!new File(filePath).exists()) {
return
}
def key = "LockHunter"
def has = project.properties.containsKey(key)
if (!has) {
println("invokeLockHunter dn 未配置 LockHunter.exe 路径")
return
}
String val = project.properties[key]
if (val == null || val.empty) {
return
}
String[] pathArr = val.split(",")
if (pathArr == null || pathArr.length == 0) {
return
}
def unlockFilePath = ""
for (int i = 0; i < pathArr.length; i++) {
def unlockFile = new File(pathArr[i])
if (unlockFile.exists() && !unlockFile.isDirectory()) {
unlockFilePath = unlockFile.getAbsolutePath()
break
}
}
if (unlockFilePath.isEmpty()) {
return
}
try {
project.exec(new Action<ExecSpec>() {
@Override
void execute(ExecSpec execSpec) {
execSpec.setExecutable("cmd")
execSpec.args("-c")
execSpec.setStandardOutput(out)
execSpec.setIgnoreExitValue(true)
ArrayList<String> args = new ArrayList<>()
args.add(unlockFilePath)
args.add("-u")
args.add("-x")
args.add("-sm")
args.add(filePath)
execSpec.commandLine(args.toArray())
}
})
} catch (Exception e) {
println("invokeLockHunter e =" + e)
}
}
5、闪退 PendingIntent 低版本的 androidx.work:work-runtime
闪退日志
argeting S+ (version " + Build.VERSION_CODES.S and above)
requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE" be specified
when creating a PendingIntent.\nStrongly consider"
using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality"
depends on the PendingIntent being mutable, e.g. if it needs to"
be used with inline replies or bubbles.
需升级androidx.work:work-runtime:2.7.0
版本至少到 2.7.0
如果你不知道都有哪些版本,可以在这里找 Jetpack WorkManager
6、as 打包、app 运行日志不全(覆盖)
找 as 安装目录下的配置文件 bin/idea.properties
修改日志缓存大小,重启 as
7、as 控制台、文件乱码
8、as Gradle Task 任务列表为空 & Task list not build …
高 as 版本
默认是不勾选
Setting -> experimental -> Gradle -> Configure all Gradle tasks during Gradle Sync【勾选上】
低 as 版本
有些默认是勾选
Setting -> experimental -> Gradle -> Do not build gradle task list during Gradle sync 【取消勾选】
最终再同步一次即可