无法使用Android Gradle插件3.0.1解析assembleAndroidTest任务的依赖关系 [英] Unable to resolve dependencies for assembleAndroidTest task with Android Gradle plugin 3.0.1

查看:418
本文介绍了无法使用Android Gradle插件3.0.1解析assembleAndroidTest任务的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新我们的项目,以使用Gradle 4.1和Android Gradle插件3.0.1。我已将我们的依赖配置更新到新配置并且项目成功编译。但是,在编译android测试时( assembleAndroidTest Gradle任务),有很多未解决的依赖关系(包括Kotlin标准库的顶级函数)。我怀疑Proguard可能会导致这种情况(虽然它没有更新Gradle),但是即使添加明确的规则来保留符号/类也没有帮助。我们使用Kotlin 1.2.10和Kotlin-Kapt插件。



感谢任何帮助。 我不使用ProGuard进行调试,但以下答案似乎很有用。
我会在迁移指南后再修改一次Gradle配置,以及首先清理和无效缓存。



Proguard



切记此问题 回答了关于如何将Kotlin与Proguard一起使用。



build.gradle中禁用这些指令 file放弃Proguard。

  minifyEnabled false 
shrinkResources false

为Kotlin配置Proguard。


你不需要做任何特别的事情。 Kotlin与ProGuard一起打包出售
。但使用ProGuard处理
应用程序时可能会遇到一些奇怪的错误。在这种情况下,只需添加:

  -dontwarn kotlin。** 


您也可以添加:

  -keep class kotlin。** {*; } 
