Java library with dexguard - java

I would like to obfuscate my pure Java library but I am not sure how to use dexguard in my and build.xml file. Can somebody show me an example? My current code is following:
<property file="build.properties"/>
<taskdef resource="build.properties"
classpath="dexguard.jar" />
<target name="build">
<antcall target="clean"/>
</target>
<target name="clean">
<delete dir="build"/>
<antcall target="compile"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
<antcall target="jar"/>
</target>
<target name="jar" >
<mkdir dir="build/jar"/>
<jar destfile="build/jar/testlib.jar" basedir="build/classes">
</jar>
<antcall target="obfuscate"/>
</target>
<target name="obfuscate"
description="Obfuscate compiled classes">
<dexguard>
-libraryjars "dexguard.jar"
-injars build/jar/testlib.jar
-outjars build/jar/testlib_obf.jar
</dexguard>
<target name="run">
<java jar="build/jar/testlib.jar" fork="true"/>
</target>

Use Gradle.
task obfDexguard(type: com.guardsquare.dexguard.gradle.DexGuardTask) {
configuration 'dexguard.txt'
injars "build/jar/testlib.jar"
outjars "build/jar/testlib_obf.jar"
}

Related

How to build jar-file with ant?

According to the training task, I need to create .jar-file with ant. There are no problems with the Assembly of ordinary classes that are in src .
But I have in project present logger, libraries to him and .properties file.
because of this, errors fly out:
[javac] error: package org.apache.logging.log4j does not exist
and
error: cannot find symbol
[javac] error: private static final Logger logger = LogManager.getLogger(Hello.class);
I have build.xml
<?xml version="1.0"?>
<project name="Runner" default="run">
<!-- define names of directories -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="classes" location="${build}/classes"/>
<!-- define all targets -->
<target name="compile">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false"/>
</target>
<target name="run" depends="compile">
<java classname="${ant.project.name}" classpath="${classes}"/>
</target>
<target name="clean">
<delete dir="${build}"/>
</target>
<target name="package" depends="compile">
<jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}">
<manifest>
<attribute name="Main–Class" value="${ant.project.name}"/>
</manifest>
</jar>
</target>
</project>
and this stracture
How and where to gather library and property in Java file to jar-file was executable?
I figured it out.
<path id="classpath">
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="init" depends="clean" description="starts">
<tstamp/>
</target>
<target name="clean" depends="package-to-jar" description="clean up">
<delete dir="${classes}"/>
<delete file="${external-lib}"/>
</target>
<target name="package-to-jar" depends="package-external-lib" description="packing all project into a jar-file">
<jar destfile="${jar}/${ant.project.name}.jar" basedir="${classes}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
<zipfileset src="${external-lib}"/>
</jar>
<delete dir="${classes}"/>
<delete file="${external-lib}"/>
</target>
<target name="package-external-lib" depends="compile" description="packing external libraries into a jar-file">
<jar destfile="${external-lib}">
<zipgroupfileset dir="${lib}">
<include name="**/*.jar"/>
</zipgroupfileset>
</jar>
</target>
<target name="compile" description="compile the source">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" classpathref="classpath"/>
<copy todir="${classes}">
<fileset dir="${src}" excludes="**/*.java"/>
</copy>
</target>
Firstly you need include required library folders to class path
<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
and add reference to javac task in compile target
<target name="compile">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false">
<classpath refid="class.path" />
</javac>
</target>
aftewards include all properties file to jar
<target name="package" depends="compile">
<jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}">
<include name="*.properties"/>
<manifest>
<attribute name="Main–Class" value="${ant.project.name}"/>
</manifest>
</jar>
</target>

java build ant file with external jar files

