Android Gradle-无法使用模块包含的lib的查找类

时间:2019-01-29 18:57:58

标签: android gradle

该项目由3个模块(app,datecs和volley)组成。 datecs模块是库libs/com.datecs.api.jar之间的接口。问题是库类/包在应用程序模块中不可见。

以下是gradle文件:

project.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

app.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '28.0.3'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

buildscript {
    repositories {
        jcenter()
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

dependencies {
    implementation files('libs/gson-2.3.1.jar')
    implementation project(':volley')
    implementation 'com.android.support:support-v4:21.0.3'
    implementation 'com.android.support:appcompat-v7:21.0.3'
    implementation project(':datecs')
}

datecs.gradle

apply plugin: 'com.android.library'

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

android {
    compileSdkVersion 21
    buildToolsVersion '28.0.3'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:support-v4:21.0.3'
    implementation 'com.android.support:appcompat-v7:21.0.3'
    testImplementation 'junit:junit:4.12'
    implementation files('libs/com.datecs.api.jar')
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

如何在应用程序模块中访问库类?

1 个答案:

答案 0 :(得分:1)

由于我无权访问您的特定库(datecs),因此我测试了一个应用程序模块和一个库模块,其中该应用程序模块需要从库模块中“导入” 'com.android.support:appcompat-v7:28.0.0'依赖项。

使用api而不是implementation帮了我大忙。

应用模块的build.gradle中的依赖项关闭:

dependencies {
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation project(':mytestlibrary')
}

库模块的build.gradle中的依赖项关闭:

dependencies {
    api 'com.android.support:appcompat-v7:28.0.0'
}

另请参阅api上关于Dependency Configurations的文档:

  

Gradle将依赖项添加到编译类路径并生成输出。当模块包含api依赖项时,它会让Gradle知道该模块希望将该依赖项可传递地导出到其他模块,以便它们在运行时和编译时都可用。