最近遇到的一些 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 【取消勾选】

最终再同步一次即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值