我不使用 ProGuard 进行调试,但以下答案似乎很有用。
我会按照迁移指南再次修改你的 Gradle 配置,首先是清理和无效缓存。
Proguard
查看question 和answers,了解如何将 Kotlin 与 Proguard 结合使用。
在您的 build.gradle 文件中禁用这些指令以丢弃 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 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 是旧的。它被替换为 minifyEnabled
使用 minifyEnabled(以及新版本 Gradle 中的其他更改),您
可能会遇到 Proguard 配置适用于您的问题
调试 apk 但不适用于仪器测试。创建的apk
仪器测试将使用它自己的 proguard 文件,因此更改您的
现有的 proguard 文件将无效。
在这种情况下,您需要指定要在
仪表测试。它可以是相当宽松的,因为它不是
完全影响您的调试和发布版本。
// 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
添加自定义的 proguard 规则文件
/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
我通过增加一个“开发者”在我的构建中解决了这个问题
我启用 proguard 的 buildType,但将其配置为保留所有代码
在我自己的包中,以及碰巧发生的一些特定库类
只能从测试中使用。我还在开发中禁用了混淆
buildType 以便可以从 IDE 进行调试。
对于调试和发布版本,我使用“真正的”proguard 设置
包括混淆和优化。
使用单独的测试模块
Separate test modules 现在可以识别变体。这意味着
不再需要指定 targetVariant。
测试模块中的每个变体都将尝试测试匹配的
目标项目中的变体。默认情况下,测试模块只包含一个
调试变体,但您可以创建新的构建类型和新风格
创建新的变体以匹配测试的应用程序项目。连接检查
为每个变体创建任务。
使测试模块仅测试不同的构建类型,而不是
调试一,使用VariantFilter在测试中禁用调试变体
项目,如下图:
android {
variantFilter { variant ->
if (variant.buildType.name.equals('debug')) {
variant.setIgnore(true);
}
}
}
如果您希望测试模块仅针对特定风格或构建
应用程序的类型,您可以使用matchingFallbacks 属性来定位
只有您要测试的变体。这也防止了测试模块
不必为自己配置这些变体。
分级
修改您的 Gradle 配置。为了to build an Android project written in Kotlin:
- 设置
kotlin-android gradle 插件并将其应用到您的项目中。
- 添加
kotlin-stdlib 依赖项。
这些动作也可以在 IntelliJ IDEA 中自动执行 /
AS 通过调用操作:
工具 |科特林 |在项目中配置 Kotlin
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
别忘了配置standard library dependency:
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib"
}
使用migration
guide 修改您的依赖配置。
注意:compile、provided 和 apk 目前仍然可用。
但是,它们将在下一个主要版本中删除
Android 插件。
手动提供版本
从 Kotlin 1.1.2 开始,与 group 的依赖关系
org.jetbrains.kotlin 默认使用版本解析
从应用的插件。
您可以使用完全依赖项手动提供版本
像这样的符号:
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 从测试配置中排除应用依赖项:
在以前版本的 Android 插件中,您可以排除某些
使用 exclude 从您的测试中传递您的应用程序依赖项
关键词。但是,使用新的依赖项配置,您必须执行
在执行时使用 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 标准库的扩展版本
如果您面向 JDK 7 或 JDK 8,则可以使用
包含附加扩展的 Kotlin 标准库
新 JDK 版本中添加的 API 函数。代替
kotlin-stdlib,使用以下依赖项之一:
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
在 Kotlin 1.1.x 中,使用 kotlin-stdlib-jre7 和 kotlin-stdlib-jre8
而是。
Kotlin 反射
如果您的项目使用Kotlin
reflection
或者测试设施,需要添加相应的依赖
还有:
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'