summaryrefslogtreecommitdiff
path: root/src/plugins/android/src/com/motorola/studio/android/obfuscate/ObfuscatorManager.java
blob: 3bfaec2db1af3aa2fbce38deda0ae59785644ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorola.studio.android.obfuscate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.osgi.framework.Bundle;

import com.motorola.studio.android.AndroidPlugin;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.AndroidUtils;

/**
 * Manager to set Proguard files inside project 
 * Manipulates the file on <project_root>/default.properties
 * Copy proguard.cfg
 */
public class ObfuscatorManager
{

    private static final String PROGUARD_PATH = "/files/proguard.cfg";

    private static final String PROGUARD_SDK_PATH = "tools/lib/proguard.cfg";

    private static final String PROGUARD_FILENAME = "proguard.cfg";

    private static final String PROGUARD_CONFIG_STATEMENT = "proguard.config=proguard.cfg";

    private static final String DEFAULT_PROPERTIES_FILENAME = "default.properties";

    private static final String PROJECT_PROPERTIES_FILENAME = "project.properties";

    private static final String NL = System.getProperty("line.separator");

    /**
     * Prepares the project with the Proguard settings 
     * to obfuscate when the project is exported in release mode
     * @param project
     * @param monitor
     * @return
     */
    public static IStatus obfuscate(IProject project, IProgressMonitor monitor)
    {
        IStatus status = Status.OK_STATUS;

        /*
         * Project properties file is the new filename for ADT
         */
        File projectPropertiesFile =
                project.getFile(PROJECT_PROPERTIES_FILENAME).getLocation().toFile();
        File defaultPropertiesFile =
                project.getFile(DEFAULT_PROPERTIES_FILENAME).getLocation().toFile();
        if (projectPropertiesFile.canWrite())
        {
            defaultPropertiesFile = projectPropertiesFile;
        }

        File proguardFile = project.getFile(PROGUARD_FILENAME).getLocation().toFile();

        try
        {
            addProguardLine(defaultPropertiesFile);
            if (!fileExists(proguardFile))
            {
                copyProguardFile(project, monitor);
            }
            try
            {
                project.refreshLocal(IResource.DEPTH_ONE, null);
            }
            catch (CoreException e)
            {
                // Do nothing, user just have to press F5 
            }
        }
        catch (Exception e)
        {
            StudioLogger.error(ObfuscatorManager.class,
                    "Error while setting Proguard to obfuscate", e);
            status =
                    new Status(IStatus.ERROR, AndroidPlugin.PLUGIN_ID,
                            "Could not set Proguard to obfuscate", e);
        }

        StudioLogger.collectUsageData(StudioLogger.WHAT_OBFUSCATE, StudioLogger.KIND_OBFUSCATE,
                StudioLogger.DESCRIPTION_DEFAULT, AndroidPlugin.PLUGIN_ID, AndroidPlugin
                        .getDefault().getBundle().getVersion().toString());

        return status;
    }

    /**
     * Removes Proguard settings 
     * (Project will not be obfuscated when the is exported in release mode)
     * @param project
     * @return
     */
    public static IStatus unobfuscate(IProject project)
    {
        IStatus status = Status.OK_STATUS;

        try
        {
            File projectPropertiesFile =
                    project.getFile(PROJECT_PROPERTIES_FILENAME).getLocation().toFile();
            File defaultPropertiesFile =
                    project.getFile(DEFAULT_PROPERTIES_FILENAME).getLocation().toFile();
            if (isProguardSet(defaultPropertiesFile))
            {
                removeProguardLine(defaultPropertiesFile);
            }
            if (isProguardSet(projectPropertiesFile))
            {
                removeProguardLine(projectPropertiesFile);
            }
            try
            {
                project.refreshLocal(IResource.DEPTH_ONE, null);
            }
            catch (CoreException e)
            {
                // Do nothing, user just have to press F5 
            }
        }
        catch (Exception e)
        {
            StudioLogger
                    .error(ObfuscatorManager.class, "Error while removing Proguard settings", e);
            status =
                    new Status(IStatus.ERROR, AndroidPlugin.PLUGIN_ID,
                            "Could not remove Proguard settings", e);
        }

        StudioLogger.collectUsageData(StudioLogger.WHAT_OBFUSCATE, StudioLogger.KIND_DESOBFUSCATE,
                StudioLogger.DESCRIPTION_DEFAULT, AndroidPlugin.PLUGIN_ID, AndroidPlugin
                        .getDefault().getBundle().getVersion().toString());

        return status;
    }

    /*
     * @param propertiesFile file to write
     * @param newContent content to write (replaces entire file)
     * @throws IOException
     */
    private static void write(File propertiesFile, String newContent) throws IOException
    {
        Writer out = new OutputStreamWriter(new FileOutputStream(propertiesFile));
        try
        {
            out.write(newContent);
        }
        finally
        {
            if (out != null)
            {
                out.close();
            }
        }
    }

    /*
     * @param ignoreProguardStatement true it does not return the line with proguard.config=proguard.cfg 
     * @return
     * @throws IOException
     */
    private static String read(File propertiesFile, boolean ignoreProguardStatement)
            throws IOException
    {
        StringBuilder text = new StringBuilder();
        Scanner scanner = null;
        FileInputStream stream = null;
        try
        {
            stream = new FileInputStream(propertiesFile);
            scanner = new Scanner(stream);
            while (scanner.hasNextLine())
            {
                String line = scanner.nextLine();
                if (!ignoreProguardStatement || !line.contains(PROGUARD_CONFIG_STATEMENT))
                {
                    text.append(line + NL);
                }
            }
        }
        finally
        {
            try
            {
                if (scanner != null)
                {
                    scanner.close();
                }
                if (stream != null)
                {
                    stream.close();
                }
            }
            catch (IOException e)
            {
                StudioLogger
                        .info("Could not close stream while reading obfuscator properties file. "
                                + e.getMessage());
            }
        }
        return text.toString();
    }

    private static boolean fileExists(File proguardFile)
    {
        return (proguardFile != null) && proguardFile.exists();
    }

    /**
     * Checks if default.properties have Proguard statement
     * and if the proguard.cfg exists in the project
     * @param project
     * @return
     */
    public static boolean isProguardSet(IProject project)
    {
        File projectPropertiesFile =
                project.getFile(PROJECT_PROPERTIES_FILENAME).getLocation().toFile();
        File defaultPropertiesFile =
                project.getFile(DEFAULT_PROPERTIES_FILENAME).getLocation().toFile();
        File proguardFile = project.getFile(PROGUARD_FILENAME).getLocation().toFile();

        return (isProguardSet(projectPropertiesFile) || isProguardSet(defaultPropertiesFile))
                && fileExists(proguardFile);
    }

    /**
     * Checks if default.properties have the Proguard statement
     * @param propertiesFile
     * @return
     */
    private static boolean isProguardSet(File propertiesFile)
    {
        String defaultPropertiesContent = null;
        try
        {
            defaultPropertiesContent = read(propertiesFile, false);
        }
        catch (IOException e)
        {
            StudioLogger.error(ObfuscatorManager.class, e.getMessage(), e);
        }
        return ((defaultPropertiesContent != null) && defaultPropertiesContent
                .contains(PROGUARD_CONFIG_STATEMENT));
    }

    /**
     * Add the following line to file 
     * proguard.config=proguard.cfg
     * if it does not exist yet
     * @param project
     * @throws IOException 
     */
    private static void addProguardLine(File propertiesFile) throws IOException
    {
        String currentContent = null;
        currentContent = read(propertiesFile, false);
        if (!currentContent.toString().contains(PROGUARD_CONFIG_STATEMENT))
        {
            String newContent =
                    currentContent.endsWith(NL) ? currentContent + PROGUARD_CONFIG_STATEMENT
                            : currentContent + NL + PROGUARD_CONFIG_STATEMENT;
            write(propertiesFile, newContent);
        }
    }

    /**
     * Remove the following line to file 
     * proguard.config=proguard.cfg
     * if it exists
     * @param project
     * @throws IOException 
     */
    private static void removeProguardLine(File propertiesFile) throws IOException
    {
        String contentWithoutProguardStatement = null;
        contentWithoutProguardStatement = read(propertiesFile, true);
        write(propertiesFile, contentWithoutProguardStatement);
    }

    private static void copyProguardFile(IProject project, IProgressMonitor monitor)
            throws IOException, CoreException
    {
        SubMonitor subMonitor = SubMonitor.convert(monitor);

        URL proguardURL = getProguardFileURL();
        if (proguardURL != null)
        {
            InputStream is = null;
            try
            {
                is = proguardURL.openStream();
                IFile destFile = project.getFile(PROGUARD_FILENAME);
                destFile.create(is, IResource.NONE, subMonitor);
            }
            finally
            {
                if (is != null)
                {
                    is.close();
                }
            }
        }
    }

    /**
     * Get the most suitable proguard file, either from SDK or our plug-in
     * @return the URL of the best proguard file found
     */
    private static URL getProguardFileURL()
    {
        String sdkPath = AndroidUtils.getSDKPathByPreference();
        File sdkPathFile = sdkPath.isEmpty() ? new File(sdkPath) : null;
        File proguardFromSDK = null;
        if (sdkPathFile != null)
        {
            proguardFromSDK = new File(sdkPathFile, PROGUARD_SDK_PATH);
        }

        URL fileURL = null;
        // copy newest proguard file from SDK
        if ((proguardFromSDK != null) && proguardFromSDK.canRead())
        {
            try
            {
                fileURL = proguardFromSDK.toURI().toURL();
            }
            catch (MalformedURLException e)
            {
                StudioLogger.warn(ObfuscatorManager.class,
                        "Exception converting proguard template file to URL", e);
            }
        }
        //copy file bundled with android plugin
        else
        {
            Bundle bundle = AndroidPlugin.getDefault().getBundle();
            fileURL = bundle.getEntry(PROGUARD_PATH);
        }

        return fileURL;
    }
}