Turning Proguard On/Off Using Properties

我与影子孤独终老i 提交于 2019-11-27 08:31:23

问题


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?


回答1:


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.




回答2:


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.




回答3:


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'
        }

    }


来源:https://stackoverflow.com/questions/10642030/turning-proguard-on-off-using-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!