Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

代码混淆后的jar包中不包含依赖的第三方库 #29

Open
wu9007 opened this issue Oct 30, 2023 · 2 comments
Open

代码混淆后的jar包中不包含依赖的第三方库 #29

wu9007 opened this issue Oct 30, 2023 · 2 comments

Comments

@wu9007
Copy link

wu9007 commented Oct 30, 2023

执行bootPro任务后打包出的jar中缺少了依赖的第三方sdk

环境:

jdk1.8
gradle 6.8
springboot 2.7.14

build.gradle配置如下:

tasks.register('proguard', ProGuardTask) {
        dependsOn compileJava
        // 输出混淆前->混淆后的映射
        printmapping "$buildDir/mapping.txt"
        // 混淆规则文件
        configuration '../config/proguard/proguard-rules.pro'
        // 混淆时依赖的库
        libraryjars files(configurations.compile.findAll {}.collect())
        libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
        libraryjars "${System.getProperty('java.home')}/lib/jce.jar"
        // 混淆输入
        injars sourceSets.main.output
        // 混淆输出
        outjars "$buildDir/classes-pro"
    }

    tasks.register('bootPro', BootJar) {
        dependsOn 'proguard'
        // 重新组织boot-inf下的文件
        bootInf {
            into('classes') {
                from "$buildDir/classes-pro"
            }
            into('lib') {
                from configurations.runtime
            }
        }
        // 包名
        archiveBaseName = "shinow-pro"
        // 入口
        mainClass = 'com.shinow.template.ServerApplication'
        // 删除混淆后的文件 避免下次混淆打包报错
        doLast {
            new File("$buildDir/classes-pro").deleteDir()
        }
    }

dependencies {
        runtimeOnly "com.shinow.brick:project-module-admin:${adminVersion}"
        implementation "com.shinow.brick:project-common-admin:${adminVersion}"
        implementation "com.shinow.brick:brick-module-schedule:${brickModuleVersion}"

        implementation "org.springframework.boot:spring-boot-starter-data-redis"
        implementation "com.github.xiaoymin:knife4j-openapi2-spring-boot-starter:4.3.0"

        implementation fileTree(dir: '../lib', includes: ['*.jar'])
        compileOnly 'org.projectlombok:lombok:1.18.24'
        annotationProcessor 'org.projectlombok:lombok:1.18.24'
        implementation 'org.mapstruct:mapstruct:1.4.2.Final'
        annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
        implementation 'com.mysql:mysql-connector-j'
        implementation 'org.postgresql:postgresql:42.4.1'
        implementation 'com.oracle.ojdbc:ojdbc8:19.3.0.0'
        implementation 'com.oracle.database.nls:orai18n:19.7.0.0'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testAnnotationProcessor "org.mapstruct:mapstruct-processor:1.4.2.Final"
        runtimeOnly "org.owasp:dependency-check-gradle:${dependencyCheckVersion}"
        pmd "com.alibaba.p3c:p3c-pmd:${p3c}"
    }

proguard-rules.pro

-obfuscationdictionary 'directory.txt'
-classobfuscationdictionary 'directory.txt'
-packageobfuscationdictionary 'directory.txt'
-overloadaggressively

# 不警告
-dontwarn

# 不理会警告,否则混淆失败
-ignorewarnings

# 不压缩
-dontshrink

# 不优化
-dontoptimize

# 一个类中的成员不使用重复的命名,如Student类混淆后不能出现a属性和a方法。
-useuniqueclassmembernames

# 不混淆注解
-keepattributes *Annotation*

# 不混淆泛型
-keepattributes Signature

# 保留程序入口
-keep class com.shinow.template.ServerApplication {
    public *;
}

# 不混淆枚举
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
@wu9007
Copy link
Author

wu9007 commented Oct 31, 2023

找到原因了,因为我使用的是implementation方式依赖的,需要调整为compile。

但是在gradle的6.8版本compile已经废弃了,请问有没有其他更好的方法来解决这个问题?

@pkaq
Copy link
Member

pkaq commented Oct 31, 2023

用api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants