I want to obfuscate Java code in Gradle using yGuard. So far, I have managed to get the obfuscation working using:
dependencies {
compile 'com.yworks:yguard:2.9.2'
}
task yGuardObfuscate {
group 'yGuard'
description 'Obfuscates existing archives.'
dependsOn "installDist"
doLast {
ant.taskdef(
name: 'yguard',
classname: 'com.yworks.yguard.YGuardTask',
classpath: sourceSets.main.runtimeClasspath.asPath
)
ant.yguard {
inoutpairs {
fileset(dir: "./build/install/module") {
include(name: "module*.jar")
exclude(name: "*_obf.jar")
}
}
rename(logFile: "./build/install/module/rename.log") {
property(name: "naming-scheme", value: "mix")
}
}
}
}
However, that has the problem that the main function gets obscured, and I can't run it anymore using java -jar module_obf.jar (which works with the unobfuscated jar).
I have tried several ways of excluding the main function from obfuscation.
First, I tried using the #Obfuscate annotation as described in the documentation (https://yworks.github.io/yGuard/task_documentation/#controlling-obfuscation-exclusion-with-annotations), like this:
#com.yworks.util.annotation.Obfuscation( exclude = true, applyToMembers = false)
public static void main(String[] args) {
try {
new Start();
} catch (SQLException e) {
LOGGER.error(e, e);
}
}
In my IDE (Eclipse 2019-14), this line was shown as valid. However, when trying to compile it with the gradle task I created, I got the following error:
...\Start.java:22: error: package com.yworks.util.annotation does not exist
#com.yworks.util.annotation.Obfuscation( exclude = true, applyToMembers = false)
^
I tried a number of things to get this to work, but could not arrive at a working solution.
So next, I tried to use a keep > method element in the yGuardObfuscate task to explicitly keep the main function of the Start class, like this:
task yGuardObfuscate {
group 'yGuard'
description 'Obfuscates existing archives.'
dependsOn "installDist"
doLast {
ant.taskdef(
name: 'yguard',
classname: 'com.yworks.yguard.YGuardTask',
classpath: sourceSets.main.runtimeClasspath.asPath
)
ant.yguard {
inoutpairs {
fileset(dir: "./build/install/module") {
include(name: "module*.jar")
exclude(name: "*_obf.jar")
}
}
rename(logFile: "./build/install/module/rename.log") {
property(name: "naming-scheme", value: "mix")
keep {
method("class": "com.company.project.module.Start", name: "public static void main(String[] args)")
}
}
}
}
}
This caused the Gradle build to fail with the following exception:
: java.lang.IllegalArgumentException: '(' expected but found void
[...]
Caused by: java.lang.IllegalArgumentException: '(' expected but found void
at com.yworks.yguard.ObfuscatorTask.toNativeMethod(ObfuscatorTask.java:188)
at com.yworks.yguard.ant.MethodSection.addEntries(MethodSection.java:35)
at com.yworks.yguard.ant.ExposeSection.createEntries(ExposeSection.java:170)
at com.yworks.yguard.ObfuscatorTask.execute(ObfuscatorTask.java:745)
at com.yworks.yguard.YGuardTask.execute(YGuardTask.java:116)
[...]
Root cause: java.lang.IllegalArgumentException: '(' expected but found void
at com.yworks.yguard.ObfuscatorTask.toNativeMethod(ObfuscatorTask.java:188)
at com.yworks.yguard.ant.MethodSection.addEntries(MethodSection.java:35)
at com.yworks.yguard.ant.ExposeSection.createEntries(ExposeSection.java:170)
at com.yworks.yguard.ObfuscatorTask.execute(ObfuscatorTask.java:745)
at com.yworks.yguard.YGuardTask.execute(YGuardTask.java:116)
Again, I tried several things here, such as writing "class" without the "", adding extra {}, but nothing helped.
So the question here is: What am I doing wrong? And how can I prevent yGuard from obfuscating the main function?
I now figured out a way to make it work, even though it sorta feels more like a workaround than a proper solution, so if anyone knows what I did wrong with the "official" solutions, please do tell me.
For this, I used the Annotation approach, and instead of using the default annotation, I created a custom annotation that is pretty much an exact copy in my project.
The annotation class looks like this:
package com.company.project.module.annotations;
public #interface Obfuscation {
boolean exclude() default true;
boolean applyToMembers() default true;
}
I use it in my Start class like this:
import com.company.project.module.annotations.*;
[...]
#Obfuscation( exclude = true, applyToMembers = false)
public static void main(String[] args) {
[...]
}
And finally, I added this custom annotation class to the rename element of my task like this:
rename(logFile: "./build/install/module/rename.log", annotationClass: "com.company.project.module.annotations.Obfuscation") {
property(name: "naming-scheme", value: "mix")
}
So the entire gradle task now looks like this:
task yGuardObfuscate {
group 'yGuard'
description 'Obfuscates existing archives.'
dependsOn "installDist"
doLast {
ant.taskdef(
name: 'yguard',
classname: 'com.yworks.yguard.YGuardTask',
classpath: sourceSets.main.runtimeClasspath.asPath
)
ant.yguard {
inoutpairs {
fileset(dir: "./build/install/module") {
include(name: "module*.jar")
exclude(name: "*_obf.jar")
}
}
rename(logFile: "./build/install/module/rename.log", annotationClass: "com.company.project.module.annotations.Obfuscation") {
property(name: "naming-scheme", value: "mix")
}
}
}
}
With that, the build now works, and I can successfully run my program using java -jar module_obf.jar.
Related
I am using the below configuration build.gradle
plugins {
id "com.google.protobuf" version "0.8.17"
id "java"
}
group "de.prerna.aws.tests"
version "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
ext {
protobufVersion = "3.18.1"
}
dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
sourceSets {
main {
proto {
srcDir 'src/main/proto'
}
java {
// include self written and generated code
srcDirs 'src/main/java'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:4.0.0-rc-2'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.39.0"
}
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
generateProtoTasks {
all().each { task ->
task.plugins { grpc{} }
}
ofSourceSet('main')
}
}
Error
* What went wrong:
Execution failed for task ':processResources'.
> Entry Person.proto is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/7.2/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.
A variant of BParolini for build.gradle (Groovy DSL)
tasks.withType(Copy) {
filesMatching("**/*.proto") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
}
I could fix this problem by adding the following code to my build.gradle.kts:
tasks {
withType<Copy> {
filesMatching("**/*.proto") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}
}
}
Extra info: I'm using Gradle 7.3-rc-3 and Java 17.
Unfortunately nobody explains reasons for this problem, so here is some of my explorations and guesses. Please correct me if you know more.
If found that following build script code causes this error:
proto { srcDir 'src/main/proto' }
If look inside "build/extracted-include-protos" directory, there are original .proto files copied into "build/extracted-include-protos/test" (but not into main).
My guess is that those auto-copied .proto files are originally uses as the only sources, but when adding "src/main/proto" source set we give some compiler tool second set of same files.
Removing this srcDir is not a good idea, because it required for IDEA to correctly open included .proto on Ctrl+click (otherwise it is opened extracted copies which is useless).
This question was answered before but the chosen answer doesn't explain a lot for me on how this is doable on Gradle.
That and the fact that I can't comment on the solution to ask for more info forced me to make this question.
I have a Gradle project that has several modules available and I now want to set up the Javadoc task to combine the Javadoc comments of all the modules into a single location where I could browse it.
How would I now be able to do this using Gradle? I run Gradle 5.5 if the version is of any importance and I have the following things set in the build.gradle file:
allprojects {
ext {
// Convenience method to configure Javadoc
configureJavadoc = { Object jDocConfig ->
jDocConfig.options {
it.author()
it.encoding = 'UTF-8'
it.memberLevel = JavadocMemberLevel.PROTECTED
if (it instanceof StandardJavadocDocletOptions) {
def opt = it as StandardJavadocDocletOptions
opt.links(
"https://docs.example.com/java/"
)
if (JavaVersion.current().isJava9Compatible()) {
opt.addBooleanOption("html5", true)
opt.addStringOption("-release", "8")
}
if (JavaVersion.current().isJava11Compatible()) {
opt.addBooleanOption("-no-module-directories", true)
}
}
}
}
}
}
subprojects {
javadoc {
destinationDir = file("$rootDir/docs/")
configureJavadoc(it)
}
}
I was able to do it with:
def exportedProjects = [
":",
":module-a",
":module-b",
":module-c"
]
task allJavadoc(type: Javadoc) {
source exportedProjects.collect { project(it).sourceSets.main.allJava }
classpath = files(exportedProjects.collect { project(it).sourceSets.main.compileClasspath })
destinationDir = file("${buildDir}/docs/javadoc-all")
}
Recently we had a version mismatch problem with a class org.apache.commons.beanutils.PropertyUtilsBean. We thought that mismatch is just between some dependency that brings commons-beanutils in versions 1.8 and 1.9.3 but after tracking and excluding each transitive dependency we were still facing an issue.
It turns out that the the PropertyUtilsBean was also packed inside the commons-digester3-3.2-with-deps instead declared as dependency to commons-beanutils.
Is it possible in gradle to search all dependencies (including transitive ones) for specific fully qualified classname? That way we could resolve such problems on the spot.
I tried it and it is possible using some custom gradle build logic:
Kotlin DSL
tasks {
val searchClass by creating {
doLast {
configurations.forEach { // check all configurations
if (it.isCanBeResolved) {
try {
val classLoader = configToClassloader(it)
// replace here class you are looking for
val cl = Class.forName("arrow.core.Either", false, classLoader)
println("found in Configuration $it")
println(cl.protectionDomain.codeSource.location)
} catch (e: Exception) {}
}
}
}
}
}
// Helper function: convert a gradle configuration to ClassLoader
fun configToClassloader(config: Configuration) =
URLClassLoader(
config.files.map {
it.toURI().toURL()
}.toTypedArray())
This could be further enhanced by replacing the hard coded classname with some parameter mechanism.
Sample output:
> Task :searchClass
Configuration configuration ':domain:apiDependenciesMetadata'
file:/Users/abendt/.gradle/caches/modules-2/files-2.1/io.arrow-kt/arrow-core-data/0.9.0/a5b0228eebd5ee2f233f9aa9b9b624a32f84f328/arrow-core-data-0.9.0.jar
Groovy DSL
def configToClassloader(config) {
return new URLClassLoader(
*config.files.collect {
it.toURI().toURL()
}.toArray())
}
task searchClass {
doLast {
configurations.forEach { // check all configurations
if (it.canBeResolved) {
try {
def classLoader = configToClassloader(it)
// replace here class you are looking for
def cl = Class.forName("arrow.core.Either", false, classLoader)
println("found in Configuration $it")
println(cl.protectionDomain.codeSource.location)
} catch (e) {}
}
}
}
}
Edit: I have recently created a Gradle Plugin that provides the described tasks: https://plugins.gradle.org/plugin/io.github.redgreencoding.findclass
You could do this
task findJarsForClass {
doLast {
def findMe = 'org/apache/commons/beanutils/PropertyUtilsBean.class'
def matches = configurations.runtime.findAll { f ->
f.name.endsWith('.jar') &&
!(zipTree(f).matching { include findMe }.empty)
}
println "Found $findMe in ${matches*.name}"
}
}
Just ctrl+left click class name which was imported, then you can see the jar on your ide(eclipse has that feature, probably IntelliJ has as well)
Try using the task dependencyInsight :
gradle -q dependencyInsight --configuration compile --dependency commons-beanutils
Every Gradle project provides the task dependencyInsight to render the
so-called dependency insight report from the command line. Given a
dependency in the dependency graph you can identify the selection
reason and track down the origin of the dependency selection.
I am trying to run the static main method of a java class from my build.gradle script asp art of the build process. I am using Android Studio 1.0.2 with the Android/Gradle Plugin 'com.android.tools.build:gradle:1.0.0'
The java class whose main method I want to run during the build resides in ...\trunk-gradle\myproject\src\main\java\de\myapp\gradle
package de.myapp.gradle;
public class ConfigureCustomer {
public static void main(String[] args){
String server = args[0];
String customer = args[1];
System.out.println(String.format("Configuring customer %s with server %s", customer, server));
}
}
Before I used ANT to call that java method as follows:
<java failonerror="yes" classname="de.myapp.gradle.ConfigureCustomer ">
<classpath>
<path location="${base.dir}/bin/classes/"/>
</classpath>
<arg line="${customer}"/>
<arg line="${server }"/>
</java>
But now I am migrating to Groovy, so here is the relevant part of my project's build.gradle file that tries to execute the main method of above class (actual task definition is at the end just before the dependencies):
apply plugin: 'com.android.application'
android {
project.ext.set("customer", "")
project.ext.set("server", "")
dexOptions {
preDexLibraries = false
}
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
//Default configuration
}
signingConfigs {
release {
//Configuration for release builds
}
}
buildTypes {
debug{
server = "test"
}
release {
server = "release"
}
}
productFlavors {
customerA{
customer = "a"
}
customerB{
customer = "b"
}
customerC{
customer = "c"
}
}
}
task (configureCustomer, type: JavaExec) {
println 'Running customer configuration...'
main = 'de.myapp.gradle.ConfigureCustomer'
args customer, server
}
dependencies {
//Dependency settings
}
So now when I run the following via the command line (windows):
graldew configureCustomer
I get the following error message:
Error: Could not find or load main class
de.myapp.gradle.ConfigureCustomer
My questions hence are as follows:
How do I manage to fix the error message above? Do I have to move my java class to another folder? Maybe configure sth in the build scipt?
How can I make sure the java task is executed after the classes have actually been compiled?
If i wanted to execute the task configureCustomer as part of another task, would I simply write the following line in my gradle's task definition?
configureCustomer
I also tried to add the classpath:
task (configureCustomer, type: JavaExec) {
println 'Running customer configuration...'
main = 'de.myapp.gradle.ConfigureCustomer'
classpath = sourceSets.main.runtimeClasspath
args customer, server
}
But all that got me was a gradle build error message saying:
Could not find property "main" on SourceSet container
So apparently "sourceSets.main.runtimeClasspath" does not exist in Android Studio's Gradle. Maybe it's named differently. Though I also tried setting the classpath like this:
classpath = '${projectDir.getAbsolutePath()}\\build\\intermediates\\classes\\' + customer + '\\release'
and I also tried this:
classpath = '${projectDir.getAbsolutePath()}\\build\\intermediates\\classes\\' + customer + '\\release\\de\\myapp\\gradle'
None of which worked, the error from above persists:
Error: Could not find or load main class
de.myapp.gradle.ConfigureCustomer
I finally found something that works for Android/Gradle but getting there seemed a lot more complicated, than it should have been.
So for recap - here is the Java class whose main method I'd like to execute:
package de.myapp.gradle;
public class ConfigureCustomer {
public static void main(String[] args){
String customer = args[0];
String versionName = args[1];
System.out.println(String.format("Configuring customer %s with versionName %s", customer, versionName ));
}
}
I want to execute the above for each flavor and only for release builds (not debug builds) so here is my task definition (you'd still have to make your task depend on one of the gradle build tasks so it's executed - I am depending on the preBuild task for this purpose):
android {
//Build type setup, signing configuration and other stuff
}
//After the android block my task definition follows:
task buildPrintout(type: JavaExec) {
android.applicationVariants.all { variant ->
//Runt he java task for every flavor
variant.productFlavors.each { flavor ->
println "Triggering customer configuration for flavor " + flavor.name
if (variant.buildType.name.equals('release')) {
//Run the java task only for release builds
//Cant find the runtime classpath in android/gradle so I'll directly link to my jar file here. The jarfile contains the class I want to run (with the main method)
classpath += files("libs/my-jarfile.jar")
//This is the fully qualified name of my class, including package name (de.myapp.gradle) and the classname (ConfigureCustomer)
main = "de.myapp.gradle.ConfigureCustomer"
//Pass in arguments - in this case the customer's name and the version name for the app (from AndroidManifest.xml)
args flavor.name, variant.versionName
}
}
}
}
You'll notice that I dumped the idea of having my Class integrated in the android project I am about to build. Instead I made that one class a separate project, built a jar file and dropped it in the libs folder of the android project i am building.
UPDATE 04.02.2015
I have slightly modified the above to use the javaexec method instead of the JavaExec Task type:
preBuild.doFirst {
android.applicationVariants.all { variant ->
variant.productFlavors.each { flavor ->
if (variant.buildType.name.equals('release')) {
javaexec {
println "Triggering customer build for flavor " + flavor.name
classpath += files("libs/my-jarfile.jar")
main = "de.myapp.gradle.ConfigureCustomer"
args flavor.name, variant.versionName
}
println "Done building customer for flavor " + flavor.name
}
}
}
}
And here is yet another variation, where we define the javaexec within a reusable (which is preferred) task, that we then add as a dependency to the preBuild task:
//Define our custom task and add the closures as an action
task buildCustomer << {
android.applicationVariants.all { variant ->
variant.productFlavors.each { flavor ->
if (variant.buildType.name.equals('release')) {
javaexec {
println "Triggering customer build for flavor " + flavor.name
classpath += files("libs/my-jarfile.jar")
main = "de.myapp.gradle.ConfigureCustomer"
args flavor.name, variant.versionName
}
println "Done building customer for flavor " + flavor.name
}
}
}
}
//Make preBuild depend on our task
preBuild.dependsOn buildCustomer
If you have any questions let me know and I'll try to answer them.
Change the way of configing classpath
classpath(files('build/intermediates/classes/release',"${android.getSdkDirectory().getAbsolutePath() + '/platforms/' + android.compileSdkVersion + '/android.jar'}"))
It works on android gradle 1.5
I have read many similar questions where the reply is that the project structure is not ideal so my questions based on the following:
I have a main project (ProjA) which needs to include a second project (ProjB) which is not a child project. ProjB has various resource files which need to be copied in the distribution of ProjA.
build.gradle of ProjA
dependencies {
compile project(":ProjB")
}
distributions {
main {
baseName = "Something"
contents {
into('bin') { from jar.archivePath }
into('lib') { from configurations.runtime }
into('etc') {
from ('../../projb/src/main/webapp') // Fix me!
}
}
}
}
1.) Ideally ProjB should expose the location of the resource files through a property used by ProjA, how can this be done?
2.) Is this the correct way to do it as I have read alot about cross-project properties not being ideal - or should I be doing something completely different?
Don't know if it helps but it seems that the best way is to do it in the following way:
distributions {
main {
baseName = "Something"
contents {
into('bin') { from jar.archivePath }
into('lib') { from configurations.runtime }
into('etc') {
from project(':projB').file('src/main/webapp')
}
}
}
}
The path must be hardcoded in that case.
Second option might be specifying a project property - in general not a very good idea - and use in another project - there must be also evaluation order defined.
In projB
ext.resourcesDir = project.file('src/main/webapp2')
and in projA
evaluationDependsOn(':projB')
and:
distributions {
main {
baseName = "Something"
contents {
into('bin') { from jar.archivePath }
into('lib') { from configurations.runtime }
into('etc') {
from project(':projB').file('src/main/webapp')
from project(':projB').resourcesDir
}
}
}
}
Here's complete example.