由于某些.jar依赖性,无法使用ProGuard编译应用的发行版本

时间:2018-07-11 10:34:53

标签: android android-gradle dependencies proguard zebra-printers

我最近将Zebra Scanner SDK添加到了我的Android应用中(通过ZSDK_ANDROID_API.jar依赖项)。进行调试编译(不使用ProGuard)可以正常工作。但是,当我使用ProGuard进行发行版编译时,出现以下错误:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesAndResourcesWithProguardForProductionRelease'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
    at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Job failed, see logs for details
    at com.android.build.gradle.internal.transforms.ProGuardTransform.transform(ProGuardTransform.java:196)
    at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:221)
    at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:217)
    at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
    at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:212)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
    ... 32 more
Caused by: java.io.IOException: Please correct the above warnings first.
    at proguard.Initializer.execute(Initializer.java:473)
    at proguard.ProGuard.initialize(ProGuard.java:233)
    at proguard.ProGuard.execute(ProGuard.java:98)
    at com.android.build.gradle.internal.transforms.BaseProguardAction.runProguard(BaseProguardAction.java:61)
    at com.android.build.gradle.internal.transforms.ProGuardTransform.doMinification(ProGuardTransform.java:253)
    at com.android.build.gradle.internal.transforms.ProGuardTransform.access$000(ProGuardTransform.java:63)
    at com.android.build.gradle.internal.transforms.ProGuardTransform$1.run(ProGuardTransform.java:173)
    at com.android.builder.tasks.Job.runTask(Job.java:47)
    at com.android.build.gradle.tasks.SimpleWorkQueue$EmptyThreadContext.runTask(SimpleWorkQueue.java:41)
    at com.android.builder.tasks.WorkQueue.run(WorkQueue.java:282)
    ... 1 more

我尝试将-keep class com.zebra.sdk.** { *; }添加到我的proguard-rules.pro文件中,但这也不起作用。

这是我的应用程序的Gradle文件:

def date = new Date()
def formattedDate = date.format('yyMMddHHmm')
def code = formattedDate.toInteger()

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'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "nl.company.app"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode code
        versionName "1.3.3"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
        }
    }

    // Specifies flavor dimensions.
    flavorDimensions "full"

    productFlavors {
        Development {
            dimension "full"
            manifestPlaceholders = [
                    appName     : "My App DEV",
                    appIcon     : "@mipmap/ic_launcher",
                    appRoundIcon: "@mipmap/ic_launcher_round"]
            applicationIdSuffix '.dev'
            versionNameSuffix "-dev"
            getProps('./config/development.props').each { p ->
                buildConfigField 'String', p.key, p.value
            }
            buildConfigField 'String', 'CLIENT', '"CLIENTNAME"'
        }
        Production {
            dimension "full"
            manifestPlaceholders = [
                    appName     : "My App",
                    appIcon     : "@mipmap/ic_launcher",
                    appRoundIcon: "@mipmap/ic_launcher_round"]
            applicationIdSuffix '.prod'
            versionNameSuffix "-prod"
            getProps('./config/production.props').each { p ->
                buildConfigField 'String', p.key, p.value
            }
            buildConfigField 'String', 'CLIENT', '"CLIENTNAME"'
            signingConfig signingConfigs.config
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
    }
}

// function to get the correct .props file
def getProps(path) {
    Properties props = new Properties()
    props.load(new FileInputStream(file(path)))
    return props
}

// restrict developer to generate release build using development URL
android.variantFilter { variant ->
    if (variant.buildType.name == 'release'
            && variant.getFlavors().get(0).name == 'development') {
        variant.setIgnore(true);
    }
}

repositories {
    flatDir {
        dirs 'libs'
    }
    maven { url "https://maven.google.com" }
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation('com.afollestad.material-dialogs:core:0.9.4.7') {
        exclude module: 'support-v13'
    }
    implementation('com.crashlytics.sdk.android:crashlytics:2.7.1@aar') {
        transitive = true;
    }
    implementation('com.crashlytics.sdk.android:answers:1.4.1@aar') {
        transitive = true;
    }
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.karumi:dexter:4.2.0'
    implementation 'com.github.bumptech.glide:glide:4.2.0'
    implementation 'org.greenrobot:eventbus:3.1.1'
    implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
    testImplementation 'junit:junit:4.12'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
    implementation files('libs/ZSDK_ANDROID_API.jar')
}

以及项目的Gradle文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'

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

2 个答案:

答案 0 :(得分:1)

您应该在Java编译器部分中解决警告。 对我来说,这有助于将其添加到我的ProGuard配置文件中:

-dontwarn org.apache.**
-dontwarn uk.co.westhawk.**
-dontwarn au.com.bytecode.**
-dontwarn com.zebra.**
-dontwarn com.fasterxml.**

-keep class org.apache.** { *; }
-keep interface org.apache.** { *; }
-keep class uk.co.westhawk.** { *; }
-keep interface uk.co.westhawk.** { *; }
-keep class au.com.bytecode.** { *; }
-keep interface au.com.bytecode.** { *; }
-keep class com.zebra.** { *; }
-keep interface com.zebra.** { *; }
-keep class com.fasterxml.** { *; }
-keep interface com.fasterxml.** { *; }

答案 1 :(得分:0)

我在Visual Studio 15.9.18中有一个Xamarin.Android应用程序构建。这是我最小的proguard.cfg文件,以使发布版本有效:

-dontwarn mono.com.symbol.emdk.**
-dontwarn com.symbol.emdk.**

-keep class mono.com.symbol.emdk.** { *; }
-keep interface mono.com.symbol.emdk.** { *; }
-keep class com.symbol.emdk.** { *; }
-keep interface com.symbol.emdk.** { *; }

注意事项1:如果仅将文本文件添加到VS中的项目中,它将创建一个开头为BOM (byte order mark)的文件。生成时,您会收到一条有用的描述性错误消息,如error MSB6006: "java.exe" exited with code 1。解决方案是确保您的proguard.cfg是不带BOM的纯文本文件。这就是为什么我们不能拥有美好的事物。

注意事项2:仅将proguard.txt文件添加到项目中是不够的。记住,您必须右键单击该文件,然后将“属性”->“构建操作”设置为ProguardConfiguration

注意事项3:我的特定应用仅使用Zebra EMDK的条形码扫描仪位。如果您正在使用其他东西,请使用YMMV。

注意事项4:请记住,在Xamarin + VS中,Proguard使用的是obj\Release\proguard\proguard_xamarin.cfg中的proguard.cfg plus 默认配置。有关详细信息,请参见the official docco