4

现在我正在将我的 android 代码转换为模块化架构方法。尝试从“聊天”模块添加对“应用程序”模块的依赖时面临问题。

我有以下“app”模块的构建配置。

android {

lintOptions {
    checkReleaseBuilds false
    abortOnError false
}

signingConfigs {
    companydevconfig {
        keyAlias 'company'
        keyPassword '123456'
        storeFile file('../app/jksFils/company_dev.jks')
        storePassword '123456'
    }
    companyqaconfig {
        keyAlias 'company'
        keyPassword '123456'
        storeFile file('../app/jksFils/company_qa.jks')
        storePassword '123456'
    }
    companyprodconfig {
        keyAlias 'company'
        keyPassword '123456'
        storeFile file('../app/jksFils/release.keystore')
        storePassword '123456'
    }
}

compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
    applicationId "com.company.employee.dev"
    minSdkVersion 21
    targetSdkVersion 28
    versionCode 1
    versionName "1.13"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
aaptOptions {
    cruncherEnabled = false
}
testOptions {
    unitTests.returnDefaultValues = true
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        testCoverageEnabled true
    }

}

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

dataBinding {
    enabled = true
}
packagingOptions {
    exclude 'META-INF/ASL2.0'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/rxjava.properties'
}
flavorDimensions "company"
productFlavors {
    dev {
        dimension "company"
        applicationId "com.company.employee.dev"
        versionCode 277
        versionName "2.0.0.16"
        signingConfig signingConfigs.companydevconfig

        buildConfigField 'String', 'BASEURL', '"https://dev.company.com"'
    }
    qa {
        dimension "company"
        applicationId "com.company.employee.qa"
        versionCode 225
        versionName "2.0.2.2"
        signingConfig signingConfigs.companyqaconfig

        buildConfigField 'String', 'BASEURL', '"https://qa.company.com"'

    }
    prod {
        dimension "company"
        applicationId "com.company.employee.prod"
        versionCode 38
        versionName "1.5.20"
        signingConfig signingConfigs.companyprodconfig

        buildConfigField 'String', 'BASEURL', '"https://cloud.company.com"'

    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
    unitTests.returnDefaultValues = true
    unitTests.all {
        setIgnoreFailures(true)
        jacoco {
            includeNoLocationClasses = true
        }
    }
  }
}

现在我添加了一个新模块“聊天”。它在构建配置中有以下代码。

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"

defaultConfig {
    applicationId "com.company.employee.chat"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
    }
}
sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

dataBinding {
    enabled true
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
    jvmTarget = "1.8"
}
}

dependencies {
implementation project(':app')

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

当我尝试构建时,我得到以下错误。

ERROR: Unable to resolve dependency for ':chat@debug/compileClasspath': Could not 
resolve project :app.
Show Details
Affected Modules: chat


ERROR: Unable to resolve dependency for ':chat@debugAndroidTest/compileClasspath': Could not 
resolve project :app.
Show Details
Affected Modules: chat


ERROR: Unable to resolve dependency for ':chat@debugUnitTest/compileClasspath': Could not 
resolve project :app.
Show Details
Affected Modules: chat


ERROR: Unable to resolve dependency for ':chat@release/compileClasspath': Could not resolve 
project :app.
Show Details
Affected Modules: chat


ERROR: Unable to resolve dependency for ':chat@releaseUnitTest/compileClasspath': Could not 
resolve project :app.
Show Details
Affected Modules: chat
4

2 回答 2

1

这里有一些要考虑的事情

  1. App Module和之间有区别Library Module

库模块符合.aar/file. 但是,App 模块编译为APK. 这意味着您不能将App Module作为依赖项导入Library Module。因此,从库模块中删除这一行:

 implementation project(':app')
  1. 确保库列在settings.gradle.

打开 App Module 的 settings.gradle 并确保列出了您的库

include ':app', ':chat'
  1. 将库模块导入您的App Module Build Gradle

将您的库模块导入为依赖项

dependencies {
implementation project(':chat')
}

最重要的是看看:创建一个 Android 库

于 2020-02-10T02:13:02.223 回答
0

您可能正在导入Application而不是Module。好吧,您也可以在模块的 Gradle 中更改它。

改变

apply plugin: 'com.android.application'

apply plugin: 'com.android.library'

您还需要从 gradle中删除applicationId 。

然后,在app.gradle 的依赖项中

代替

实施项目(':lib-name')

我用了,

实施项目(路径:':lib-name',配置:'默认')

例如:实施项目(路径:':myService',配置:
'默认')

它就像一个魅力。:)

我正在添加一个带有服务的依赖模块,并且没有将库作为 AOSP 项目的一部分。

以防万一,它可以帮助某人。

于 2020-02-11T03:37:28.757 回答