【问题标题】:Error using Proguard使用 Proguard 时出错
【发布时间】:2017-02-07 06:44:52
【问题描述】:

我正在尝试在我的应用程序中实现 proguard,但我无法做到。我已经启用了 minifyEnabled 和 shrinkResources。这是我的 build.gradle 文件:

buildscript {
repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'android-apt'

repositories {
maven { url 'https://maven.fabric.io/public' }
}

apply plugin: 'com.neenbedankt.android-apt'

android {
signingConfigs {
    testSigned {
        keyAlias 'test'
        keyPassword 'testing123'
        storePassword 'testing123'
        //storeFile file('C:/Users/Motobeans/Desktop/testSignedArchitecture.jks')
    }
}
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
    applicationId "in.mylo.pregnancy.baby.app"
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    versionCode rootProject.ext.versionCode
    versionName rootProject.ext.versionName
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    debug {
        shrinkResources false // This must be first
        minifyEnabled false   // This must be after shrinkResources
        multiDexEnabled true
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
    release {
        shrinkResources false
        minifyEnabled false
        multiDexEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
// Multidex support
compile 'com.android.support:multidex:1.0.1'
// Android Support Libraries
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:support-v4:$rootProject.supportLibraryVersion"
compile "com.android.support:design:$rootProject.supportLibraryVersion"
compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:palette-v7:$rootProject.supportLibraryVersion"
/*
 * Guava is a set of core libraries that includes new collection types (such as multimap and multiset),
 * immutable collections, a graph library, functional types, an in-memory cache, and APIs/utilities for concurrency,
 * I/O, hashing, primitives, reflection, string processing, and much more!
 */
compile "com.google.guava:guava:$rootProject.guavaVersion"
// BugFender (logging to server)
compile 'com.bugfender.sdk:android:0.6.2'
// Timber (Logging library)
compile 'com.jakewharton.timber:timber:4.3.1'
// Dagger Dependency
compile 'com.google.dagger:dagger:2.7'
apt 'com.google.dagger:dagger-compiler:2.7'
compile 'javax.annotation:javax.annotation-api:1.2'
// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
//RxAndroid
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
// Logging Interceptor
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
// Crash Handler : Show custom activity
compile 'cat.ereza:customactivityoncrash:1.5.0'
// EasyImage - Image picker from Gallery or Camera
compile 'com.github.jkwiecien:EasyImage:1.3.1'
//Marshmallow Permission Library
compile('com.github.afollestad:assent:0.2.0') {
    transitive = true
}
// jakewharton butterknief library
compile 'com.jakewharton:butterknife:8.1.0'
apt 'com.jakewharton:butterknife-compiler:8.1.0'

// Jakewharton byte library
compile 'com.jakewharton.byteunits:byteunits:0.9.1'
// Glide - Image Loading Library
compile 'com.github.bumptech.glide:glide:3.7.0'

// Leak canary (A memory leak detection library for Android and Java)
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'

// EventBus
compile 'org.greenrobot:eventbus:3.0.0'

// Marshmallow Permission Library
compile('com.github.afollestad:assent:0.2.0') {
    transitive = true
}

// Ripple Layout Activity Transition
compile 'com.liuguangqiang.ripplelayout:library:0.1.0'

// Fabric Crashlytics
compile('com.crashlytics.sdk.android:crashlytics:2.6.6@aar') {
    transitive = true;
}

// Circular ImageView
compile 'de.hdodenhof:circleimageview:1.2.1'

// View pager
compile 'com.ogaclejapan.smarttablayout:library:1.6.1@aar'

// Crescento Curve container
compile 'com.github.developer-shivam:crescento:1.0.1'

// Shape Image View
compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
//compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'

// Fonts
compile 'me.anwarshahriar:calligrapher:1.0'

//Google Sign In
/*  compile 'com.google.android.gms:play-services-auth:9.8.0'
compile 'com.google.android.gms:play-services:9.8.0'*/
/*
    compile 'com.google.android.gms:play-services-auth:10.0.1'
*/

// Branch.io
compile 'io.branch.sdk.android:library:2.+'

// Branch + Segment Integration library
compile 'com.weiwangcn.betterspinner:library:1.1.0'

//segment and clevertap
compile 'com.segment.analytics.android:analytics:4.+'
compile 'com.clevertap.android:clevertap-segment-android:+'

//segment and branch
compile 'io.branch.segment.analytics.android.integrations:branch:+'

// Loader View
compile 'com.elyeproj.libraries:loaderviewlibrary:1.2.2'

// Animation
compile 'com.eftimoff:androidplayer:1.0.3@aar'
compile 'com.nineoldandroids:library:2.4.0'

compile 'com.ogaclejapan.smarttablayout:library:1.6.1@aar'

//Custom Chrome tab
compile 'com.android.support:customtabs:25.0.1'

// Epoxy Adapter
compile 'com.airbnb.android:epoxy:1.5.0'

//mixpanel
compile "com.mixpanel.android:mixpanel-android:4.+"

// firebase//Add this line
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-messaging:9.6.1'
compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.thefinestartist:ytpa:1.2.1'
compile 'com.caverock:androidsvg:1.2.1'

// Flow layout
compile 'org.apmem.tools:layouts:1.10@aar'
// Text Drawable
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
compile 'pl.bclogic:pulsator4droid:1.0.3'

//Tune
compile 'com.tune:tune-marketing-console-sdk:4.9.0'

// Pug notification
compile 'com.github.halysongoncalves:pugnotification:1.8.1'

compile 'com.facebook.android:facebook-android-sdk:4.+'
}
apply plugin: 'com.google.gms.google-services'

这是我得到的错误图像:

错误代码太大,所以我决定发布图像而不是代码。任何帮助表示赞赏,在此先感谢

【问题讨论】:

  • @IntelliJAmiya 谢谢,我试试
  • 通常需要在proguard文件中定义异常。我认为 fabric 和其他依赖项应该在他们的文档中列出要添加的内容。
  • @AviParshan 是的,我还没做过,我试试,谢谢
  • @BhaveshMisri 这也可能是你的多索引的问题

标签: android android-gradle-plugin build.gradle


【解决方案1】:

首先纠正你的 debugrelease 部分。

 buildTypes
 {
    debug {
     shrinkResources true // This must be first 
     minifyEnabled true   // This must be after shrinkResources 
      proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
    release {
     shrinkResources true // This must be first 
     minifyEnabled true   // This must be after shrinkResources 
      proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }

 }

欲了解更多信息,Read Errors while building APK Android Studio

【讨论】:

  • 还是一样的错误,看看我的第一行错误,还是一样的
  • @BhaveshMisri 好的。请分享您的build.gradle
  • 我已经对我的问题进行了修改,您现在可以查看。
【解决方案2】:

将每个依赖项的例外添加到 proguard-rules.pro 文件中。不过,某些依赖项不需要 proguard 规则。

例如,因为你使用 retrofit ,你需要添加这个:

# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

到你的 proguard 文件

【讨论】:

  • 我也在清单文件中配置了它,但它显示相同的错误。我想我必须在 pro guard 文件中为其他依赖项定义异常。
  • 您是否总是将 minifyEnable 和 shrinkresources 设置为 true?
  • 不,当我将它们设置为 false 时,应用程序运行良好。但是,如果我将其中任何一个设置为 true,则会显示上述错误。
  • 所以你绝对是对的。 shirnkResources 是旧方法,你可以使用 minifyResources。你明白我的回答,还是我应该详细说明?
  • 让我试试,之后我会回复你的。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2018-12-17
  • 2014-07-28
相关资源
最近更新 更多