Gradle 无法解析来自另一个模块的引用
Gradle can't resolve references from another module
我有一个包含以下模块的项目:
- 应用
- 设备
应用程序取决于设备。设备包含应用引用的 NetworkComponent class。更具体地说:
package com.some.package.ui.login;
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.some.package.R
import com.some.package.device.network.NetworkComponent
import com.some.package.ui.terms.TermsActivity
class LoginActivity : AppCompatActivity(),
LoginViewModel.Navigator {
val loginViewModel by lazy { LoginViewModel(this, NetworkComponent()) }
在 Android Studio 中一切都解决得很好。然而,当我尝试构建项目时,遇到构建错误:
e: /Users/android/some-app-android/app/src/main/kotlin/com/some/package/ui/login/LoginActivity.kt: (7, 27): Unresolved reference: network
e: /Users/android/some-app-android/app/src/main/kotlin/com/some/package/ui/login/LoginActivity.kt: (13, 55): Unresolved reference: NetworkComponent
[KOTLIN] deleting /Users/android/some-app-android/app/build/tmp/kotlin-classes/debug on error
[KOTLIN] deleting /Users/android/some-app-android/app/build/tmp/kotlin-classes/debug on error
:app:compileDebugKotlin FAILED
这是 NetworkComponent 的样子:
package com.some.package.device.network
import com.some.package.domain.datasources.CodeValidator
import retrofit2.Retrofit
class NetworkComponent : CodeValidator {
val codeValidator: CodeValidatorApi
init {
val retrofit = Retrofit.Builder()
.baseUrl("www.test.com")
.build()
codeValidator = retrofit.create(CodeValidatorApi::class.java)
}
override fun validate(code: String) = codeValidator.validate(code)
}
以下是构建文件:
顶部:
buildscript {
ext.kotlin_version = '1.1.51'
ext.android_tools = '3.0.0'
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:$android_tools"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
应用程序:
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
//apply plugin: 'io.fabric'
// Manifest version information
def versionMajor = 0
def versionMinor = 0
def versionPatch = 1
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.some.package"
minSdkVersion 21
targetSdkVersion 27
versionCode versionMajor * 10000 + versionMinor * 100 + versionPatch
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
// applicationIdSuffix '.debug'
resValue "string", "application_name", "TEST APP Debug"
debuggable true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
resValue "string", "application_name", "TEST APP"
}
}
dataBinding {
enabled = true
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
debug.java.srcDirs += 'src/debug/kotlin'
}
packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/services/javax.annotation.processing.Processor' // butterknife
}
}
repositories {
maven { url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo' }
}
dependencies {
implementation project(':device')
implementation project(':data')
implementation project(':domain')
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:support-v4:27.0.0'
implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'com.android.support:design:27.0.0'
implementation 'com.android.support:support-annotations:27.0.0'
kapt "com.android.databinding:compiler:$android_tools"
// Logging
implementation 'com.jakewharton.timber:timber:4.5.1'
// Unit tests
testImplementation 'junit:junit:4.12'
// testImplementation 'org.robolectric:robolectric:3.0'
testImplementation 'org.mockito:mockito-core:2.11.0'
// testImplementation 'joda-time:joda-time:2.9.4'
// debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.1'
// releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
// testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
implementation 'io.reactivex.rxjava2:rxkotlin:2.1.0'
debugImplementation 'com.facebook.stetho:stetho:1.4.1'
debugImplementation 'com.uphyca:stetho_realm:2.0.0'
// implementation('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
// transitive = true;
// }
}
设备:
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':domain')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'io.reactivex.rxjava2:rxkotlin:2.1.0'
implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
testImplementation 'junit:junit:4.12'
}
我不太确定这是怎么发生的。我尝试将 gradle 插件降级到 2.3.3,但没有成功。
有什么想法吗?
原来我在我的设备模块构建文件中遗漏了这个关键行:
apply plugin: 'kotlin-android'
感谢这个答案提供的线索
我有一个包含以下模块的项目:
- 应用
- 设备
应用程序取决于设备。设备包含应用引用的 NetworkComponent class。更具体地说:
package com.some.package.ui.login;
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.some.package.R
import com.some.package.device.network.NetworkComponent
import com.some.package.ui.terms.TermsActivity
class LoginActivity : AppCompatActivity(),
LoginViewModel.Navigator {
val loginViewModel by lazy { LoginViewModel(this, NetworkComponent()) }
在 Android Studio 中一切都解决得很好。然而,当我尝试构建项目时,遇到构建错误:
e: /Users/android/some-app-android/app/src/main/kotlin/com/some/package/ui/login/LoginActivity.kt: (7, 27): Unresolved reference: network
e: /Users/android/some-app-android/app/src/main/kotlin/com/some/package/ui/login/LoginActivity.kt: (13, 55): Unresolved reference: NetworkComponent
[KOTLIN] deleting /Users/android/some-app-android/app/build/tmp/kotlin-classes/debug on error
[KOTLIN] deleting /Users/android/some-app-android/app/build/tmp/kotlin-classes/debug on error
:app:compileDebugKotlin FAILED
这是 NetworkComponent 的样子:
package com.some.package.device.network
import com.some.package.domain.datasources.CodeValidator
import retrofit2.Retrofit
class NetworkComponent : CodeValidator {
val codeValidator: CodeValidatorApi
init {
val retrofit = Retrofit.Builder()
.baseUrl("www.test.com")
.build()
codeValidator = retrofit.create(CodeValidatorApi::class.java)
}
override fun validate(code: String) = codeValidator.validate(code)
}
以下是构建文件:
顶部:
buildscript {
ext.kotlin_version = '1.1.51'
ext.android_tools = '3.0.0'
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:$android_tools"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
应用程序:
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
//apply plugin: 'io.fabric'
// Manifest version information
def versionMajor = 0
def versionMinor = 0
def versionPatch = 1
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.some.package"
minSdkVersion 21
targetSdkVersion 27
versionCode versionMajor * 10000 + versionMinor * 100 + versionPatch
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
// applicationIdSuffix '.debug'
resValue "string", "application_name", "TEST APP Debug"
debuggable true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
resValue "string", "application_name", "TEST APP"
}
}
dataBinding {
enabled = true
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
debug.java.srcDirs += 'src/debug/kotlin'
}
packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/services/javax.annotation.processing.Processor' // butterknife
}
}
repositories {
maven { url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo' }
}
dependencies {
implementation project(':device')
implementation project(':data')
implementation project(':domain')
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:support-v4:27.0.0'
implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'com.android.support:design:27.0.0'
implementation 'com.android.support:support-annotations:27.0.0'
kapt "com.android.databinding:compiler:$android_tools"
// Logging
implementation 'com.jakewharton.timber:timber:4.5.1'
// Unit tests
testImplementation 'junit:junit:4.12'
// testImplementation 'org.robolectric:robolectric:3.0'
testImplementation 'org.mockito:mockito-core:2.11.0'
// testImplementation 'joda-time:joda-time:2.9.4'
// debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.1'
// releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
// testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
implementation 'io.reactivex.rxjava2:rxkotlin:2.1.0'
debugImplementation 'com.facebook.stetho:stetho:1.4.1'
debugImplementation 'com.uphyca:stetho_realm:2.0.0'
// implementation('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
// transitive = true;
// }
}
设备:
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':domain')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'io.reactivex.rxjava2:rxkotlin:2.1.0'
implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
testImplementation 'junit:junit:4.12'
}
我不太确定这是怎么发生的。我尝试将 gradle 插件降级到 2.3.3,但没有成功。
有什么想法吗?
原来我在我的设备模块构建文件中遗漏了这个关键行:
apply plugin: 'kotlin-android'
感谢这个答案提供的线索