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.
You must specify injars in your Proguard task.
Example:
task proguard(type: proguard.gradle.ProGuardTask) {
injars "build/libs"
}
I could obfuscate with proguard a non-Android project with the plugin proguard-gradle .
Specify your proguard configurations (keep options) in a file
Specify input & output jars
Specify the dependencies (jars) which your project referred to.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'net.sf.proguard:proguard-gradle:6.0.3'
classpath 'net.sf.proguard:proguard-base:6.0.3'
}
}
def dependsDir = "${buildDir}/libs/dependencies/"
def outputJar = "${buildDir}/libs/${rootProject.name}.jar"
task proguard(type: proguard.gradle.ProGuardTask) {
configuration 'proguard.conf'
injars "${outputJar}"
outjars "${buildDir}/libs/${rootProject.name}_proguard.jar"
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
libraryjars "${dependsDir}"
}
Add the above gradle task to your build.gradle
Execute gradle proguard
Related
I published a small library of mine to a free maven hosting service, and am using that package in another project. I've done this before without issue but something isn't working this time.
Gradle finds the file just fine, and downloads it I assume. I know this because any change to the URL, package name, or version, and Gradle throws the "can't find the dep in any of these places" error.
However, any import of the packages in this JAR error, saying it can't find the package. IntelliJ, when I refresh gradle deps, also doesn't show my library in the "External Libraries" section.
Here's my gradle.build for the project I'm using the library in:
apply plugin: 'java'
repositories {
maven { url = 'https://repo.repsy.io/mvn/viveleroi/tileowner' }
}
group = project.property("group")
version = project.property("version")
dependencies {
compileOnly 'network.darkhelmet.tileowner:tileowner:1.0.0'
}
compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
options.forkOptions.executable = 'javac'
}
The test class:
import network.darkhelmet.tileowner.TileOwner;
public class Demo {}
Gradle says it's on the classpath:
compileClasspath - Compile classpath for source set 'main'.
\--- network.darkhelmet.tileowner:tileowner:1.0.0
compileOnly - Compile only dependencies for source set 'main'. (n)
\--- network.darkhelmet.tileowner:tileowner:1.0.0 (n)
I've downloaded the published jar of my library from that repository, unzipped it, and have confirmed all files are in there as expected.
Here's the build.gradle of my library project:
import org.apache.tools.ant.filters.ReplaceTokens
buildscript {
dependencies {
classpath group: 'com.github.rodionmoiseev.gradle.plugins', name: 'idea-utils', version: '0.2'
}
}
plugins {
id "com.github.johnrengelman.shadow" version "7.0.0"
id "xyz.jpenilla.run-paper" version "1.0.6"
id 'maven-publish'
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'idea-utils'
apply plugin: 'checkstyle'
processResources {
filter ReplaceTokens, tokens: [
"apiversion": project.property("apiversion"),
"version" : project.property("version")
]
}
repositories {
mavenLocal()
mavenCentral()
maven { url = "https://repo.aikar.co/content/groups/aikar/" }
maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
}
group = project.property("group")
version = project.property("version")
targetCompatibility = sourceCompatibility = JavaVersion.VERSION_17
dependencies {
compileOnly 'org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT'
implementation 'co.aikar:acf-bukkit:0.5.0-SNAPSHOT'
}
compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
options.forkOptions.executable = 'javac'
}
jar {
actions = []
dependsOn = []
dependsOn('shadowJar')
}
shadowJar {
relocate 'co.aikar.commands', 'network.darkhelmet.tileowner.acf'
relocate 'co.aikar.locales', 'network.darkhelmet.tileowner.locales'
}
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
repositories {
maven {
name = 'repsy'
url = 'https://repo.repsy.io/mvn/viveleroi/tileowner'
credentials(PasswordCredentials)
}
}
}
Adding clojure to an already existing gradle java project
I have a Java project that I want to start trying to add some Clojure to, but I'm hitting a few issues. I'm using the IntelliJ IDEA with the Cursive for Clojure IntelliJ plugin.
I am also using the gradle-clojure plugin for Clojure. My Java classes recognise and can call my Clojure code, but my Java code will no longer compile because at compile time it can no longer see my Clojure code.
Do I need to add an extra step in my build.gradle? Do I need to compile my Clojure separate and manually before trying to compile Java?
Any help would be greatly appreciated.
Environment
gradle-clojure v0.3.1 gradle v4.4.1Java v1.8 Intellij IDE on MacOS High Sierra
Stacktrace <> Task :compileJava FAILED java:8: error: cannot find symbol import com.example.clojure;
You can use Netflix's Clojure Wrapper, nebula.clojure.
Here is an example of a Gradle Build script for a project that uses Java and Clojure:
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "com.netflix.nebula:nebula-clojure-plugin:4.4.0"
}
}
plugins {
id "nebula.clojure" version "4.4.0"
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.demo'
def artifactName = 'demo-service'
version = latestRepoTag()
static def latestRepoTag() {
def cmd = "git describe --abbrev=0"
def proc = cmd.execute();
return proc.text.trim();
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext['spring-restdocs.version'] = '1.2.1.RELEASE'
ext {
springCloudVersion = 'Dalston.SR4'
}
compileJava {
classpath = project.files(
project.compileClojure.outputs,
classpath
)
}
compileClojure {
jvmOptions {
jvmArgs '-Djava.awt.headless=true'
}
}
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
jar {
baseName = "${artifactName}"
version = latestRepoTag()
}
clojure.aotCompile = true
configurations{
dev
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Dalston.SR3'
}
}
dependencies {
compile 'org.clojure:clojure:1.6.0'
compile('com.google.guava:guava:19.0')
compile("commons-io:commons-io:2.5")
compile "org.apache.pdfbox:pdfbox:2.0.0-RC3"
compile("org.apache.commons:commons-lang3:3.0")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.cloud:spring-cloud-starter-eureka")
compileOnly("org.projectlombok:lombok:1.16.6")
runtime('com.h2database:h2:1.4.190')
runtime("com.ingres.jdbc:iijdbc:10.0-4.0.5")
runtime('org.apache.commons:commons-dbcp2:2.1.1')
runtime('org.postgresql:postgresql:9.4.1209')
dev("org.springframework.boot:spring-boot-devtools")
testCompile('com.jayway.jsonpath:json-path')
testCompile('com.jayway.jsonpath:json-path-assert:2.2.0')
testCompile('com.google.code.gson:gson:2.8.1')
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.springframework.restdocs:spring-restdocs-mockmvc:1.2.1.RELEASE")
}
task wrapper(type: Wrapper) {
gradleVersion = '4.1'
}
bootRun {
classpath = sourceSets.main.runtimeClasspath + configurations.dev
systemProperties = System.properties
jvmArgs = ["-client", "-Dsun.net.inetaddr.ttl=60", "-Djava.security.egd=file:/dev/./urandom"]
environment = [
'spring_profiles_active': 'beta,fast_discovery'
]
}
Here is a link to the plugin on Gradle's plugin documentation site:
I need to use browsermob-proxy.
My project can be run from IDE, but when I build it by gradle and add compile 'net.lightbody.bmp:browsermob-core:2.1.4' into my gradle config file the jar is successfully built (there are no any errors), but the main function is not loaded:
#gradle clean jar
#java -jar build/proxy-0.1.jar
"Error: Could not find or load main class myproject.MainKt"
If I add this utility to my existed package (that could be built and run as a fat jar) it can not be built.
What am I doing wrong?
Is it possible that it is bug in browsermob-proxy?
Thanks.
My gradle file is the following:
group 'proxy'
version '0.1'
buildscript {
ext.kotlin_version = '1.1.3'
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.jetbrains.kotlin', name: 'kotlin-gradle-plugin', version: kotlin_version
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M6'
}
}
apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'
repositories {
mavenCentral()
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib-jre8'
compile 'org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version'
compile 'net.lightbody.bmp:browsermob-core:2.1.4'
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M6")
testCompile("org.junit.jupiter:junit-jupiter-params:5.0.0-M6")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M6")
}
jar {
destinationDir = file('build/')
manifest {
attributes 'Main-Class': 'myproject.MainKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
I found some help in this question. So I know that I must use JDK7 in order for unit testing to work.. So I came up with a build sript like this:
buildscript
{
repositories
{
jcenter()
}
dependencies
{
classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
}
}
retrolambda.oldjdk = 'C:/Program Files/Java/jdk1.7.0_79'
apply plugin: 'org.javafxports.jfxmobile'
apply plugin: 'java'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
jcenter()
}
dependencies
{
testCompile 'junit:junit:4.12'
}
mainClassName = 'AppMain'
jfxmobile
{
javafxportsVersion = '8.60.7'
android
{
applicationPackage = 'com.somename.someapplication'
androidSdk = 'C:/Program Files (x86)/Android/android-sdk' //
}
}
But i keep getting this exception:
Could not get unknown property 'retrolambda' for root project
Any ideas as to how I can fix this?
You have to apply the jfxmobile plugin first, which in turn will add the retrolambda dependency. Then you can configure the retrolambda oldJdk variable.
(Pay attention for the camelCase spelling)
buildscript
{
repositories
{
jcenter()
}
dependencies
{
classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
}
}
apply plugin: 'org.javafxports.jfxmobile'
apply plugin: 'java'
retrolambda.oldJdk = 'C:/Program Files/Java/jdk1.7.0_79'
I'm trying to make build configuration of multimodule project.
My goal is to have configuration, which will build war-file and deploy it to Tomcat.
My project contains four subprojects. There are:
api-common
api-school
api-spo
api-odo
I'd like to have war with all of modules or just some of them (api-common must always be in war)
I've main build.gradle where I configure cargo plugin
apply plugin: 'idea'
apply plugin: 'com.bmuschko.cargo'
allprojects{
apply plugin: 'java'
group = 'ru.dnevnik.enrollment'
version = '2.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bmuschko:gradle-cargo-plugin:2.1'
}
}
cargo {
containerId = 'tomcat7x'
deployable {
context = 'api'
file = file('enrollment-api\\build\\libs\\enrollment-api-1.0.war') //todo: bad practice
}
}
Enrollment-api project has build.gradle file as
apply plugin: 'war'
configurations {
full
odo
spo
school
}
dependencies {
compile project(':enrollment-api-common')
odo project(':enrollment-api-odo')
school project(':enrollment-api-school')
spo project(':enrollment-api-spo')
}
I don't understand how to use configurations to run for example
gradle cargoDeployRemote {someFlag}
to achive my goal