Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with Proguard using Gradle in a non-Android project

I am trying to obfuscate the code of my Java project with Proguard. This project has been created on Intellij and is configured with Gradle 2.2.1. There is a proxy on the computer I work on so Intellij can not access the Internet.

I tried for hours to find a simple example to obfuscate my code.

I tried to do :

task proguardTask(type: proguard.gradle.ProGuardTask) { println "ha"}
task proguardTask(type: proguard.gradle.ProGuardTask) << { println "ha"}

and I got an exception every time :

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':proguardTask'.
> Index: 0, Size: 0
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at proguard.ClassPath.get(ClassPath.java:77)

Here's my build.gradle file:

// Configuration des library java
project.ext.LIBJava = [
        "project1",

        "project2", "project2",
        "project2_OS", "project2_UI",
        "project3",
        "project3_Windows"
]

//----------------------------------------------------------------------------------------------------------

ext.buildOnline = false
ext.buildOnlineMapsforge = false
ext.buildAndroid = false

//----------------------------------------------------------------------------------------------------------

apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'java'

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        flatDir dirs: 'C:/Path/To/Proguard/proguard-5.1/lib'
    }
    dependencies {
        classpath ':proguard'
    }
}

def filterProjects(filter) { return subprojects.findAll { project -> filter.contains(project.name) } }


allprojects {
    group = 'org.libWin'
    version = '0.1.1'
}

// Configuration injection for all subprojects
subprojects {
    repositories {
        mavenLocal()
        mavenCentral()
    }
}

//----------------------------------------------------------------------------------------------------------

task proguardTask(type: proguard.gradle.ProGuardTask) {
    println "ha"
}

configure(filterProjects(project.LIBJava)) {
    apply plugin: 'java'

    dependencies {
        if (buildOnline) { // On-line
            testCompile group: 'junit', name: 'junit', version: 4.11           //  "$jUnitVersion"
        }
    }

    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
}

Does anyone know where the problem comes from ? How to obfuscate easily my code with Proguard using Gradle ?

Thanks.

like image 899
Alexis Paoleschi Avatar asked Jan 21 '15 14:01

Alexis Paoleschi


1 Answers

You must specify injars in your Proguard task.

Example:

task proguard(type: proguard.gradle.ProGuardTask) {
    injars "build/libs"
}
like image 156
calclavia Avatar answered Oct 24 '22 09:10

calclavia