Android Development is now basically on par with Android studio, since the use of Android Studio development, you must master the use of Gradle, the use of Gradle packaging APK has become the mainstream trend, to facilitate the developers to build different versions of the application, To fulfill the different needs.
One, unified configuration item properties
1.Android Configuration
android { compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION) buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION defaultConfig { minSdkVersion project.ANDROID_BUILD_MIN_SDK_VERSION targetSdkVersion project.ANDROID_BUILD_TARGET_SDK_VERSION versionCode Integer.parseInt(project.VERSION_CODE) versionName project.VERSION_NAME }
As described in the preceding code:
The previous two sentences are configured with the SDK version and the compiled tools version of the project, and Minsdkversion, Targetsdkversion, Versioncode are configured in the defaultconfig below. And Versionname, I believe that all of you can understand the meaning of these nouns, and then look at their corresponding values, project. Version_name, where is this set again, look at the following:
VERSION_NAME=1.0.0VERSION_CODE=100ANDROID_BUILD_TARGET_SDK_VERSION=23ANDROID_BUILD_MIN_SDK_VERSION=14ANDROID_BUILD_TOOLS_VERSION=23.0.2ANDROID_BUILD_SDK_VERSION=23
These properties are set in the gradle.properties of the project
2. Version Configuration
android{Buildtypes {Debug{Buildconfigfield"Boolean","Log_debug","true"} release {Buildconfigfield"Boolean","Log_debug","false" //Obfuscation switchMinifyenabledtrue //Whether the zip is alignedZipalignenabledtrue //Whether to open the debuggable switchDebuggablefalse //Whether to open the jnidebuggable switchJnidebuggablefalse //Remove useless resource filesShrinkresourcestrue} }}
You can see that there are configuration debug switches, confusion and the like, are unified configuration.
3. Channel Configuration
productflavors {demotest {Buildconfigfield"Boolean","Url_product","false"ApplicationID"Cn.example.demo.debug"Manifestplaceholders = [getui_app_id:Project. getui_app_id, Getui_app_key:Project. Getui_app_key, Getui_app_secret:Project. Getui_app_secret, Package_name:applicationid]} demoproduct { Buildconfigfield"Boolean","Url_product","true"ApplicationID"Cn.example.demo"Manifestplaceholders = [getui_app_id:Project. Getui_app_id_release, Getui_app_key:Project. Getui_app_key_release, Getui_app_secret:Project. Getui_app_secret_release, Package_name:applicationid]}}
This is the configuration of different versions of the channel name, the channel configuration or the same as the Android configuration, in the Gradle.properties
#个推debug appid信息GETUI_APP_ID=sssssssssssssssssssssGETUI_APP_KEY=sssssssssssssssssssssGETUI_APP_SECRET=sssssssssssssssssssss#个推release版本appid 信息GETUI_APP_ID_RELEASE=xxxxxxxxxxxxxxxxxxxGETUI_APP_KEY_RELEASE=xxxxxxxxxxxxxxxxxxxGETUI_APP_SECRET_RELEASE=xxxxxxxxxxxxxxxxxxx
4. Signature information Configuration
For signature related information, it is certainly not good to write directly in Gradle, especially some open source projects that can be added to Gradle.properties:
RELEASE_KEY_PASSWORD=xxxxRELEASE_KEY_ALIAS=xxxRELEASE_STORE_PASSWORD=xxxRELEASE_STORE_FILE=../.keystore/xxx.jks
Then you can refer to it in Build.gradle:
{ signingConfigs { release { storeFile file(RELEASE_STORE_FILE) storePassword RELEASE_STORE_PASSWORD keyAlias RELEASE_KEY_ALIAS keyPassword RELEASE_KEY_PASSWORD } }}
5. Customize the exported apk name configuration
android { rename the apk with the version name applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File( output.outputFile.parent, "ganchai-${variant.buildType.name}-${variant.versionName}- ${variant.productFlavors[0].name}.apk".toLowerCase()) } }}
Second, confusion
Obfuscation can make anti-compilation code less readable, prevent others from stealing code, and can significantly reduce the size of the APK package.
1. First technique
First, in addition to the default obfuscation configuration (android-sdk/tools/proguard/proguard-android.txt), your own code must be configured yourself:
# # in the Proguard-rules.pro under module############################################ # # Main program can't confuse the code # # ############################################-dontwarn Com.example.**-keep class com. example. * * {*; }################################################ not optimize generics and reflection # # #############################################-keepattributes Signature
Next is the troublesome third-party library, in general, if it is the Aurora push, its package name is Cn.jpush, add the following code:
-dontwarn cn.jpush.**-keep class cn.jpush.** { *; }
The same is true of the other third libraries, or decompile these third-party libraries and confuse them with the package name.
Third, dynamic set up some additional information
If you want to add the current compile time, the compiled machine, the latest commit version to the APK, and this information is not written in the code, the powerful gradle gives us the opportunity:
Android {defaultconfig {Resvalue"string","Build_time", Buildtime () resvalue"string","Build_host",HostName() Resvalue"string","Build_revision", revision ()}}def Buildtime () {return NewDate ().format("Yyyy-mm-dd HH:mm:ss")}defHostName() {returnSystem.getproperty ("User.Name") +"@"+ Inetaddress.localhost.HostName}def Revision () {DEF code =NewBytearrayoutputstream () exec {CommandLine' git ',' Rev-parse ','--short ',' HEAD 'StandardOutput = code}returnCode.tostring ()}
The code implements a dynamic addition of 3 string resources: Build_time, Build_host, Build_revision, and then, in other places, as in the reference string, as follows:
// 在Activity里调用getString(R.string.build_time) // 输出build时间getString(R.string.build_host) // 输出电脑的用户名和PC名getString(R.string// 输出最后一次commit
Iv. Summary
Android packaging because the groovy language is powerful and powerful, it inevitably becomes complex. Gradle is still very complex, and is only used at present.
Android Gradle Use detailed