-keep class kotlin.Metadata {*; }
-dontwarn kotlin。**
-keepclassmembers class ** $ WhenMappings {
< fields> ;;
}
-keepclassmembers类kotlin.Metadata {
public< methods> ;;
}
-assumenosideeffects class kotlin.jvm.internal.Intrinsics {
static void checkParameterIsNotNull(java.lang.Object,java.lang.String);

$ / code>

检查相关问题以启用Proguard进行测试或不测试:



proguard gradle debug build但不是测试



指定要在检测测试中使用的Proguard文件。
$ b


runProguard老了。它被minifyEnabled替换



使用minifyEnabled(以及Gradle新版本中的其他更改),
可能会遇到Proguard配置适用于$ b的问题$ b debug apk但不适用于仪器测试。为
instrumentation测试创建的apk将使用自己的proguard文件,因此更改您的
现有proguard文件将不起作用。



在这种情况下,您需要指定在
检测测试中使用的proguard文件。它可以是相当宽容的,因为它不是
影响您的调试和发布版本。

  // android内部块
debug {
shrinkResources true //删除未使用的图形等
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
testProguardFile('test-proguard-rules.pro')
}


启用了proguard的Android单元测试



添加自定义proguard规则文件


/project/app/proguard-test-rules.pro

 #应用于您的测试apk /代码的Proguard规则。 
-ignorewarnings

-keepattributes *注释*

-dontnote junit.framework。**
-dontnote junit.runner。**

-dontwarn android.test。**
-dontwarn android.support.test。**
-dontwarn org.junit。**
-dontwarn org.hamcrest。* *
-dontwarn com.squareup.javawriter.JavaWriter
#如果您使用Mockito
#-dontwarn org.mockito。**
,请取消注释。将以下内容添加到您的版本中。 gradle为您的应用程序。测试时使用proguard文件。

/project/app/build.gradle

android {
debug {
minifyEnabled true
testProguardFile'proguard-test-rules。 pro'
}
}


添加一个buidType进行测试


我已经通过增加一个dev$来解决这个问题b $ b buildType其中启用了proguard,但将其配置为将所有代码
保留在我自己的包中,并且仅从测试中使用发生
的几个特定库类。我还禁用了dev
buildType中的模糊处理,以便它可以从IDE中调试。



对于调试和发布版本,我使用真实proguard设置
包括模糊处理和优化。

使用单独的测试模块


独立的测试模块现在可以识别变体。这意味着指定targetVariant的
不再是必需的。



测试模块中的每个变体都会尝试在目标项目中测试匹配的
变体。默认情况下,测试模块仅包含一个
调试版本,但您可以创建新的版本类型,并且为
添加新版本以创建新的版本以匹配测试的应用项目。为每个变体创建connectedCheck
任务。为了使测试模块仅测试一种不同的构建类型,而不是
调试,使用VariantFilter禁用测试
项目中的调试变体,如下所示:

  android {
variantFilter {variant - >
if(variant.buildType.name.equals('debug')){
variant.setIgnore(true);





如果你想要一个测试模块只定位某些特定的风味或构建
类型的应用程序,您可以使用 matchingFallbacks 属性将目标
仅限于您想要测试的变体。这也可以防止测试模块
不必为自己配置这些变体。







Gradle



修改您的Gradle配置。为了构建用Kotlin编写的Android项目


  • 设置 kotlin-android gradle插件并将其应用到您的项目中。

  • 添加 kotlin-stdlib 依赖关系。
    / b>


    这些动作也可以通过调用动作在IntelliJ IDEA /
    AS中自动执行:


    工具| Kotlin |在Project中配置Kotlin







    kotlin-android



      buildscript {
    ext.kotlin_version ='1.2.10'

    ...

    依赖关系{
    classpathorg.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version
    }
    }
    apply plugin:'com.android.application'
    apply plugin:'kotlin-android'






    < h2> kotlin-stdlib

    不要忘记配置标准库依赖项

     存储库{
    mavenCentral ()
    }

    依赖项{
    compileorg.jetbrains.kotlin:kotlin-stdlib
    }

    使用迁移
    指南



    注意: , 目前 仍然可用



    然而,将被移除下一个主要版本是
    的Android插件。







    提供手动版


    从Kotlin 1.1.2开始,组
    org .jetbrains.kotlin 默认使用应用插件中采用
    的版本进行解析。



    您可以使用完全依赖性
    符号手动提供版本,如下所示:

      compileorg.jetbrains.kotlin:kotlin-stdlib:$ kotlin_version







    解决策略 您也可以强制解决策略

      configurations.all {
    resolutionStrategy {
    forceorg.jetbrains.kotlin:kotlin-stdlib:$ kotlin_version
    }
    }

    当您使用 Android Gradle插件3.0.1

      / /相反,因为新的构建模型会延迟依赖关系解析,所以您
    //应该使用Variant API查询和修改分辨率策略:
    android {
    applicationVariants.all {variant - >
    variant.getCompileConfiguration()。resolutionStrategy {
    ...
    }
    variant.runtimeConfiguration.resolutionStrategy {
    ...
    }
    resolution.getAnnotationProcessorConfiguration()。resolutionStrategy {
    ...
    }
    }
    }

    使用Variant API从测试配置中排除应用程序依赖关系: b
    $ b


    在以前版本的Android插件中,您可以使用排除
    关键字从您的测试中排除您的应用的某些
    传递依赖项。但是,对于新的依赖配置,您必须使用Variant API在执行时执行


      android。 testVariants.all {variant  - > 
    variant.getCompileConfiguration()。exclude group:'com.jakewharton.threetenabp',module:'threetenabp'
    variant.getRuntimeConfiguration()。exclude group:'com.jakewharton.threetenabp',module:' threetenabp'
    }







    Kotlin标准库的扩展版
    $ b


    如果您定位JDK 7或JDK 8 ,则可以使用
    的扩展版本Kotlin标准库,其中包含针对在新JDK版本中添加的API的附加扩展
    函数。而不是
    kotlin-stdlib ,请使用以下其中一个依赖项:

     <$ 
    compileorg.jetbrains.kotlin:kotlin-stdlib-jdk8
    p $ p>

    在Kotlin 1.1.x 中,使用 kotlin-stdlib-jre7 kotlin-stdlib-jre8







    Kotlin反射




    如果您的项目使用 Kotlin
    反射

    或测试设施,您需要添加相应的依赖关系


      compileorg.jetbrains.kotlin:kotlin-reflect
    testCompileorg.jetbrains。 kotlin:kotlin-test
    testCompileorg.jetbrains.kotlin:kotlin-test-junit







    Kapt



    请参阅 Kotlin注释处理工具 kapt )。



    应用 kotlin-kapt Gradle插件:

      apply plugin:'kotlin-kapt'


    I'm updating our project to use Gradle 4.1 and Android Gradle plugin 3.0.1. I have updated our dependency configuration to the new configuration and the project successfully compiles. However, there are lots of unresolved dependencies (incl. Kotlin standard library's top-level functions) when compiling android tests (assembleAndroidTest Gradle task). I was suspecting that Proguard might cause this (although it didn't before updating Gradle), but even adding explicit rules to keep symbols/classes doesn't help. We use Kotlin 1.2.10 and Kotlin-Kapt plugin.

    I appreciate any help.

    解决方案

    I don't use ProGuard for debug but the following answers seem useful. I would revise your Gradle configuration another time following the migration guide, and first of all clean and invalidate caches.

    Proguard

    Chech this question and answers about how to use Kotlin with Proguard.

    Disable these directives in your build.gradle file to discard Proguard.

    minifyEnabled false
    shrinkResources false
    

    Configure Proguard for Kotlin.

    You don't need to do anything special. Kotlin works with ProGuard out of the box. But you may face some strange errors when processing your application with ProGuard. In this case just add:

    -dontwarn kotlin.**
    

    You also can add:

    -keep class kotlin.** { *; }
    -keep class kotlin.Metadata { *; }
    -dontwarn kotlin.**
    -keepclassmembers class **$WhenMappings {
        <fields>;
    }
    -keepclassmembers class kotlin.Metadata {
        public <methods>;
    }
    -assumenosideeffects class kotlin.jvm.internal.Intrinsics {
        static void checkParameterIsNotNull(java.lang.Object, java.lang.String);
    }
    

    Check this related questions to either enable Proguard for tests or not:

    proguard gradle debug build but not the tests

    Specify the Proguard file to use on the instrumentation tests.

    runProguard is old. It was replaced with minifyEnabled

    With minifyEnabled (and other changes in new versions of Gradle) you will may encounter issues where the Proguard config works for your debug apk but not for the instrumentation tests. The apk created for instrumentation tests will use its own proguard file, so changing your existing proguard file will have no effect.

    In this case, you need to specify the proguard file to use on the instrumentation tests. It can be quite permissive because it's not affecting your debug and release builds at all.

        // inside android block
        debug {
            shrinkResources true  // removes unused graphics etc
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            testProguardFile('test-proguard-rules.pro')
        }
    

    Android Unit Tests with proguard enabled

    Add a custom proguard rules file

    /project/app/proguard-test-rules.pro

    # Proguard rules that are applied to your test apk/code.
    -ignorewarnings
    
    -keepattributes *Annotation*
    
    -dontnote junit.framework.**
    -dontnote junit.runner.**
    
    -dontwarn android.test.**
    -dontwarn android.support.test.**
    -dontwarn org.junit.**
    -dontwarn org.hamcrest.**
    -dontwarn com.squareup.javawriter.JavaWriter
    # Uncomment this if you use Mockito
    #-dontwarn org.mockito.**
    The add the following to your build.gradle for your app. To use the proguard file when testing.
    
    /project/app/build.gradle
    
    android {
        debug {
            minifyEnabled true
            testProguardFile 'proguard-test-rules.pro'
        }
    }
    

    Add a buidType for testing

    I've solved this problem in my build by having an additional "dev" buildType where I enable proguard, but configure it to keep all code in my own package, and a few specific library classes that happen to be used from tests only. I also disable obfuscation in the dev buildType so that it can be debugged from an IDE.

    For debug and release builds I use my "real" proguard settings including obfuscation and optimizations.

    Use separate test modules

    Separate test modules are now variant-aware. This means that specifying targetVariant is no longer necessary.

    Each variant in the test module will attempt to test a matching variant in the target project. By default, test modules contain only a debug variant, but you can create new build types and new flavors to create new variants to match the tested app project. A connectedCheck task is created for each variant.

    To make the test module test a different build type only, and not the debug one, use VariantFilter to disable the debug variant in the test project, as shown below:

    android {
        variantFilter { variant ->
            if (variant.buildType.name.equals('debug')) {
                variant.setIgnore(true);
            }
        }
    }
    

    If you want a test module to target only certain flavors or build types of an app, you can use the matchingFallbacks property to target only the variants you want to test. This also prevents the test module from having to configure those variants for itself.


    Gradle

    Revise your Gradle configuration. In order to to build an Android project written in Kotlin:

    • Set up the kotlin-android gradle plugin and apply it to your project.
    • Add kotlin-stdlib dependencies.

    Those actions may also be performed automatically in IntelliJ IDEA / AS by invoking the action:

    Tools | Kotlin | Configure Kotlin in Project


    kotlin-android

    buildscript {
        ext.kotlin_version = '1.2.10'
    
        ...
    
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    


    kotlin-stdlib

    Don't forget to configure the standard library dependency:

    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib"
    }
    

    Revise your dependencies configuration using the migration guide.

    Note: compile, provided, and apk are currently still available.

    However, they will be removed in the next major release of the Android plugin.


    Provide version manually

    Starting with Kotlin 1.1.2, the dependencies with group org.jetbrains.kotlin are by default resolved with the version taken from the applied plugin.

    You can provide the version manually using the full dependency notation like this:

    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    


    Resolution strategy

    You also can force the resolution strategy:

    configurations.all {
        resolutionStrategy {
            force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        }
    }
    

    As you are using Android Gradle plugin 3.0.1:

    // Instead, because the new build model delays dependency resolution, you
    // should query and modify the resolution strategy using the Variant API:
    android {
        applicationVariants.all { variant ->
            variant.getCompileConfiguration().resolutionStrategy {
                ...
            }
            variant.runtimeConfiguration.resolutionStrategy {
                ...
            }
            variant.getAnnotationProcessorConfiguration().resolutionStrategy {
                ...
            }
        }
    }
    

    Exclude app dependencies from test configurations using the Variant API:

    On previous versions of the Android plugin, you could exclude certain transitive dependencies of your app from your tests using the exclude keyword. However, with the new dependency configurations, you must do it at execution time using the Variant API:

    android.testVariants.all { variant ->
        variant.getCompileConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
        variant.getRuntimeConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
    }
    


    Extended versions of the Kotlin standard library

    If you're targeting JDK 7 or JDK 8, you can use extended versions of the Kotlin standard library which contain additional extension functions for APIs added in new JDK versions. Instead of kotlin-stdlib, use one of the following dependencies:

    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    

    In Kotlin 1.1.x, use kotlin-stdlib-jre7 and kotlin-stdlib-jre8 instead.


    Kotlin reflection

    If your project uses Kotlin reflection or testing facilities, you need to add the corresponding dependencies as well:

    compile "org.jetbrains.kotlin:kotlin-reflect"
    testCompile "org.jetbrains.kotlin:kotlin-test"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit"
    


    Kapt

    See the description of Kotlin annotation processing tool (kapt).

    Apply the kotlin-kapt Gradle plugin:

    apply plugin: 'kotlin-kapt'
    

    这篇关于无法使用Android Gradle插件3.0.1解析assembleAndroidTest任务的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