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

Android Studio(Kotlin构建)无法识别标签类型问题求助

问题解决:构建签名Bundle时style.xml报错

问题背景

在Android Studio 7.3.1搭配Gradle 7.4环境下构建签名Bundle,仅在构建流程中(首次触发)出现style.xml两次报错,已查阅大量同类问题帖但未找到有效解决方案。项目在其他开发者电脑可正常构建,怀疑缺少组件或仓库配置存在问题。

附代码信息

布局文件(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ToggleButton
        android:id="@+id/toggle_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Strobe"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:min="100"
        android:max="300"
        android:layout_marginStart="64dp"
        android:layout_marginEnd="64dp"
        android:layout_marginBottom="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

模块级build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.example.someonestrobe'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.someonestrobe"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            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'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

解决方案

  • 检查根目录build.gradle的仓库配置
    确保项目根目录的build.gradle中添加了谷歌仓库,否则无法拉取Material、ConstraintLayout等依赖:

    buildscript {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath "com.android.tools.build:gradle:7.4.0"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20" // 版本需与Android Studio插件匹配
        }
    }
    
    allprojects {
        repositories {
            google()
            mavenCentral()
        }
    }
    
  • 验证Android Studio组件安装
    打开SDK Manager:

    1. 确认SDK Platforms中已安装Android 12L(API 32)
    2. 确认SDK Tools中已安装对应版本的Android Build Tools(建议32.0.0或兼容版本)
    3. 确认Kotlin插件版本与gradle插件版本匹配(Android Studio 7.3.1对应Kotlin 1.7.x)
  • 清理项目缓存与构建文件

    1. 执行File > Invalidate Caches...,勾选Clear file system cache and local history,重启Android Studio
    2. 执行Build菜单中的Clean Project,再执行Rebuild Project
    3. 终端执行命令:./gradlew clean build --refresh-dependencies(Windows系统用gradlew.bat)
  • 检查style.xml的主题配置
    因为报错出现在style.xml,大概率是主题引用问题:

    1. 确保主题继承自MaterialComponents相关主题,比如:
      <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
          <!-- 自定义属性 -->
      </style>
      
    2. 检查是否引用了未定义的主题属性,若有需修正或添加对应依赖
  • 同步Gradle配置
    点击Android Studio右上角的Sync Project with Gradle Files按钮,确保所有依赖都已正确拉取

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 03:20:59