我想知道如何使用 yguard 来混淆和 android 库项目。我尝试过,但每次生成空白 jar。
我也尝试过 ProGuard,但它似乎并没有混淆类文件中的字符串。
这是我的 build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="yguard" basedir=".">
<target name="init">
<property name="project_name" value="Utils"/>
<property name="srcDir" value="src\com\example\utils"/>
<property name="classDir" value="gen\com\example\utils"/>
<property name="jar" value="${project_name}.jar"/>
<property name="obfjar" value="${project_name}_obf.jar"/>
<property name="renamelog" value="${project_name}_renamelog.xml"/>
<property name="shrinklog" value="${project_name}_shrinklog.xml"/>
<property name="mainclass" value="Utils"/>
<mkdir dir="${classDir}" />
</target>
<target depends="jar" name="yguard">
<taskdef name="yguard" classname="com.yworks.yguard.YGuardTask" classpath="libs/yguard.jar"/>
<yguard>
<inoutpair in="${jar}" out="${obfjar}"/>
<attribute name="Deprecated"/>
<shrink logfile="${shrinklog}">
<keep>
<method name="void CopyStream(InputStream is, OutputStream os)" class="${mainclass}" />
</keep>
</shrink>
<rename mainclass="${mainclass}" logfile="${renamelog}">
<keep>
<class classes="none" methods="public" fields="none">
<patternset>
<include name="com.example.utils.*"/>
</patternset>
</class>
</keep>
</rename>
</yguard>
</target>
<!-- compile -->
<target name="compile" depends="init">
<javac srcdir="${srcDir}" includes="com/example/utils/*.java" destdir="${classDir}">
</javac>
</target>
<!-- create .jar -->
<target name="jar" depends="compile">
<jar jarfile="${jar}" basedir="${classDir}" includes="com/example/utils/*"/>
</target>
<!-- run project -->
<target name="run" depends="yguard">
<java classname="${mainclass}" fork="true">
<classpath>
<pathelement location="${obfjar}"/>
</classpath>
</java>
</target>
<!-- removes all that has been built -->
<target name="clean" depends="init">
<delete dir="${classDir}" includeEmptyDirs="true" />
</target>
</project>
还有我的 com.example.utils.Utils 文件,它是一个实用程序类,将在其他项目中使用:
package com.example.utils;
public class Utils {
public static String url1 = "http://www/url1";
public static String url2= "http://www/url2";
public static void CopyStream(InputStream is, OutputStream os)
{
final int buffer_size=1024;
try
{
byte[] bytes=new byte[buffer_size];
for(;;)
{
int count=is.read(bytes, 0, buffer_size);
if(count==-1)
break;
os.write(bytes, 0, count);
}
}
catch(Exception ex){}
}
public static InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
public static String downloadStringfromUrl(String urlString) throws IOException
{
InputStream is = Utils.downloadUrl(urlString);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line);
is.close();
return sb.toString();
}
}