gpt4 book ai didi

android - 无法使用 Android Gradle 插件 3.0.1 解决 assembleAndroidTest 任务的依赖关系

转载 作者:行者123 更新时间:2023-12-02 13:00:24 26 4
gpt4 key购买 nike

我正在更新我们的项目以使用 Gradle 4.1 和 Android Gradle 插件 3.0.1。我已将我们的依赖配置更新为 new configuration项目成功编译。但是,在编译 android 测试(assembleAndroidTest Gradle 任务)时,有很多 Unresolved 依赖项(包括 Kotlin 标准库的顶级函数)。我怀疑 Proguard 可能会导致这种情况(尽管在更新 Gradle 之前不会),但即使添加明确的规则来保留符号/类也无济于事。我们使用 Kotlin 1.2.10 和 Kotlin-Kapt 插件。

感谢任何帮助。

最佳答案

我不使用 ProGuard 进行调试,但以下答案似乎很有用。我会按照迁移指南再次修改您的 Gradle 配置,首先是清理和使缓存无效。

混淆器

检查这个questionanswers关于如何将 Kotlin 与 Proguard 结合使用。

在您的 build.gradle 文件中禁用这些指令以丢弃 Proguard。

minifyEnabled false
shrinkResources false

为 Kotlin 配置 Proguard。

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.**

您还可以添加:

-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);
}

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

proguard gradle debug build but not the tests

指定要在仪器测试中使用的 Proguard 文件。

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

添加自定义混淆器规则文件

/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'
}
}

添加用于测试的 buidType

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.

使用单独的测试模块

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

修改您的 Gradle 配置。为了to build an Android project written in Kotlin :

  • 设置 kotlin-android gradle 插件并将其应用到您的项目。
  • 添加 kotlin-stdlib 依赖项。

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

Tools | Kotlin | Configure Kotlin in Project


Kotlin 安卓

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 标准库

不要忘记配置 standard library dependency :

repositories {
mavenCentral()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib"
}

使用 migrationguide 修改您的依赖配置.

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

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


手动提供版本

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"

解析策略

您也可以 force the resolution strategy :

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

当你使用 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 {
...
}
}
}

使用 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'
}

Kotlin 标准库的扩展版本

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 反射

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"

卡普特

参见 Kotlin annotation processing tool 的描述(kapt).

应用kotlin-kapt Gradle插件:

apply plugin: 'kotlin-kapt'

关于android - 无法使用 Android Gradle 插件 3.0.1 解决 assembleAndroidTest 任务的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48170785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com