Enable Proguard in Android application

10

I did an example project, using SQLCipher , and I'm trying to obfuscate the code with Proguard , however, after exporting and signing the application, and converting classes.dex with dex2jar , I can practically see all code with Java Decompiler , because the code was not obfuscated. I'd like to know what I'm doing wrong in setting Proguard . Here's the setting for proguard-project.txt:

 -injars      bin/classes
 -injars      libs
 -outjars     bin/classes-processed.jar
 -libraryjars C:/adt-bundle-windows-x86_64-20140702/sdk/platforms/android-        13/android.jar

-optimizations  !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 5
-allowaccessmodification
-dontpreverify
-renamesourcefileattribute SourceFile
-keepattributes *Annotation* 
-keepattributes SourceFile,LineNumberTable,Signature
 # Obfuscation
-repackageclasses ''
-flattenpackagehierarchy ''


-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-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*(...);
}

-keepclasseswithmembers class * {
 public <init>(android.content.Context, android.util.AttributeSet);
 }

-keepclasseswithmembers class * {
 public <init>(android.content.Context, android.util.AttributeSet, int);
 }

-keepclassmembers class * implements android.os.Parcelable {
 static android.os.Parcelable$Creator CREATOR;
 }

 -keepclassmembers class **.R$* {
  public static <fields>;
  }

 -keep public class net.sqlcipher.** {
  *;
  }

-keep public class net.sqlcipher.database.** {
   *;
 }
    
asked by anonymous 19.05.2015 / 13:22

2 answers

1

Fabio, it is possible that your proguard-project.txt file is not being applied to the project. If you are using Android Studio and Gradle take a look at build.grad, look for input proguardFiles and make sure your file is listed there. For more detailed instructions refer to it here: link

    
26.06.2015 / 19:49
0

Original code:

package com.example.sqlcipher_exemplo;

import java.io.File;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabaseHook;
import android.app.Activity;
import android.content.ContentValues;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity 
{

    EventDataSQLHelper eventsData;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if ("google_sdk".equals( Build.PRODUCT ) || Build.FINGERPRINT.startsWith("generic")) 
        {

               Log.i("EMULADOR", "rodando no emulador");

        } 
        else 
        {
               Log.i("DISPOSITIVO", "rodando no dispositivo");
        }

        SQLiteDatabase.loadLibs(this);
        String password = "123";
        eventsData = new EventDataSQLHelper(this);
        SQLiteDatabase db = eventsData.getWritableDatabase(password);   
        for(int i=1; i<100; i++)
        {
            addEvent("Hello Android Event "+i, db); 
        }
        db.close();
        db = eventsData.getReadableDatabase(password);
        Cursor cursor = getEvents(db);
        showEvents(cursor);
        db.close();
    }

    private void addEvent (String title, SQLiteDatabase db)
    {
        ContentValues values = new ContentValues();
        values.put(EventDataSQLHelper.TIME, System.currentTimeMillis());
        values.put(EventDataSQLHelper.TITLE, title);
        db.insert(EventDataSQLHelper.TABLE, null, values);
    }

    private Cursor getEvents(SQLiteDatabase db)
    {
        Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null, null, null);

        startManagingCursor(cursor);
        return cursor;
    }

    private void showEvents(Cursor cursor)
    {
        StringBuilder ret = new StringBuilder("Saved Events:\n\n");
        while (cursor.moveToNext())
        {
            long id = cursor.getLong(0);
            long time = cursor.getLong(1);
            String title = cursor.getString(2);
            ret.append(id+": "+time+":"+title+"\n");
        }
        Log.i("sqldemo", ret.toString());
    }

    /*
    private void decryptDatabase() 
    {
          File unencryptedFile = getDatabasePath("events.db");
          unencryptedFile.delete();
          File databaseFile = getDatabasePath("events.db");
          SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
           public void preKey(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase
              .rawExecSQL("PRAGMA cipher_default_use_hmac = off;");
           }

           public void postKey(SQLiteDatabase sqLiteDatabase) 
           {

           }
          };
          SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
            databaseFile, "123", null, hook); // Exception 
          if (database.isOpen()) 
          {
           database.rawExecSQL(String.format(
             "ATTACH DATABASE '%s' as plaintext KEY '';",
             unencryptedFile.getAbsolutePath()));
           database.rawExecSQL("SELECT sqlcipher_export('plaintext');");
           database.rawExecSQL("DETACH DATABASE plaintext;");
           android.database.sqlite.SQLiteDatabase sqlDB = android.database.sqlite.SQLiteDatabase
             .openOrCreateDatabase(unencryptedFile, null);
           sqlDB.close();
           database.close();
          }

          databaseFile.delete();
    }*/
}

Code after Proguard:

package com.example.sqlcipher_exemplo;

import android.app.Activity;
import android.content.ContentValues;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

public class MainActivity
  extends Activity
{
  a a;

  private Cursor a(SQLiteDatabase paramSQLiteDatabase)
  {
    paramSQLiteDatabase = paramSQLiteDatabase.query("events", null, null, null, null, null, null);
    startManagingCursor(paramSQLiteDatabase);
    return paramSQLiteDatabase;
  }

  private void a(String paramString, SQLiteDatabase paramSQLiteDatabase)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("time", Long.valueOf(System.currentTimeMillis()));
    localContentValues.put("title", paramString);
    paramSQLiteDatabase.insert("events", null, localContentValues);
  }

  private void a(Cursor paramCursor)
  {
    StringBuilder localStringBuilder = new StringBuilder("Saved Events:\n\n");
    for (;;)
    {
      if (!paramCursor.moveToNext())
      {
        Log.i("sqldemo", localStringBuilder.toString());
        return;
      }
      long l1 = paramCursor.getLong(0);
      long l2 = paramCursor.getLong(1);
      String str = paramCursor.getString(2);
      localStringBuilder.append(l1 + ": " + l2 + ":" + str + "\n");
    }
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(2130903040);
    int i;
    if (("google_sdk".equals(Build.PRODUCT)) || (Build.FINGERPRINT.startsWith("generic")))
    {
      Log.i("EMULADOR", "rodando no emulador");
      SQLiteDatabase.loadLibs(this);
      this.a = new a(this);
      paramBundle = this.a.getWritableDatabase("123");
      i = 1;
    }
    for (;;)
    {
      if (i >= 100)
      {
        paramBundle.close();
        paramBundle = this.a.getReadableDatabase("123");
        a(a(paramBundle));
        paramBundle.close();
        return;
        Log.i("DISPOSITIVO", "rodando no dispositivo");
        break;
      }
      a("Hello Android Event " + i, paramBundle);
      i += 1;
    }
  }
}
    
31.07.2015 / 17:51