Last active
November 17, 2023 08:33
-
-
Save iurysza/8b1dfb998bb9054cd708043a55f8e227 to your computer and use it in GitHub Desktop.
Publication strategy for kotlin libraries using proguard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryVersion=0.1.0 | |
libraryArtifactId=kotlin-library | |
proguardVersion=7.0.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import proguard.gradle.ProGuardTask | |
buildscript { | |
repositories { | |
jcenter() | |
google() | |
} | |
dependencies { | |
classpath "com.guardsquare:proguard-gradle:$proguardVersion" | |
} | |
} | |
task("obfuscateArtifact", type: ProGuardTask, dependsOn: jar) { | |
description = "Obfuscates source files" | |
def artifactName = "$libraryArtifactId-${libraryVersion}.jar" | |
def obfuscatedFolder = "$buildDir/obfuscated" | |
injars "$buildDir/libs/$artifactName" | |
outjars "$obfuscatedFolder/$artifactName" | |
// For de-obfuscating stack traces later on | |
printseeds "$obfuscatedFolder/seeds.txt" | |
printmapping "$obfuscatedFolder/mapping.txt" | |
// dependencies | |
libraryjars "${System.getProperty('java.home')}/lib/rt.jar" //works for java 8 | |
libraryjars configurations.runtime | |
libraryjars sourceSets.main.compileClasspath | |
configuration files("$projectDir/gradle/configuration.pro") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group = "com.group" | |
version = "$libraryVersion" | |
publishing { | |
publications { | |
create("main", MavenPublication) { | |
artifactId = libraryArtifactId | |
it.artifact("$buildDir/obfuscated/${libraryArtifactId}-${version}.jar") { | |
builtBy obfuscateArtifact | |
} | |
addLibInfoToPom(it) | |
addDependenciesToPom(it) | |
} | |
create("unobfuscated", MavenPublication) { | |
artifactId = "$libraryArtifactId-unobfuscated" | |
from components["java"] | |
addLibInfoToPom(it) | |
} | |
} | |
repositories { | |
maven { | |
name = "LocalBuild" | |
url = "file://$buildDir/repo" | |
} | |
maven { | |
name = "NexusName" | |
url = "https://nexus/repository/releases/" | |
credentials { | |
username = System.getenv("NEXUS_USERNAME") | |
password = System.getenv("NEXUS_PASSWORD") | |
} | |
} | |
} | |
} | |
private addLibInfoToPom(Publication publication) { | |
publication.pom { | |
name = "My obfuscated library" | |
description = "Does stuff that I want to hide" | |
} | |
} | |
private addDependenciesToPom(MavenPublication publication) { | |
publication.pom.withXml { | |
def dependencies = asNode().appendNode("dependencies") | |
configurations.implementation.allDependencies.each { dep -> | |
def depNode = dependencies.appendNode("dependency") | |
depNode.appendNode("groupId", dep.group) | |
depNode.appendNode("artifactId", dep.name) | |
depNode.appendNode("version", dep.version) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment