Android Studio Multi-channel bulk packaging and code obfuscation _android

Source: Internet
Author: User
Tags pack

One, bulk packaging

1, the integration of the Friendship Union statistics, and in the Androidmanifest.xml added the following code

<meta-data
android:name= "Umeng_channel"
android:value= "${channel_value}"/>

2, add the following code under the Android tab of App Build.gradle:

productflavors {
MyApp {}
_360 {}
Appchina {} hiapk {}} productflavors.all {
Flavor- > flavor.manifestplaceholders = [channel_value:name]
}

3. Check to see if the Gradle PATH environment variable is configured, execute gradle-v under the command line, and if it is not recognized, locate the Gradle directory under the Androidstudio installation directory, add the bin directory under it to the path, and then execute the following command:

Gradle Assemblerelease

Wait to compile package success, do not know is not my first use of the reason, after the execution of this command has been in download what things, after about four or five minutes, began to pack the apk of different channels, and finally the following figure shows:

This is done through the command line, of course, you can also directly through the UI, select Menu Build–>generate signed apk–> choose to create a good key KeyStore (do not create one), and then click Next will pop up the following image of the dialog box:

Here you can also select a channel or build type,flavors to select at least one, click Finish can also be multi-channel packaging.

Sigh: It's been a hassle to use Eclipse's multi-channel packaging, and now it feels good and quick to pack as a multi-channel package.

Second, code confusion

1, the Build.gradle in the buildtypes under the minifyenable to True

Shrinkresources false

The above line of code is to overflow unused unnecessary resource files in order to reduce the size of the final installation package, open in release mode under True,debug does not need to set true, or for the report warnings

2, edit the app directory under the Proguard-rules.pro file as follows:

# ADD Project specific Proguard rules here. 
# By default, the flags in this file are appended to flags specified # in E:\androisdk/tools/proguard/proguard-android.txt
# You can edit the ' include path and order by changing the ' proguardfiles # directive in Build.gradle. # for more details, and # http://developer.android.com/guide/developing/tools/proguard.html # ADD any project specific K EEP options: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class
Name to the JavaScript interface # class: #-keepclassmembers class Fqcn.of.javascript.interface.for.webview {# public *; #} #----------------Universal confusing start----------------------optimizationpasses 5 # Specifies the compression level of the Code-dontusemixedcaseclassnames # Whether or not to use the case Mix-dontpreverify # to make a pre-check-verbose # when confused to log-ignorewarnings # Ignore warnings-optimizations!code/simplification/arit hmetic,!field/*,!class/merging/* # Confusion using the algorithm-keep public class * extends Android.app.Activity # Keep which classes are not confused-keep public cl Ass * EXtends Android.app.Application # Keep which classes are not confused-keep public class * extends Android.app.Service # Keep which classes are not confused-keep public class * Extends Android.content.BroadcastReceiver # Keep What classes are not confused-keep public class * extends Android.content.ContentProvider # Keep Which classes are not confused-keep public class * extends Android.app.backup.BackupAgentHelper # Keep which classes are not confused-keep public class * extends Android . Preference. Preference # Keep which classes are not confused-keep public class Com.android.vending.licensing.ILicensingService # Keep which classes are not confused- Keepclasseswithmembernames class * {# Keep native method not confused native <methods>;}-keepclasseswithmembers class * {# Keep it from the set
The semantic control class is not confused with public <init> (Android.content.Context, Android.util.AttributeSet); -keepclasseswithmembers class * {# Keep custom control class not confused by Public <init> (Android.content.Context,
Android.util.AttributeSet, int); }-keepclassmembers class * extends Android.app.Activity {# Keep the custom control class from being confused with public void * (Android.view.View);-keepclassme Mbers enum * {# Keep enumerated enum class not confused public static **[] values (); pubLIC Static * * valueof (java.lang.String); }-keep class * Implements android.os.Parcelable {# Keep Parcelable not confused public static final Android.os.parcelable$creator
*; #----------------Universal confusing End---------------------#----------------Friend 5.0 confuse start----------------------Dontshrink- Dontoptimize-dontwarn com.google.android.maps.**-dontwarn Android.webkit.webview-dontwarn com.umeng.**-dontwarn com.tencent.weibo.sdk.**-dontwarn com.facebook.**-keep enum com.facebook.**-keepattributes Exceptions,InnerClasses , Signature-keepattributes *annotation*-keepattributes sourcefile,linenumbertable-keep public interface Com.facebook . * *-keep public interface com.tencent.**-keep public interface com.umeng.socialize.**-keep public interface Com.umeng.s  ocialize.sensor.**-keep public Interface com.umeng.scrshot.**-keep public class com.umeng.socialize.* {*;}-keep Public Class javax.**-keep public class android.webkit.**-keep class com.facebook.**-keep class com.facebook.** {*;}-keep Classcom.umeng.scrshot.**-keep public class com.tencent.** {*;}-keep class com.umeng.socialize.sensor.**-keep class Com.ume ng.socialize.handler.**-keep class com.umeng.socialize.handler.*-keep class com.tencent.mm.sdk.modelmsg.WXMediaMessage {*;}-keep class com.tencent.mm.sdk.modelmsg.** implements com.tencent.mm.sdk.modelmsg.wxmediamessage$imediaobject {*;}-keep class Im.yixin.sdk.api.YXMessage {*;}-keep Class im.yixin.sdk.api.** implements im.yixin.sdk.api.yxmessage$yxmessagedata{*;}-dontwarn twitter4j.** class twitter4j.** {*;}-keep class com.tencent.** {*;}-dontwarn com.tencent.**-keep public class com.umeng.soexample.r$*{p
ublic static final int *; -keep public class com.umeng.soexample.r$*{public static final int *;}-keep class com.tencent.open.tdialog$*-keep cl Ass com.tencent.open.tdialog$* {*;}-keep class Com.tencent.open.pkdialog-keep class Com.tencent.open.PKDialog {*;}- Keep class com.tencent.open.pkdialog$*-keep class com.tencent.open.pkdialog$* {*;}
-keep class com.sina.** {*;}-dontwarn com.sina.**-keep class com.alipay.share.sdk.** {*;}-keepnames class * Implemen
TS android.os.Parcelable {public static final * * CREATOR}-keep class com.linkedin.** {*;}-keepattributes Signature #----------------Friend 5.0 confuse End---------------------#----------------The Gold map confuse start----------------------Dontwarn com.amap.api.mapcore2d.** #定位-keep class com.amap.api.location.**{*;}-keep class com.amap.api.fence.**{*;}-keep Class com.autonavi.aps.amapapi.model.**{*;} #搜索-keep class com.amap.api.services.**{*;} #2D地图-keep class com.amap.api.maps2d.**{*}-keep class com.amap.api.mapcore2d.**{*;} #---------------- Gold map confusing End---------------------#----------------Aurora Push confuse start----------------------dontoptimize-dontpreverify- Dontwarn cn.jpush.**-keep class cn.jpush.** {*;} #gson-dontwarn com.google.**-keep class com.google.gson.** {*;} #pro Tobuf-dontwarn com.google.**-keep class com.google.protobuf.** {*;} #----------------Aurora push to confuse end---------------------#----------------Mob SMS Authentication confusing start----------------------keep class Android.net.http.sslerror-keep class android.webkit.**{*}-keep class cn.sharesdk.**{*;}-keep class cn.smssdk.**{*;}-keep class com.mob.**{*; #---------- ------Mob SMS Authentication confusing end---------------------

The code is much, but it's easy to understand, first of all the general confusion must have (refer to the Android Studio implementation Code confusion), the other based on the project added to the Third-party library to the Third-party developer platform to find the confusion they provided to the content can be

A. If the mixing package succeeds, the application runs to report the following error:

That must be because there is no confusing code to add mob SMS validation, because it's not mentioned in their official integration documents, but in the past the forum found the answer, as in the last section of the code above.

B. If the encapsulation of the data is Gson, add confusion, including all JavaBean in the project, the entity class, and the confusing code as follows (refer to the official GitHub description):

# #---------------Begin:proguard configuration for Gson----------# Gson uses generic type information stored in a class File when working with fields.
Proguard # Removes such information by default and so configure it to keep all of it. -keepattributes Signature # Gson specific Classes-keep class Sun.misc.Unsafe {*;} #-keep class com.google.gson.stream.* * {*;} # application classes that'll be serialized/deserialized over Gson #-keep class Com.google.gson.examples.androi  d.model.** {*;} # #---------------End:proguard configuration for Gson-----------keep class com.xxx.xxxxxxxxx.bean.** { *; # Keep entity classes in the project not confused C. If your project integrates the Mpandroidchart chart, then you also need to add confusion, refer to here, the code is as follows:-keep class com.github.mikephil.charting.** {*;} # Make sure Mpandroidchart load animations are available d. If you use WebView in your project and customize Javainterface with JS interaction, you need to add the following confusing code: #---------------- JS confusing start----------------------Keepclassmembers class Com.xxxxx.xxx.xxxactivity$myjavascriptinterface {public *;}- Keepattributes *annotation*-keepattributes *JAVASCRiptinterface* #----------------JS to confuse end--------------------- 

E. If a lot of warnings are reported if a apk failure is generated, you can add this line of code to ignore the warning

-ignorewarnings # Ignore Warning

Wait for the build apk success, to see if the exception, if the run to a certain step of the crash, then according to the specific crash information to add the corresponding confusion rules can be added this sentence is to facilitate debugging.

3, the implementation of packaging can be

4, to pack the proceeds apk with Dex2jar and Jdgui back to compile, look at Java source, found that there are indeed a lot of documents are confused.

The above is a small series to introduce you to the Android Studio multi-channel bulk packaging and code confusion, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.