Androidstudio Confusing packaging

Source: Internet
Author: User

Projects in Androidstudio can be introduced into open source projects on GitHub in the form of compile, referencing module, not necessarily in the form of adding jar packages to libs folders. When the final realease is packaged, the confusing settings are more important, otherwise the resulting package will have many features that are not working properly. Below is a list of basic app Build.gradle and confusing files. And note that confusing the packaged apk file will be less confusing than not using it.

Set the Build.gradle and obfuscation files in the app root and then write the obfuscation option to the obfuscation file. Although some module may be referenced in the app project, these module projects can not define their own confusion, my project directory structure is as follows:

Where the app is the main module that references the remaining module, the Build.gradle used by the referenced module are not enabled for confusion by using the default settings:

    Buildtypes {        Release {            minifyenabled false            proguardfiles getdefaultproguardfile (' Proguard-android.txt ') ), ' Proguard-rules.pro '        }    }

In the home directory of the Build.gradle:

Apply plugin: ' Com.android.application ' def releasetime () {return new Date (). Format ("Yyyy-mm-dd", Timezone.gettimezone ("UTC"))} Repositories {maven {URL ' https://oss.sonatype.org/content/repositories/snapshots '}}android {compilesd        Kversion buildtoolsversion ' 22.0.1 ' defaultconfig {applicationid "com.*****.****"//own ApplicationID Minsdkversion targetsdkversion Versioncode 1 Versionname "1.0"}//Perform lint check with any errors or warnings    Prompt, the build is terminated, and we can turn it off. lintoptions {Abortonerror false}//signature Signingconfigs {debug {} release {//own signature text Piece StoreFile file ("Doc/****.jks") storepassword "******" keyalias "******" key Password "******"}} buildtypes {debug {//Display log Buildconfigfield "Boolean", "Log_debug", "true" Versionnamesuffix "-debug" minifyenabled false zipalignenabled False shrinkresources false Signingconfig Signingconfigs.debug} release {            Do not display LOG Buildconfigfield "boolean", "Log_debug", "false"//obfuscation minifyenabled True            Zipalign optimization zipalignenabled true//Remove useless resource file shrinkresources true The previous section represents the system default of the Android program to confuse the file, the file already contains the basic obfuscation statement, and the latter file is its own definition to confuse the file Proguardfiles getdefaultproguardfile (' Proguard-an Droid.txt '), ' proguard-rules.pro '//signature signingconfig signingconfigs.release Applicationva Riants.all {variant-variant.outputs.each {output, def outputFile = OUTPUT.O                        Utputfile if (outputFile! = null && outputFile.name.endsWith ('. apk ')) { The output apk name is called app_v1.0_2015-xx-xx_xxxx.apk//def fileName = "App_v${defaultconfig.versionname}_${relea          Setime ()}.apk "              def fileName = "app_v${defaultconfig.versionname}_${variant.productflavors[0].name}.apk"    Output.outputfile = new File (outputfile.parent, FileName)}}}        }//channel flavors, configure different styles of app, friends channel statistics with productflavors {"offical" {} "Yingyongbao" {}    Wandoujia {} baidu {} "}}dependencies {}" {Compile Filetree (dir: ' Libs ', include: [' *.jar '])    Compile project (': Appsdk ') compile ' com.jakewharton:butterknife:7.0.1 '//compile ' com.faradaj:blur-behind:1.1 '    Compile ' com.github.ksoichiro:android-observablescrollview:1.6.0 ' compile ' com.nineoldandroids:library:2.4.0 ' Compile ' in.srain.cube:ultra-ptr:1.0.10 ' compile ' com.android.support:cardview-v7:22.1.0 ' Compile project (': Multi-i Mage-selector ')}
The project's obfuscation options are defined in the Proguard-rules.pro file, defined as follows:
# ADD Project specific Proguard rules here.# by default, the "flags in" This file is appended to the flags specified# in D:\And roid\sdk/tools/proguard/proguard-android.txt# You can edit the include path and order by changing the proguardfiles# Direc tive in build.gradle.## for more details, see# http://developer.android.com/guide/developing/tools/proguard.html# Add A NY Project specific keep options here:# If your project uses WebView with JS, uncomment the following# and specify the ful LY qualified class name to the JavaScript interface# Class:-keepclassmembers class Fqcn.of.javascript.interface.for.webview {public *;} #指定代码的压缩级别-optimizationpasses AA Baoming do not mix case-dontusemixedcaseclassnames# do not ignore non-public library class-dontskipnonpubliclibraryclasses # Optimizes the class file that does not optimize the input-dontoptimize #预校验-dontpreverify the algorithm used to confuse the #混淆时是否记录日志-verbose #-optimizations!code/simplification/ arithmetic,!field/*,!class/merging/* #保护注解-keepattributes *annotation*# Keep What classes are not confused-keep public class * extends Android.app.fragment-keep public class * EXtends Android.app.activity-keep public class * extends Android.app.application-keep public class * extends ANDROID.APP.S Ervice-keep public class * extends Android.content.broadcastreceiver-keep public class * extends Android.content.contentprovider-keep public class * extends Android.app.backup.backupagenthelper-keep public class * Extends Android.preference.preference-keep public class com.android.vending.licensing.ilicensingservice# If you have a reference V4 package you can add the following line-keep public class * extends android.support.v4.app.fragment# ignore warning-ignorewarning# #记录生成的日志数据, gradle Build at the root of this project output # # #apk package inside all class internal structure-dump proguard/class_files.txt# not confusing classes and members-printseeds proguard/seeds.txt# list from apk The code removed in-printusage proguard/unused.txt# before and after the mapping-printmapping proguard/mapping.txt####### #记录生成的日志数据, gradle Build In this project root output-end###### #如果引用了v4或者v7包-dontwarn android.support.**### #混淆保护自己项目的部分代码以及引用的第三方jar包library-end#### #保持 Native method is not confused-keepclasseswithmembernames class * {native <methods>;} #保持自定义控件类不被混淆-keepclasseswithmembeRS class * {public <init> (Android.content.Context, android.util.AttributeSet);} #保持自定义控件类不被混淆-keepclassmembers class * extends android.app.Activity {public void * (Android.view.View);}    -keep public class * extends Android.view.View {public <init> (android.content.Context);    Public <init> (Android.content.Context, Android.util.AttributeSet);    Public <init> (Android.content.Context, Android.util.AttributeSet, int.); public void set* (...);} #保持 parcelable not be confused-keep class * implements android.os.Parcelable {public static final Android.os.parcelable$creator *;} #保持 Serializable not be confused-keepnames class * Implements java.io.serializable# keep Serializable not be confused and enum    Class is also not confused-keepclassmembers class * Implements Java.io.Serializable {static final long serialversionuid;    private static final java.io.objectstreamfield[] Serialpersistentfields;    !static!transient <fields>;    !private <fields>;    !private <methods>; private void WriteObject(Java.io.ObjectOutputStream);    private void ReadObject (Java.io.ObjectInputStream);    Java.lang.Object Writereplace (); Java.lang.Object readresolve ();}  #保持枚举 enum class is not confused-keepclassmembers enum * {public static **[] values (); public static * * VALUEOF (java.lang.String);} -keepclassmembers class * {public void *buttonclicked (Android.view.View);} #不混淆资源类-keepclassmembers class * *. r$* {public static <fields>;} #避免混淆泛型 If the confusion of the error is recommended to turn off #-keepattributes signature# Remove the log class to print each level of the code, the official package can be used as a forbidden log, which can be used as a forbidden log printing function, Another implementation is to control the #-assumenosideeffects class Android.util.Log {# public static * * * V (...) through buildconfig.debug variables; # public static * * * I (...); # public static * * * * d (...); # public static * * * W (...); # public static * * * e (...); #}############################################################################################################# ######## above General ################################################################################## #################################################################### Common third-party module obfuscation options ################################### #gso n# if using the Gson parsing package, add the following lines directly can be successfully confused, or will error. -keepattributes signature# Gson Specific Classes-keep class Sun.misc.Unsafe {*;} # application classes that'll be serialized/deserialized over Gson-keep class com.google.gson.** {*;} -keep class com.google.gson.stream.** {*;} #mob-keep class Android.net.http.sslerror-keep class android.webkit.**{*;} -keep class cn.sharesdk.**{*;} -keep class com.sina.**{*;} -keep class m.framework.**{*;} -keep class * *. r$* {*;} -keep class * *. r{*;} -dontwarn Cn.sharesdk.**-dontwarn * *. r$* #butterknife-keep class butterknife.** {*;} -dontwarn Butterknife.internal.**-keep class **$ $ViewBinder {*;} -keepclasseswithmembernames class * {@butterknife. * &LT;FIELDS&GT;;} -keepclasseswithmembernames class * {@butterknife. * &LT;METHODS&GT;;} ##### #引用的其他Module可以直接在app的这个混淆文件里配置 # If you use a tool such as Gson to make the JavaBean class that it parses, that is, the entity class is not confused. -keep class Com.matrix.app.entity. json.** {*;} -keep class com.matrix.appsdk.network.model.** {*;} # # # # # # # # #混淆保护自己项目的部分代码以及引用的第三方jar包library ####### #如果在当前的application module or a dependent library module uses third-party libraries and does not need to explicitly add rules #- Libraryjars xxx# adds the possibility of encountering the same jar multiple specified errors at the time of packaging, generally simply adding a declaration that ignores the warning and keeps certain classes from being confused. #以libaray的形式引用了开源项目, if you do not want to confuse keep, set the Minifyenabled=false-keep class com.nineoldandroids.** in the Build.gradle of the introduced module { *; }-keep interface com.nineoldandroids.** {*;} -dontwarn com.nineoldandroids.**# drop-down refresh-keep class in.srain.cube.** {*;} -keep interface in.srain.cube.** {*;} -dontwarn in.srain.cube.**# Observablescrollview:tab Fragment-keep class com.github.ksoichiro.** {*;} -keep interface com.github.ksoichiro.** {*;} -dontwarn com.github.ksoichiro.**

Then do the build release package, will be in the app directory to generate a Proguard folder, there is confusion before and after the file mapping information and other text files, the bug is positioned on it:

Androidstudio Confusing packaging

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.