I created an ant build file to compile my java src into a .jar file. It uses external jar files.
My files dir:
+src
----android
------------Address.java
------------HelloServer.java
------------HelloServerResource.java
+lib
------------org.codehaus.jackson.core.jar
------------org.codehaus.jackson.mapper.jar
------------org.json.jar
------------org.restlet.ext.jackson.jar
------------org.restlet.jar
build.xml
The external libraries are in lib folder.
The main class is in HelloServer.java
Here is my ant build file:
<project name="helloworld">
<property name="src-dir" location="src"/>
<property name="build-dir" location="build"/>
<property name="classes-dir" value="${build-dir}/classes"/>
<property name="dist-dir" location="dist"/>
<property name="lib-dir" value="lib"/>
<property name="jar-dir" value="${build-dir}/jar"/>
<property name="main-class" value="android.HelloServer"/>
<path id="classpath">
<fileset dir="${lib-dir}" includes="**/*.jar"/>
</path>
<target name="clean" description="compile the source">
<delete dir="${build-dir}" />
<delete dir="${dist-dir}" />
</target>
<target name="cleanall" depends="clean"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build-dir}"/>
<mkdir dir="${classes-dir}"/>
<mkdir dir="${jar-dir}"/>
<!--<mkdir dir="${dist-dir}"/>-->
</target>
<target name="compile" depends="init" description="compile the source " >
<javac srcdir="${src-dir}" destdir="${classes-dir}" classpathref="classpath" includeantruntime="false" />
<!--<javac srcdir="${src-dir}" destdir="${build-dir}"/>-->
</target>
<target name="jar" depends="compile">
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar-dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
</project>
I execute run and all the dirs were created successfully, the .jar file was created and it was able to be executed.
I open terminal and go to the jar dir. I tried to execute through command line like this and it gave me the following error:
$java -jar helloworld.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/restlet/Server
at android.HelloServer.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.restlet.Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
Why does it say class file not found? I thought by creating the helloworld jar file, i actually included the external libraries.
You'll need to combine all the jars together using zipgroupfileset:
<target name="jar" depends="compile">
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
<zipgroupfileset dir="${lib-dir}" includes="*.jar"/>
</jar>
</target>
See: Creating a bundle jar with ant
i found a solution. basically i compile my jar file and point it to the external jar files using manifest file.
here is my build file:
<project name="helloworld">
<property name="src-dir" location="src"/>
<property name="build-dir" location="build"/>
<property name="classes-dir" value="${build-dir}/classes"/>
<property name="dist-dir" location="dist"/>
<property name="lib-dir" value="lib"/>
<property name="jar-dir" value="${build-dir}/jar"/>
<property name="main-class" value="android.HelloServer"/>
<path id="classpath">
<fileset dir="${lib-dir}" includes="**/*.jar"/>
</path>
<target name="clean" description="compile the source">
<delete dir="${build-dir}" />
<delete dir="${dist-dir}" />
</target>
<target name="cleanall" depends="clean"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build-dir}"/>
<mkdir dir="${classes-dir}"/>
<mkdir dir="${jar-dir}"/>
<!--<mkdir dir="${dist-dir}"/>-->
</target>
<target name="compile" depends="init" description="compile the source " >
<javac srcdir="${src-dir}" destdir="${classes-dir}"
classpathref="classpath" includeantruntime="false" />
<!--<javac srcdir="${src-dir}" destdir="${build-dir}"/>-->
</target>
<target name="jar" depends="compile">
<manifestclasspath property="jar.classpath" jarfile="${jar-dir}/${ant.project.name}.jar">
<classpath refid="classpath"/>
</manifestclasspath>
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="${jar.classpath}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar-dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
</project>
The main difference is that i added the following into targer "jar":
<manifestclasspath property="jar.classpath" jarfile="${jar-dir}/${ant.project.name}.jar">
<classpath refid="classpath"/>
</manifestclasspath>
<attribute name="Class-Path" value="${jar.classpath}"/>

Ant does not work as I expect in Eclipse

I am new to ANT and to use it, I simply created a new java project in Eclipse that just print the word Welcome in the screen. I ran the program using Eclipse and "Welcome" was successfully printed on the screen. This is my program
public class welcome {
public static void main(String[] args)
{
System.out.println("Welcome!!!");
}
}
Then I just followed the usual way to build ANT file using Eclipse so I used Export feature and choose ANT buildfile.
This the buildfile I have got:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="test">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../usr/lib/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="test.classpath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="test.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="welcome">
<java classname="welcome" failonerror="true" fork="yes">
<classpath refid="test.classpath"/>
</java>
</target>
</project>
when I ran the program as Ant, it only gives me a message that build successful! without printing the "Welcome" word on screen!
This is the output
> Buildfile: /home/name/workspace/test/build.xml
> build-subprojects: init: build-project:
> [echo] test: /home/name/workspace/test/build.xml build: BUILD SUCCESSFUL Total time: 376 milliseconds
The default target of your ant file is "build" which will create a jar file of your project (which can be then executed later). If you want to run your project via ant, change the target to "welcome" (see the end of your ant file). That should execute the program as you expected.
Apache Ant is a Java library and command-line tool whose mission is to
drive processes described in build files as targets and extension
points dependent upon each other. The main known usage of Ant is the
build of Java applications
Also class names first letter should be CAPITALIZED ...
More or less this is how your ant should look like, but i still dont understand why you want to use ANT for whatever you are trying to do here.
<project name="Welcome" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="(packagename).(class name)"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>

