Gradle没有选择在lib文件夹

时间:2017-06-07 14:54:37

标签: android gradle android-gradle

我需要为apk执行和单元测试用例执行加载不同的运行时库。

我熟悉compile和testCompile范围,但问题是我不想在运行单元测试时加载特定的编译范围库。

所以我想出了一些像下面这样的build.gradle。我可以在libs文件夹下复制所需的jar / aars,但不考虑类路径。

这个脚本有什么问题吗?

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "example.com.myapplication"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

task getDynamicJars(type: Copy) {
        delete fileTree("$projectDir/libs/")
        if(project.gradle.startParameter.taskNames.contains("test")){
            println("Loading Unit test dependencies");
            from "$rootDir/runtimeLibs/test/"
            into "$projectDir/libs/"
            include '**/*.*'

        }else{
            println("Loading Execute dependencies from:"+"$projectDir/runtimeLibs/execution/"+" To:"+"$projectDir/libs/");
            from "$rootDir/runtimeLibs/execution/"
            into "$projectDir/libs/"
            include '**/*.*'
        }

}

dependencies {
    println("Loading dependencies");
    compile fileTree(dir: "libs", include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.0'
    testCompile 'junit:junit:4.12'
}

project.afterEvaluate {
    preBuild.dependsOn getDynamicJars
}

1 个答案:

答案 0 :(得分:0)

你喜欢这个解决方案:

def libsFolder
// ..
  buildTypes {
    release {
      // ..
      libsFolder = "${releaseLibsDir}"
    }

    debug {
      // ..
      libsFolder = "${debugLibsDir}"
    }
  }
// ..
dependencies {
  compile fileTree(dir: "${libsFolder}", include: ['*.jar'])
}