Android报错以及常见开发问题集锦

250 阅读4分钟

adb

adb连接不上,也就是有些设备连接至Mac,AS不显示该设备

  • 需要找adbkey、adbkey.pub放置在~/.android目录下
  • 或者重启adb即可
adb kill-server  
adb start-server 

blog.csdn.net/sy373466062…

Kotlin

  • 关于kotlin版本不同步的报错
Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
    /Users/jiayuanfa/.gradle/caches/transforms-2/files-2.1/411e2e7616ac1a914ddfcb9fba518bc5/jetified-kotlin-stdlib-jdk7-1.5.0.jar (version 1.5)
    /Users/jiayuanfa/.gradle/caches/transforms-2/files-2.1/6a8c8e4eba2a83dc3f14973b01d6031e/jetified-kotlin-stdlib-1.6.10.jar (version 1.6)
    /Users/jiayuanfa/.gradle/caches/transforms-2/files-2.1/e8b1d2d5fdd86fecd68c254a6b3f4fd1/jetified-kotlin-stdlib-common-1.6.10.jar (version 1.6)

此问题目前都没找到什么好的解释,参照官方给的引入kotlin的方案也不行

Kotlin第一次安装的时候报错

Build file '/Users/jiayuanfa/Desktop/AndroidLearn/app/build.gradle' line: 43

A problem occurred evaluating project ':app'.
> Build was configured to prefer settings repositories over project repositories but repository 'MavenRepo' was added by build file 'app/build.gradle'

  • 此错误是因为:Gradle7.0之后配置项有修改,导致拉不下来Kotlin代码库
  • 解决问题的办法:在项目级别的setting.gradle中添加以下代码即可
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}
rootProject.name = "AndroidLearn"
include ':app'

Git

Thread

Exception

Java

NDK

NDK 配置报错

ndk does not contain any platforms解决

  • 缺失了platforms文件夹,直接删掉缺失该文件夹的NDK,重新下载NDK,重新配置即可

image.png

NDK版本过高,不适配当前版本的Android Studio。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:stripDebugDebugSymbols'.
> No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi
  • 此问题的报错,是因为指定编译的安卓平台所需要的NDK版本数据没有下载完整,去下载完整再切换即可

Gradle

Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.res.ResourceCompilerRunnable
Caused by: java.lang.ClassNotFoundException: Didn't find class "retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory" on path: DexPathList[[zip file "/data/app/~~R9pFine83DvAdmRAs7P3jQ==/com.example.hidecodesdkfordingding-MocJFfeGYxn6cbFpClYk3Q==/base.apk"],nativeLibraryDirectories=[/data/app/~~R9pFine83DvAdmRAs7P3jQ==/com.example.hidecodesdkfordingding-MocJFfeGYxn6cbFpClYk3Q==/lib/x86, /data/app/~~R9pFine83DvAdmRAs7P3jQ==/com.example.hidecodesdkfordingding-MocJFfeGYxn6cbFpClYk3Q==/base.apk!/lib/x86, /system/lib, /system_ext/lib]]
  • 引入对应的三方库即可

编译打包arr的时候报错

Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error). The following direct local .aar file dependencies of the :app project caused this error: 
  • 原因是因为要打包的module里边已经包含了其他的aar包

解决方案:

  • 首先将要打包的module的build.gradle中的依赖命令修改一下
compileOnly fileTree(dir: "libs", include: ["*.jar", "*.aar"])
  • 然后将module中所依赖的libs包在主工程中拷贝一份并放在libs下面,主工程app的build.gradle配置如下
implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])
  • 最后重新编译即可不报错,正确打出aar包
  • 需要注意,打出来的aar包并不包含其他aar包,其他项目使用的时候还是需要把其他的aar包拖入

AndroidX

Failed resolution of: Landroid/support/v4/content/LocalBroadcastManager;
  • androidx 依赖第三方库时,第三方库自身使用v4库,引起程序崩溃
  • 在gradle.properties中配置下列代码即可
android.enableJetifier=true

Flutter

Android

去除AndroidX依赖

Python

Python升级到版本3.x

Python从3.x降级到2.x

which python
  • 查看当前python安装目录
  • 文件夹前往进入目录
  • 删除所有当前关于python3的文件
python --version

image.png

  • 降级成功

Android 权限相关问题

相机以及相册以及存储读取等问题

    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

image.png

image.png

Camera

Camrea2

相机预览不能全屏展示,改动官方代码小于号改为大于号

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    if (0 == mRatioWidth || 0 == mRatioHeight) {
        setMeasuredDimension(width, height);
    } else {
        if (width > height * mRatioWidth / mRatioHeight) {
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        } else {
            setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
        }
    }
}

三方库

Zxing

解码报错

System.err: com.google.zxing.NotFoundException

暂时不用处理

ANR

一方面是不熟悉原理机制,还有一方面对CPU/RAM/IO/GC/Thermal、GC机制、内存分配、LMK机制、同步锁原理等基础掌握的不够

UI

ActionBar不起作用,给Activity设置主题即可

  • windowActionBarOverlay false 的意思是展示ActionBar并且内容展示在ActionBar下边而且不是覆盖,反之则覆盖显示
<style name="sdk.Theme2" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">#000000</item>
    <item name="colorSecondary">#FF03DAC5</item>
    <item name="android:statusBarColor" ns1:targetApi="l">#000000</item>

    <item name="android:windowActionBarOverlay">false</item>
    <item name="android:windowTranslucentStatus">false</item>
</style>

HTTP

java.io.IOException: Cleartext HTTP traffic to www.weather.com.cn not permitted
  • 高版本需要HTTPS,配置一下网络策略即可
  • 方法一:创建network_security_config.xml文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

并在application中设置

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AndroidLearn"
    android:networkSecurityConfig="@xml/network_security_config"
    tools:targetApi="31">
  • 方法二:直接设置
usesCleartextTraffic="true"
<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AndroidLearn"
    android:usesCleartextTraffic="true"
    tools:targetApi="31">

Gradle编译命令报错

Kotlin could not find the required JDK tools in the Java installation '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home' used by Gradle. Make sure Gradle is running on a JDK, not JRE.

sudo rm -rf ~/.gradle