Ant实现Android代码编译混淆(“找不到程序包R”解决方法)

最近在项目里本来是用ProguardGUI实现混淆并生成jar包,想写一个Ant的xml来实现一键生成

但在过程中执行javac的时候遇到了“找不到程序包R”的错误,纠结了好久,下面一部分一部分的介绍下

初始化部分就不多说了,主要是实现手动clean的部分,看代码:

<pre name="code" class="html"><target name="init"
	  description="Initializing all output directories" >
     <!--  Delete the ${build} and ${dist} directory trees -->
	  <delete dir="${build}"/>
	  <delete dir="${dist}"/>
	  <mkdir dir="${build}"/>

</target>



打包资源,生成R包(这一步在命令行进行ant操作的时候是必须的,在Eclipse里会自动生成R.java,这步就不需要)


 <target name="package-resource" depends="init">
      <mkdir dir="${build}/classes"/>
      <mkdir dir="${rootdir}/gen"/>
      <exec executable="${aapt}" failonerror="true">
          <arg line="package" />
          <arg line="-f" />
          <arg line="-m" />
          <arg line="-J ${rootdir}/gen " />
          <arg line="-S ${rootdir}/res"/>
          <arg line="-M AndroidManifest.xml" />
          <arg line="-A ${rootdir}/assets"/>
          <arg line="-I ${android.jar}" />
      </exec>
</target> 

编译,重点来了,错误就是在这步

在javac中,漏掉了两个<src path>的定义,才会报找不到R的错误

    <target name="compile" depends="package-resource"
	      description="compile the source " >
	<!--   Compile the java code from ${src} into ${build} -->
	  <mkdir dir="${build}"/>
	  <mkdir dir="${build}/${assets_name}"/>
	    <javac encoding="utf-8" target="1.6" destdir="${build}" bootclasspath="${android.jar}">
	        <src path="src"/>
	        <src path="gen"/>
		    <classpath>
				<fileset dir="${external.lib}" includes="*.jar"/>
			</classpath>
	    </javac> 
     </target>

最后转移jar包并混淆生成


	 <target name="dist" depends="compile"
	       description="generate the distribution" >
	
	   <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
	  <jar jarfile="${dist}/ad_weibo_sdk_temp_${DSTAMP}.jar" basedir="${build}"/>
	  
	  <delete dir="${build}"/>
	</target>
	
	<target name="obfuscate"  depends="dist">
	
	  <!-- Add Proguard Tasks -->
	  <property name="proguard.jar" location="${android.tools.dir}/proguard/lib/proguard.jar" />
	  <taskdef name="proguard" classname="proguard.ant.ProGuardTask" classpath="${proguard.jar}" />
	
	  <proguard>
	      -include      proguard.cfg
	      -injars       ${dist}/temp_${DSTAMP}.jar
	      -outjars      ${dist}/release.jar
	      -libraryjars  libs/http1220.jar
	      -libraryjars  libs/android-support-v4.jar
	      -libraryjars  ${android.jar}
	      -dump         "${dist}/dump.txt"
	      -printseeds   "${dist}/seeds.txt"
	      -printusage   "${dist}/usage.txt"
	      -printmapping "${dist}/mapping.txt"
	  </proguard>
	              
	</target>



阅读更多
换一批

没有更多推荐了,返回首页