连接moshi到Android Studio(Kotlin)

我在Android Studio中使用Kotlin。 我无法编译连接moshi库的项目。 https://github.com/square/moshi

告诉我,我做错了什么? 我可以在控制台中看到:

:app:packageInstantRunResourcesDebug UP-TO-DATE :app:checkManifestChangesDebug :app:transformClassesWithAndroidGradleClassShrinkerForDebug com/squareup/moshi/JsonReader references unknown class: javax/annotation/Nullable com/squareup/moshi/CollectionJsonAdapter$1 references unknown class: javax/annotation/Nullable com/squareup/moshi/JsonAdapter$5 references unknown class: javax/annotation/Nullable com/squareup/moshi/package-info references unknown class: javax/annotation/ParametersAreNonnullByDefault com/squareup/moshi/ArrayJsonAdapter$1 references unknown class: javax/annotation/Nullable com/squareup/moshi/AdapterMethodsFactory$1 references unknown class: javax/annotation/Nullable okio/package-info references unknown class: javax/annotation/ParametersAreNonnullByDefault com/squareup/moshi/Types references unknown class: javax/annotation/Nullable com/squareup/moshi/JsonAdapter references unknown class: javax/annotation/Nullable okio/ByteString references unknown class: javax/annotation/Nullable 

顶级build.gradle

 buildscript { ext.kotlin_version = '1.2.10' repositories { jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() google() } } task clean(type: Delete) { delete rootProject.buildDir } 

应用程序\的build.gradle

 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { applicationId "com.lfom.modbuster" 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' } debug { debuggable true minifyEnabled true } } productFlavors { } } dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testImplementation 'junit:junit:4.12' implementation 'com.android.support:appcompat-v7:26.+' implementation 'com.android.support:design:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-reflect" implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.google.android.gms:play-services-vision:11.0.4' implementation 'com.squareup.moshi:moshi:1.5.0' implementation 'com.squareup.moshi:moshi-kotlin:1.5.0' implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' } repositories { mavenCentral() maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" } } 

https://github.com/LevWi/modbuster-blt/blob/Signals/app/build.gradle


proguard-rules.pro

 -dontwarn okio.** -dontwarn javax.annotation.** -keepclasseswithmembers class * { @com.squareup.moshi.* ; } -keep @com.squareup.moshi.JsonQualifier interface * -keepclassmembers class kotlin.Metadata { public ; } 

首先在调试minifyEnabled false设置minifyEnabled true ,但在发布时将minifyEnabled true设置为minifyEnabled false 。 这没有意义。 要么都设置为true要么都设为false

第二件事是。 你知道minifyEnabled做什么的吗? 它在您的项目中启用了ProGuard。 在这里看到更多的细节。

然而。 你有两个选择来解决这个问题。 将minifyEnabled设置为false或以正确的方式应用ProGuard。

开始加入

 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 

到你的build.gradle

通过在您的proguard-rules.pro应用这些规则结束。