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

Android多模块项目:app模块无法导入dev_resources模块类

Android多模块项目无法引用dev_resources模块类的问题排查

我正在开发一个Android多模块项目,app模块始终找不到dev_resources模块中的类,已经尝试给dev_resources添加library插件但问题仍未解决。


现有配置文件

app模块build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    namespace 'es.paytef.hioposconnector'
    compileSdk 33

    defaultConfig {
        applicationId "es.paytef.hioposconnector"
        minSdk 24
        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'
        }
    }
    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}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.5.1'
    implementation platform('androidx.compose:compose-bom:2022.10.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    implementation 'androidx.appcompat:appcompat-resources:1.6.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    // Librerías agregadas
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.7.20"
    implementation 'com.google.code.gson:gson:2.9.0'
    implementation("javax.xml.stream:stax-api:1.0-2")

    // retrofit
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation "com.squareup.okhttp3:okhttp:4.9.1"
    implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"

    // hilt
    implementation "com.google.dagger:hilt-android:2.40"
    //Singleton and Inject annotation
    implementation 'javax.inject:javax.inject:1'
    implementation 'androidx.test.ext:junit-ktx:1.1.3'
    implementation 'androidx.test:runner:1.4.0'
    kapt "com.google.dagger:hilt-android-compiler:2.40"
    implementation "androidx.hilt:hilt-navigation-fragment:1.0.0"
    implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0-alpha03"

    //hilt libraries for custom Component.
    implementation "com.google.dagger:dagger:2.40"
    kapt "com.google.dagger:dagger-compiler:2.40"

    //dev_resource
    implementation project(path: ':dev_resources')
}

dev_resources模块build.gradle

group 'org.openapitools'
version '1.0.0'

buildscript {
    ext.kotlin_version = '1.7.21'
    ext.retrofitVersion = '2.9.0'

    repositories {
        maven { url "https://repo1.maven.org/maven2" }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'
apply plugin: 'maven-publish'

repositories {
    maven { url "https://repo1.maven.org/maven2" }
}

test {
    useJUnitPlatform()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
    implementation "com.google.code.gson:gson:2.9.0"
    implementation "com.squareup.okhttp3:logging-interceptor:4.10.0"
    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-scalars:$retrofitVersion"
    testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
}

settings.gradle

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "HioPos Connector"
include ':app'
include ':dev_resources'

问题排查与修复方案

核心问题是dev_resources模块未配置为Android兼容的库模块,即使添加了library插件也没匹配正确类型,同时存在版本冲突等细节问题,具体修复步骤如下:

  1. 修正dev_resources的插件配置
    替换原有插件声明为Android库专用插件,同时移除冗余的buildscript块(根目录已统一管理插件依赖):

    plugins {
        id 'com.android.library'
        id 'org.jetbrains.kotlin.android'
        id 'maven-publish'
    }
    
  2. 添加Android库核心配置
    在dev_resources的build.gradle中添加android块,确保编译环境与app模块对齐:

    android {
        namespace 'org.openapitools.devresources' // 自定义符合模块的命名空间
        compileSdk 33
    
        defaultConfig {
            minSdk 24
            targetSdk 33
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    
  3. 统一依赖版本避免冲突
    将dev_resources中okhttp-logging-interceptor版本从4.10.0改为4.9.1,与app模块保持一致;同时把kotlin版本改为1.7.20,对齐app模块的kotlin-reflect版本。

  4. 清理并重新构建

    • 执行Android Studio的File -> Invalidate Caches...,选择Invalidate and Restart
    • 运行命令./gradlew clean build清理旧产物并重新编译

修改后的完整dev_resources build.gradle示例:

group 'org.openapitools'
version '1.0.0'

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

android {
    namespace 'org.openapitools.devresources'
    compileSdk 33

    defaultConfig {
        minSdk 24
        targetSdk 33
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
    implementation "com.google.code.gson:gson:2.9.0"
    implementation "com.squareup.okhttp3:logging-interceptor:4.9.1"
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation "com.squareup.retrofit2:converter-scalars:2.9.0"
    testImplementation "io.kotlintest:kotlintest-runner-junit5:3.4.2"
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 17:59:59