Turning Proguard On/Off Using Properties

2019-01-20 15:02发布

In my project.properties file there is a property proguard.config and when i run ant it will run proguard. All of the following will cause proguard to run

 proguard.config
 proguard.config=
 proguard.config=proguard.cfg

Is there a way within the properties file to turn proguard on/off?

Otherwise I am going to need to write a script to add/remove or rename the proguard.config property to control this. I would rather just get/set properties. I want the best solution for this. Right now I am coding replaceregexpr to rename the proguard.config to something else to turn off. Looking for a better solution and would like to know how others are controlling this?

3条回答
走好不送
2楼-- · 2019-01-20 15:21

A lot simpler way is to just have the line

#proguard.config=proguard.cfg

or even better still leave it out altogether! That way proguard doesn't run at all.

查看更多
迷人小祖宗
3楼-- · 2019-01-20 15:31

set this property

minifyEnabled false

in your app build.gradle

Like this to disable in debug and release too:

 buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false 
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }
查看更多
戒情不戒烟
4楼-- · 2019-01-20 15:35

Here is how it is done:

Do NOT set proguard.config=proguard.cfg in your project.properties file.

in your build.properties file set

 proguarded=on    # or whatever variable you chose

Define these Macros if you are building Android so that debugging is always off:

  <macrodef name="set-app-debuggable">
    <sequential>
        <echo>Updating AndroidManifest.xml with debuggable set to true</echo>           
       <replaceregexp file="./AndroidManifest.xml"
                            match='android:debuggable="(.*)"'
                            replace='android:debuggable="true"'
                            byline="false">         
        </replaceregexp>
    </sequential>
</macrodef>
<macrodef name="set-app-not-debuggable">
    <sequential>
        <echo>Updating AndroidManifest.xml with debuggable set to false</echo>          
        <replaceregexp file="./AndroidManifest.xml"
                            match='android:debuggable="(.*)"'
                            replace='android:debuggable="false"'
                            byline="false">         
        </replaceregexp>
    </sequential>
</macrodef>

Then set these conditions or similar to these depending on if you want to preserve debugging outside proguard builds:

<target name="-set-mode-check">
    <echo> set mode checking properties ... </echo>
    <echo> Proguard Property  value is '${proguard.config}' </echo>
    <echo> Proguard Property  value is '${proguarded}' </echo>
    <condition property="proguard.config" value="proguard.cfg">
       <isset property="proguarded"/>
   </condition>
    <echo> Proguard Property  value after condition set is '${proguard.config}' </echo>
     <if condition="${proguarded}">
        <then>
            <echo>****  This build is proguarded so setting debuggable off  ****</echo>
            <set-app-not-debuggable />
        </then>
        <else>
            <echo>****  This build is not proguarded so setting debuggable on ****</echo>
            <set-app-debuggable />
        </else>
    </if>

</target>

No need to set proguard.config in the project.properties file at all.

查看更多
登录 后发表回答