Problem Background: our game project needs to be transplanted to a channel. This channel has its own billing and login method, providing us with an android SDK project, which only contains res, SRC is a jar package.
Idea: Use the Eclipse project to package, decompress the APK, view the class. DEX and resource. ARSC files in it, check what content is available, and write a script to implement the same processing.
The general Android project ant package resources on the Internet are already rampant. I will not go into details here. For more information,
1. it is a good habit to merge the res files of third-party libraries and their own projects. Files in the library of common channels start with their own channel names, because we cannot make the two projects contain the same files. For the merge of strings and styles with the same file names, we need to merge the corresponding content. The merged res file is what we finally use.
2. Use the aapt tool to generate two R files. One r file is under the package path of the project, and the other R file is under the package path of the third-party project. Method: The aapt tool is used twice. The package path is obtained from androidmanifest. xml of the two projects respectively.
3. When you use the aapt tool to generate resource resources, remember that-S is the merged res. Otherwise, the corresponding file cannot be found during the program running.
In this way, the resources of the third-party project will be transferred to the APK file. Add the code resource jar in two ways.
1. Add a third-party jar package during compilation; otherwise, the compilation will not pass.
2. When the Dex is generated and the tool dx is used, the jar path is added, so that the jar is entered into Dex. So far, the third-party jar package has been added.
Note:
$ {SDK. dir}: Android SDK path. You can specify your own location.
$ {Lib-Dir}: Put the libs path of third-party jar under your project
$ {Outdir-gen}: Project SRC path
$ {Resource-all-res-Dir}: merged res path
$ {Library-Dir}: third-party project path
$ {Projecthomedir}: path of your project
<? XML version = "1.0" encoding = "gb18030"?>
<Project name = "anttask" basedir = "." default = "signapk">
<Path id = "android. antlibs">
<Pathelement Path = "$ {SDK. dir}/tools/lib/anttasks. Jar"/>
<Pathelement Path = "$ {SDK. dir}/tools/lib/sdklib. Jar"/>
<Pathelement Path = "$ {SDK. dir}/tools/lib/androidprefs. Jar"/>
</Path>
<Path id = "project. classpath">
<Pathelement location = "$ {Android-jar}"/>
</Path>
<Path id = "lib_classpath">
<Pathelement location = "bin"/>
<Pathelement location = "$ {Android-jar}"/>
<Fileset dir = "$ {Lib-Dir}">
<Span style = "color: #009900;"> <include name = "third-party jar. Jar"/> </span>
</Fileset>
</Path>
<Taskdef name = "setup" classname = "com. Android. Ant. setuptask" classpathref = "android. antlibs"/>
<Setup/>
<Span style = "background-color: RGB (204,204,204)"> 2. generate two R files. The path of the r file package is different: </span>
<Span style = "background-color: RGB (204,0, 0) "> </span> <PRE class =" html "name =" code "> <target name =" genrjava ">
<Echo> generating R. Java/manifest. Java from the resources... </echo>
<Mkdir dir = "$ {outdir-gen}"/>
<Echo> <span style = "font-size: 18px; color: #990000; font-weight: bold"> androidmanifest from your own project. XML to get the r path to generate the r file </span> </echo>
<Exec executable = "$ {tools_aapt}" failonerror = "true">
<Arg value = "package"/>
<Arg value = "-M"/>
<Arg value = "-J"/>
<Arg value = "$ {outdir-gen}"/>
<Arg value = "-M"/>
<Arg value = "$ {projecthomedir}/androidmanifest. xml"/>
<Arg value = "-s"/>
<Arg value = "$ {resource-all-res-Dir}"/>
<Arg value = "-I"/>
<Arg value = "$ {Android-jar}"/>
<Arg value = "$ {Lib-Dir}"/>
<Echo> <span style = "font-size: 18px; color: #990000; font-weight: bold"> androidmanifest from a third-party project. XML to get the r path to generate the r file </span> </echo>
</Exec> <exec executable = "$ {tools_aapt}" failonerror = "true"> <
Arg value = "package"/>
<Arg value = "-M"/>
<Arg value = "-J"/>
<Arg value = "$ {outdir-gen}"/>
<Arg value = "-M"/>
<Arg value = "<span style =" background-color: RGB (0,153, 0) ">$ {library-Dir}/androidmanifest. xml </span>"/>
<Arg value = "-s"/>
<Arg value = "<span style =" font-size: 18px; color: #006600; font-weight: bold ">$ {resource-all-res-Dir} </span>"/>
<Arg value = "-I"/>
<Arg value = "$ {Android-jar}"/>
<Arg value = "$ {Lib-Dir}"/>
</Exec>
</Target> </PRE> <br>
<Br>
<PRE style = "font-size: 18px; font-weight: bold"> </PRE>
<P style = "font-size: 18px; font-weight: bold"> </P>
<PRE style = "font-size: 18px; font-weight: bold"> </PRE>
3. <span style = "font-size: 18px"> <span style = "background-color: RGB (204,204,204)"> <strong> set. aidl file generation. java files </strong> </span>
<P style = "font-size: 18px; font-weight: bold"> </P>
<P style = "font-size: 18px; font-weight: bold"> <span style = "background-color: RGB (204,0, 0) "> </span> </P>
<PRE style = "font-size: 18px; font-weight: bold" class = "html" name = "code"> <target name = "aidl">
<Echo> compiling aidl files into Java classes... </echo>
<Apply executable = "$ {tools_aidl}" failonerror = "true">
<Arg value = "-p $ {SDK. dir}"/>
<Arg value = "-I $ {srcdir}"/>
<Fileset dir = "$ {srcdir}">
<Include name = "**/*. aidl"/>
</Fileset>
</Apply>
</Target> </PRE> <br>
<Span style = "font-size: 18px"> <span style = "background-color: RGB (204,204,204)"> <strong> 4. compile a Java file </strong> </span>
<P style = "font-size: 18px; font-weight: bold"> </P>
<P style = "font-size: 18px; font-weight: bold"> </P>
<PRE style = "font-size: 18px; font-weight: bold" class = "html" name = "code"> <! -- Compile this project's. java files into. class files. -->
<Target name = "compile" depends = "genrjava, aidl">
<Mkdir dir = "$ {outdir-classes}"/>
<Javac executable = "$ {javacpath}" fork = "true" DEBUG = "false" extdirs = "" srcdir = "$ {outdir-gen}" destdir = "$ {outdir -Classes} ">
<Classpath refID = "lib_classpath"/>
</Javac>
</Target> </PRE>
<P> <span style = "background-color: RGB (204,204,204)"> 5. obfuscation </span> </P>
<PRE style = "font-size: 18px; font-weight: bold" class = "html" name = "code"> <span style = "background-color: RGB (204,0, 0) "> </span> <PRE class =" html "name =" code "> </PRE> <PRE class =" html "name =" code "> </PRE> <PRE class = "html" name = "code"> <! -- Execute proguard class flies -->
<Target name = "proguard" depends = "compile">
<Jar basedir = "$ {outdir-classes}" destfile = "Temp. Jar"/>
<Java jar = "$ {proguard-home}/proguard. Jar" fork = "true" failonerror = "true">
<Jvmarg value = "-dmaximum. inlined. Code. Length = 32"/>
<Arg value = "-injars temp. Jar"/>
<Arg value = "-outjars optimized. Jar"/>
<Arg value = "-libraryjars $ {Android-jar}"/>
<Arg value = "-libraryjars $ {Lib-Dir}/third-party jar"/>
<Arg value = "-dontpreverify"/>
<Arg value = "-dontoptimize"/>
<Arg value = "-dontusemixedcaseclassnames"/>
<Arg value = "-repackageclasses'' "/>
<Arg value = "-allowaccessmodification"/>
<Arg value = "-keep public class * extends Android. App. Activity"/>
<Arg value = "-keep public class * extends Android. content. contentprovider"/>
<Arg value = "-keep public class * extends Android. View. View"/>
<Arg value = "-keep public class * extends javax. microedition. MIDlet. MIDlet"/>
<Arg value = "-keep public class * extends Android. Preference. Preference"/>
<Arg value = "-optimizationpasses 7"/>
<Arg value = "-verbose"/>
<Arg value = "-dontskipnonpubliclibraryclasses"/>
<Arg value = "-dontskipnonpubliclibraryclassmembers"/>
</Java>
<Delete file = "Temp. Jar"/>
<Delete dir = "$ {outdir-classes}"/>
<Mkdir dir = "$ {outdir-classes}"/>
<Unzip src = "optimized. Jar" DEST = "$ {outdir-classes}"/>
<Delete file = "optimized. Jar"/>
</Target>
<! -- End proguard class files --> </PRE> <br>
<Span style = "background-color: RGB (204,204,204)"> 6. Generate DEX and include the JAR file. </span>
<PRE> </PRE>
<PRE style = "font-size: 18px; font-weight: bold" class = "html" name = "code"> <PRE style = "font-size: 18px; font-weight: bold "class =" html "name =" code "> <PRE class =" html "name =" code "> <! -- Convert this project's. class files into. Dex files. -->
<Target name = "Dex" depends = "compile">
<Echo> converting compiled files and external libraries into $ {outdir-DX} \ test. Dex... </echo>
<Mkdir dir = "$ {outdir-DX}"/>
<Apply executable = "$ {tools_dx}" failonerror = "true" parallel = "true">
<Arg value = "-- Dex"/>
<Arg value = "-- output =$ {basedir}/dx/test. Dex"/>
<Arg Path = "$ {outdir-classes}"/>
<Fileset dir = "$ {projecthomedir}" includes = "*. Jar"/>
<Arg Path = "$ {Lib-Dir}"/>
</Apply>
</Target> </PRE> <br>
<Br>
<PRE> </PRE>
7. <span style = "background-color: RGB (204,204,204)"> package res </span> <PRE class = "html" name = "code"> <! -- Put the project's resources into the output package file. -->
<Target name = "package-res">
<Echo> packaging resources and assets... </echo>
<Mkdir dir = "$ {apkdir}"/>
<Exec executable = "$ {tools_aapt}" failonerror = "true">
<Arg value = "package"/>
<Arg value = "-F"/>
<Arg value = "-M"/>
<Arg value = "$ {projecthomedir}/androidmanifest. xml"/>
<Arg value = "-s"/>
<Arg value = "$ {resource-all-res-Dir}"/>
<Arg value = "-a"/>
<Arg value = "$ {asset-Dir}"/>
<Arg value = "-I"/>
<Arg value = "$ {Android-jar}"/>
<Arg value = "-F"/>
<Arg value = "$ {resources-package}"/>
</Exec>
</Target> </PRE> <br>
8. <span style = "background-color: RGB (204,204,204)"> package an APK </span> <br>
<PRE class = "html" name = "code"> <target name = "packageapk" depends = "Dex, package-res">
<Echo> packaging APK for release... </echo>
<Exec executable = "$ {tools_apkbuilder}" failonerror = "true">
<Arg value = "$ {basedir}/APK/$ {unsignedapkname}"/>
<Arg value = "-U"/>
<Arg value = "-z"/>
<Arg value = "$ {basedir}/respak"/>
<Arg value = "-F"/>
<Arg value = "$ {basedir}/dx/test. Dex"/>
<Arg value = "-RF"/>
<Arg value = "$ {basedir}/src"/>
<Arg value = "-RJ"/>
<Arg value = "$ {basedir}/src"/>
</Exec>
<Echo> it will need to be signed with jarsigner before being published. </echo>
</Target> </PRE> <br>
9. <span style = "background-color: RGB (204,204,204)"> signature APK </span> <br>
<PRE class = "html" name = "code"> <target name = "signapk" depends = "packageapk">
<Echo> packaging $ {out-Unsigned-package} for release... </echo>
<Signjar
Jar = "$ {basedir}/APK/$ {unsignedapkname }"
Signedjar = "$ {basedir}/APK/$ {apkname }"
Keystore = "******"
Storepass = "******"
Alias = "alias"
Keypass = "******"
Verbose = "-verbose"/>
</Target>
</Project> </PRE> <br>
<Br>
</PRE>