开发者

Producing an obfuscated library out of an Android Library Project

开发者 https://www.devze.com 2023-04-11 17:50 出处:网络
I\'ve written an Android application using an Android Library project, also written by me. I have to release the source code of the app, but I don\'t want to distribute the sources of the library. Thi

I've written an Android application using an Android Library project, also written by me. I have to release the source code of the app, but I don't want to distribute the sources of the library. This library defines, among other things, custom views, so it includes XML layouts and resources. I was thinking of releasing this library in binary f开发者_StackOverfloworm (a .jar maybe?), to be referenced by the main project. I want this binary library to be obfuscated via Proguard. Is this feasible? How?

Thanks.


As mentioned by Morrison Chang, there currently isn't any way to release an android library project as a jar, nor is there any way to obfuscate the project with the exception of the java source. However, there is a way to obfuscate the source code, although this is not officially supported.

All you need to do is run the "ant clean release" command in your library. The obfuscated source is written into "bin/proguard/obfuscated.jar". Just put that jar file in the libs directory of your exported library project, and delete the contents of the src directory, and your pretty much done.

There is one gotcha with the above approach, however. It doesn't handle resources quite right. To fix this, you should strip out all the resource classes (R.class and R$*.class) from your copy of obfuscated.jar. This in turn will require you to disable obfuscation for these resource classes. This can be done by adding the following to proguard-project.txt:

-keep public class **.R {
  public *;
}
-keep public class **.R$* {
  public *;
}

For your reference, here is the ant target I use to build an obfuscated, sourceless android library project:

<?xml version="1.0" encoding="UTF-8"?>
<project name="library_rules" default="librelease">
  <target name="librelease" depends="release"
      description="Build a sourceless and obfuscated android library.">
    <property name="libname" value="myProject" />
    <property name="librelease.dir" location="bin/${libname}" />
    <delete file="${librelease.dir}"/>
    <mkdir dir="${librelease.dir}"/>
    <mkdir dir="${librelease.dir}/libs"/>
    <mkdir dir="${librelease.dir}/src"/>
    <copy todir="${librelease.dir}/res">
      <fileset dir="res"/>
    </copy>
    <copy file="AndroidManifest.xml" todir="${librelease.dir}" />
    <copy file="ant.properties" todir="${librelease.dir}" />
    <copy file="build.xml" todir="${librelease.dir}" />
    <copy file="project.properties" tofile="${librelease.dir}/project.properties" />
    <jar destfile="${librelease.dir}/libs/${libname}.jar">
      <zipfileset src="bin/proguard/obfuscated.jar" excludes="**/R.class,**/R$$*.class"/>
    </jar>
  </target>
</project>

The android library project to be exported will be located in bin/myProject. This has been tested using Android SDK tools v20.0.3 and v21.


I don't think you can get what you want. An Android Library Project is a full Android Project and contains all of the resources that you mention. If you want to obfuscate pure code, so that only your API is visible then you can create one via Proguard. However as Proguard can only handle Java files, it has no knowledge about Android xml and resources which require the Android SDK tool chain to pack into an APK. So in short if just Java code in an external jar (which may call out to Android APIs) you can Proguard it. If you are thinking about other Android resource files then I don't believe so.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号