You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio生成的APK可安装于microC但无法运行的问题排查

应用在Android 9开发板无法启动问题排查与解决

问题概况

  • 应用在Android Studio模拟器中运行正常,安装至搭载Android 9的microC开发板后,安装成功但无法启动
  • adb logcat捕获错误:WindowManager: unable to start animation, surface is null or no children

相关配置

模块build.gradle

plugins {
    id 'com.android.application'
//    id 'org.jetbrains.kotlin.android'
    id 'maven-publish'
}

android {
    namespace 'com.example.nfcnpn532'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.nfcpn532"
        minSdk 28
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
/*    kotlinOptions {
        jvmTarget = '1.8'
    }*/
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.3.2'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
    sourceSets {
        main{
            jniLibs.srcDirs = ['src/main/jniLibs']
        }
    }
    defaultConfig {
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a", "x86_64", "x86"
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
    externalNativeBuild {
        cmake {
            version "3.30.5"
        }
    }
    ndkVersion '25.1.8937393'
}

dependencies {
    implementation "androidx.annotation:annotation:1.6.0"
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'androidx.test:core:1.5.0'
    androidTestImplementation 'androidx.test:runner:1.5.2'
    androidTestImplementation 'commons-net:commons-net:3.8.0'
    androidTestImplementation 'org.apache.commons:commons-lang3:3.11'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.github.mik3y:usb-serial-for-android:3.5.1'
    implementation 'androidx.core:core:1.10.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation "androidx.compose.ui:ui:1.4.3"
    implementation "androidx.compose.material3:material3:1.1.1"
    implementation "androidx.compose.foundation:foundation:1.4.3"
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.google.errorprone:error_prone_annotations:2.3.4'
    compileOnly 'com.google.errorprone:error_prone_annotations:2.3.4'
    compileOnly 'javax.annotation:javax.annotation-api:1.3.2'

    constraints {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
            because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib")
        }
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
            because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
        }
    }
}

项目根build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.2'
        classpath 'com.android.tools.build:gradle:8.0.2'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
    }
}

排查与解决步骤

1. 修复Gradle插件重复声明

根build.gradle中重复添加了Android Gradle插件,保留一个版本即可,建议保留7.2.2(与现有Compose版本兼容性更好),删除classpath 'com.android.tools.build:gradle:8.0.2'这一行。

2. 降级Compose版本适配Android 9

高版本Compose对Android 9的动画支持存在兼容性问题,将Compose相关依赖降级到适配API 28的版本:
修改模块build.gradle中的相关配置:

buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion '1.2.0'
}
dependencies {
    implementation "androidx.compose.ui:ui:1.2.1"
    implementation "androidx.compose.material3:material3:1.0.1"
    implementation "androidx.compose.foundation:foundation:1.2.1"
}

同步项目后编译测试。

3. 优化ABI适配

开发板为ARM架构,移除x86系列ABI过滤,只保留ARM相关:

defaultConfig {
    ndk {
        abiFilters "armeabi-v7a", "arm64-v8a"
    }
}

同时确认jniLibs目录下的库文件包含对应ARM架构的版本。

4. 禁用窗口动画验证

在开发板的开发者选项中,关闭窗口动画缩放、过渡动画缩放、动画程序时长缩放三项。如果应用能正常启动,说明是动画兼容性问题,可在AndroidManifest.xml的<application>标签中添加以下配置永久禁用应用动画:

android:windowAnimationStyle="@null"
android:hardwareAccelerated="true"

5. 检查Surface初始化逻辑

如果应用使用了自定义SurfaceView或JNI操作Surface,确保在Android 9上Surface完成初始化后再执行绘制或动画操作,避免在Surface未就绪时触发相关逻辑导致报错。

内容的提问来源于stack exchange,提问作者Wan Ammar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 22:47:03