Recently has been doing Android self-packaged, has been completed in the form of pure command line packaging, the original ecological ant script packaging. And the Android-based SDK package. and the realization of multi-channel packaging, and later colleagues recommended Gradle, online information said Gradle all kinds of good, their own interest is to achieve a bit. In fact, because Android's support for Eclipse is weakened, most people use Gradle to merge with Android Studio, so the sample will be much more, but the author's project is more in eclipse. Because the developers in the transplant process found that the error is much more so has not been completely transplanted (well, in fact, the early party transplant), so the author is using the eclipse of the IDE to do experiments, the following first put a few basic knowledge
The first is to create a new Android project, and then use your own IDE to generate Gradle files, detailed reference http://blog.csdn.net/x605940745/article/details/ 41242687, interested comrades can take pure handwriting, such a way can not rely on the IDE, so it will be better
This is the original build.gradle .
Buildscript {repositories {mavencentral ()} dependencies {classpath ' Com.android.tools.build:gra dle:0.8.+ '}}apply plugin: ' Android ' dependencies {compile filetree (dir: ' Libs ', include: ' *.jar ')}android {Comp Ilesdkversion buildtoolsversion "19.0.3" sourcesets {main {manifest.srcfile ' androidmanifest. XML ' java.srcdirs = [' src '] resources.srcdirs = [' src '] aidl.srcdirs = [' src '] Renderscript.srcdirs = [' src '] res.srcdirs = [' res '] assets.srcdirs = [' Assets ']}// Move the tests to Tests/java, tests/res, etc ... Instrumenttest.setroot (' tests ')//Move The build types to build-types/<type>//For instance, build-t Ypes/debug/java, Build-types/debug/androidmanifest.xml, ...//This moves them out of them the default location under s rc/<type>/which would//conflict with src/being used by the main source Set. Adding New build types or product flavors should be accompanied//by a similar customization. Debug.setroot (' Build-types/debug ') release.setroot (' Build-types/release ')}}
I'll attach my own build.gradle to the following.
Buildscript {repositories {mavencentral ()} dependencies {classpath ' Com.android.tools.build:gra dle:0.12.+ '}}apply plugin: ' Android ' ant.importbuild ' build.xml '//The ant script is imported here dependencies {compile Filetree (dir: ' Libs ', include: ' *.jar ')}android {compilesdkversion buildtoolsversion ' 20.0.0 ' signingconfigs { Signature, here the file name and password are wrong, there will be externally imported statements Myconfig {//absolute path and relative path are able to storefile file ("E:\\KEYSTORE\\MYDEMO.K Eystore ")//Signature file Storepassword" 276021750 "//password keyalias" Mydemo.keystore " Keypassword "111"}} buildtypes{Release {//This is the step used to generate the APK. See note//1 in detail. Add signature signingconfig signingconfigs.myconfig//2.runproguard run obfuscation code runproguard TRUE//Mixed Xiao Rule file proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-project.txt '}} p Roductflavors {//Here is multi-channelPlace, Androidmanifest.xml file in the face to be marked, the following will be affixed to the file Yingyongbao {manifestplaceholders = [channel_name: "Yingyongbao "]} umeng {manifestplaceholders = [channel_name:" Umeng "]} Wandoujia {manifestplaceholders = [channel_name: "Wandoujia"]}} allprojects {//Here is an external import file, then change the properties of the file itself. This only changes the signature, but also can change the other afterevaluate {project, def propsfile = Rootproject.file (' E:\\keystore\\keystore.properti Es ') def configname = ' myconfig ' if (propsfile.exists () && android.signingConfigs.hasProperty (config Name) {def props = new Properties () props.load (new FileInputStream (Propsfile)) Android. Signingconfigs[configname].storefile = File (props[' storefile ') android.signingconfigs[configname].storepasswor D = props[' Storepassword ' Android.signingconfigs[configname].keyalias = props[' Keyalias '] Android.s IgningcOnfigs[configname].keypassword = props[' Keypassword '}}} sourcesets {main {mani Fest.srcfile ' androidmanifest.xml ' java.srcdirs = [' src '] resources.srcdirs = [' src '] aid L.srcdirs = [' src '] renderscript.srcdirs = [' src '] res.srcdirs = [' res '] Assets.srcdirs = [' Assets ']} Move the tests to Tests/java, tests/res, etc ... Instrumenttest.setroot (' tests ')//Move The build types to build-types/<type>//For instance, build-t Ypes/debug/java, Build-types/debug/androidmanifest.xml, ...//This moves them out of them the default location under s rc/<type>/which would//conflict with src/being used by the main source set. Adding New build types or product flavors should be accompanied//by a similar customization. Debug.setroot (' Build-types/debug ') release.setroot(' Build-types/release ')}} Here the detailed features to see the notes will be able to, the violence posted on the no problem. Here are a few files to paste, one is Androidmanifest.xml file, inside is added a line
<meta-data android:name= "Umeng_channel" android:value= "${channel_name}"/>
Build.xml added a target to write Gradle deploy with the cmd command to run the contents.
<target name= "Deploy" > <!--<replaceregexp flags= "G" byline= "true" > <regexp pattern= " public static final Host host = Host.development; "/> substitution expression is the replacement value. The replaced values are defined in the corresponding configuration file <substitution expression= "public static final host host = Host.test;" in the/> fileset attribute Dir is used to specify the folder where the replaced file is located includes to specify which file to replace. <fileset dir= "./src/net/xtion/crm/base" includes= "Crmappcontext.java"/> </replaceregexp> <replace encoding= "Utf-8" dir= "./src/com/example/learn723" ><include name= "Mainactivity.java"/> <replacefilter token= "public static final String host =" Host.test "," value= "public static final String host =" Host.ab C ";"/></replace></target>
Another signature file
Storefile=e:\\keystore\\mydemo.keystorestorepassword=276021750keypassword=276021750keyalias=mydemo.keystore
Detailed demo can be downloaded here
http://download.csdn.net/detail/killer1989/8927225
This advantage we can see. Change the file information with ant full-time. Here specifically to do multi-channel packaging, to be able to separate, in fact, to do all their own initiative has two steps, one is from SVN own initiative to obtain. This online has a very large number of versions to learn, there is a script to change the Build.gradle file, dynamic transformation of the signature (in fact, can do bulk changes) and dynamically add these functions of the code, can use the shell, can use Python, you need to be able to try
Gradle Pack Android (implement external import signature file, multi-channel packaging, import ant script)