Ant Classpath Error When Unzipping Input File Java

I'm a beginner with ant and am wondering what this error means. "failed to create task or type classpath" It gives me that error on the line that is unzipping the file.
<project name="Project2" default="run">
<target name="compile" depends="clean">
<javac srcdir="." includeantruntime="false"/>
</target>
<target name="clean">
<delete>
<fileset dir="." includes="**/*.class"/>
</delete>
<delete>
<fileset dir="." includes="META-INF"/>
</delete>
<delete>
<fileset dir="." includes="*.*~"/>
</delete>
</target>
<target name="getInput">
<get src="file.zip" dest="."/>
<unzip src="folder.zip" dest="folder"/>
</target>
<target name="run" depends= "compile, who, getInput" >
<java classname="Driver" fork="yes"/>
<classpath path = "."/>
</target>
<target name="who">
<echo message="Name"/>
</target>
<target name="jar">
<jar destfile="project2.jar" basedir="."/>
</target>
Any help would be appreciated!
this is what you should do:
put the src values to have the same value
check if the source file exists under the project root

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Layout

I'm trying to build an application using ant. Everything appears to be fine when I build but I continually get the above error for what I've tried so far.
java -jar dist/pmml_export.jar
java -cp ".:log4j-1.2.16.jar" -jar dist/pmml_export.jar
java -cp log4j-1.2.16.jar -jar dist/pmml_export.jar
I doubled checked to see if Layout was in the jar I'm referencing and it is there. I'm fairly new to both ant and log4j so I could be making an obvious mistake but I'm just not seeing it. Here is my build.xml.
<?xml version="1.0"?>
<project name="pmml_export" default="archive">
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<path id="compile.classpath">
<fileset dir="build/classes" includes="*.class" />
</path>
<property name="ant.dir" value="apache-log4j-1.2.16"/>
<path id="classpath">
<fileset dir="${ant.dir}" includes="**/*.jar"/>
</path>
<target name="exceptions" depends="init">
<javac srcdir="src/exceptions" destdir="build/classes" classpathref="compile.classpath"/>
<echo> Exceptions compiled! </echo>
</target>
<target name="symbol-table" depends="exceptions" >
<javac srcdir="src/translator/symbol_table" destdir="build/classes" classpathref="compile.classpath"/>
<echo> Symbol table compiled! </echo>
</target>
<target name="parser" depends="symbol-table" >
<javac srcdir="src/translator/parser" destdir="build/classes" classpathref="compile.classpath"/>
<echo> Parser compiled! </echo>
</target>
<target name="lexer" depends="parser" >
<javac srcdir="src/translator/lexer" destdir="build/classes" classpathref="compile.classpath"/>
<echo> Lexer compiled! </echo>
</target>
<target name="translator" depends="lexer" >
<javac srcdir="src/translator" destdir="build/classes" classpathref="compile.classpath"/>
<echo> Translator compiled! </echo>
</target>
<target name="exporter" depends="translator" >
<javac srcdir="src/pmml_export" destdir="build/classes" classpathref="compile.classpath" />
<echo> Exporter compiled! </echo>
</target>
<target name="archive" depends="exporter" >
<property name="manifest.mf" location="dist/manifest.txt" />
<manifest file="${manifest.mf}" >
<attribute name="Main-Class" value="pmml_export.PMML_Export"/>
</manifest>
<jar destfile="dist/pmml_export.jar" manifest="${manifest.mf}"
basedir="build/classes" />
</target>
<target name="run" depends="archive">
<java jar="dist/pmml_exporter.jar" fork="true">
<classpath>
<path refid="classpath"/>
<path location="dist/pmml_exporter.jar"/>
</classpath>
</java>
</target>
</project>
When you use the -jar option, the -cp and -classpath options are ignored. The proper way to embed the classpath with the -jar option is to set a Class-Path directive in the jar's MANIFEST.MF file.

Categories