使用Proguard混淆基于Appium的jar时重复类定义

时间:2019-07-05 14:22:47

标签: selenium gradle appium proguard obfuscation

我正在构建一个 java 应用程序,该应用程序使用Appium来自动化移动应用程序。我正在使用Gradle来管理依赖项。

项目成功编译并构建了jar,但在使用gradle proguardtask进行混淆时,观察到以下错误。

> Task :proguardTask
Note: there were 57 duplicate class definitions.
...
...
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':proguardTask'.
...
...
...
Caused by: org.gradle.api.UncheckedIOException: java.io.IOException: 
Can't write [/home/mmt/eclipse-workspace/MobileTest/build/libs/MobileTest-0.1-obfus.jar] 
(Can't read [/home/mmt/eclipse-workspace/MobileTest/build/libs/MobileTest-0.1.jar] 
(Duplicate jar entry [org/openqa/selenium/SearchContext.class]))

我尝试exclude处理没有运气的可传递依赖项。

尝试进行transitive = false并手动添加可传递对象仍无法获得所需的输出。

以下是我的构建脚本。

build.gradle


    buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:6.1.1'
    }
    } plugins {
        id 'java'
        id 'maven-publish'
    }
    repositories {
        mavenLocal()
        maven {
            url = 'http://repo.maven.apache.org/maven2'
        }
    }
    dependencies {
        compile 'org.testng:testng:6.14.3'
        compile 'io.appium:java-client:6.1.0'
        compile 'org.apache.poi:poi:3.16'
        compile 'net.sourceforge.tess4j:tess4j:4.3.0'
        compile 'log4j:log4j:1.2.17'
        testCompile 'junit:junit:4.11'
    }

    group = 'com.mobile'
    version = '0.1'
    description = 'AppiumTest'
    sourceCompatibility = '1.8'

    publishing {
        publications {
            maven(MavenPublication) {
                from(components.java)
            }
        }
    }

    jar{
        from { configurations.compile.collect { zipTree(it) } }
        manifest {
            attributes 'Implementation-Title': 'Jar File with dependencies',  
                'Implementation-Version': version,
                'Main-Class': 'com.mobile.MobileTest.App'
        }
    }

    task proguardTask(type: proguard.gradle.ProGuardTask,dependsOn: jar) {
        configuration file('proguard.pro')
        injars 'build/libs/MobileTest-0.1.jar'
        outjars 'build/libs/MobileTest-0.1-obfus.jar'   
    }

    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }

    test {
        useTestNG()
    }

proguard.pro

-dontoptimize
-allowaccessmodification
-dontshrink
-dontwarn
-dontpreverify
-keepattributes Signature
-keepattributes Exceptions
-keep public class com.mobile.MobileTest.App {*;}
-keep class module-info{*;}

-keep public class com.beust.testng.**{*;}
-keep public interface com.beust.testng.**{*;}
-keep public enum com.beust.testng.**{*;}

-keep public class org.apache.log4j.**{*;}
-keep public interface org.apache.log4j.**{*;}
-keep public enum org.apache.log4j.**{*;}

-keep public class io.appium.java_client.**{*;}
-keep public interface io.appium.java_client.**{*;}
-keep public enum io.appium.java_client.**{*;}

-keep public class kobaltBuild.classes.**{*;}
-keep public interface kobaltBuild.classes.**{*;}
-keep public enum kobaltBuild.classes.**{*;}

-keep public class net.sourceforge.tess4j.**{*;}
-keep public interface net.sourceforge.tess4j.**{*;}
-keep public enum net.sourceforge.tess4j.**{*;}

-keep public class org.**{*;}
-keep public interface org.**{*;}
-keep public enum org.**{*;}

-keep public class scripts.**{*;}
-keep public interface scripts.**{*;}
-keep public enum scripts.**{*;}

-keep public class src.main.resources.**{*;}
-keep public interface src.main.resources.**{*;}
-keep public enum src.main.resources.**{*;}

-keep public class tessdata.**{*;}
-keep public interface tessdata.**{*;}
-keep public enum tessdata.**{*;}

-keep public class com.recognition.software.jdeskew.**{*;}
-keep public interface com.recognition.software.jdeskew.**{*;}
-keep public enum com.recognition.software.jdeskew.**{*;}

-keep public class bsh.**{*;}
-keep public interface bsh.**{*;}
-keep public enum bsh.**{*;}

-keep public class java.**{*;}
-keep public interface java.**{*;}
-keep public enum java.**{*;}

-keep public class javax.**{*;}
-keep public interface javax.**{*;}
-keep public enum javax.**{*;}

-keep public class org.**{*;}
-keep public interface org.**{*;}
-keep public enum org.**{*;}

任何朝着正确方向的指针都值得赞赏。

更新

找到仓库here

1 个答案:

答案 0 :(得分:1)

jar任务在打包时允许重复的类,但是progaurd不允许这样做。添加以下内容以强制jar任务排除重复项:

jar {
    duplicatesStrategy = 'exclude'
    from { configurations.compile.collect { zipTree(it) } }
    ...
}

此处有更多信息:https://discuss.gradle.org/t/duplicated-classes-output-jar-with-gradle/17301