diff options
Diffstat (limited to 'examples/android-proguard-example')
9 files changed, 365 insertions, 0 deletions
diff --git a/examples/android-proguard-example/AndroidManifest.xml b/examples/android-proguard-example/AndroidManifest.xml new file mode 100755 index 00000000..7e9b1d8b --- /dev/null +++ b/examples/android-proguard-example/AndroidManifest.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.google.gson.examples.android" + android:versionCode="1" + android:versionName="1.0"> + <uses-sdk android:minSdkVersion="3"/> + <application android:icon="@drawable/icon" android:label="@string/app_name"> + <activity android:name=".GsonProguardExampleActivity" + android:label="@string/app_name" + android:exported="true" + android:icon="@drawable/icon" + android:configChanges="keyboardHidden|orientation" + android:enabled="true"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> + <uses-permission android:name="android.permission.INTERNET" /> +</manifest> diff --git a/examples/android-proguard-example/default.properties b/examples/android-proguard-example/default.properties new file mode 100644 index 00000000..7d4fed0b --- /dev/null +++ b/examples/android-proguard-example/default.properties @@ -0,0 +1,12 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system use, +# "build.properties", and override values to adapt the script to your +# project structure. + +# Project target. +target=android-3 +proguard.config=proguard.cfg
\ No newline at end of file diff --git a/examples/android-proguard-example/proguard.cfg b/examples/android-proguard-example/proguard.cfg new file mode 100644 index 00000000..9bdaa484 --- /dev/null +++ b/examples/android-proguard-example/proguard.cfg @@ -0,0 +1,90 @@ +##---------------Begin: proguard configuration common for all Android apps ---------- +-optimizationpasses 5 +-dontusemixedcaseclassnames +-dontskipnonpubliclibraryclasses +-dontskipnonpubliclibraryclassmembers +-dontpreverify +-verbose +-dump class_files.txt +-printseeds seeds.txt +-printusage unused.txt +-printmapping mapping.txt +-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* + +-allowaccessmodification +-keepattributes *Annotation* +-renamesourcefileattribute SourceFile +-keepattributes SourceFile,LineNumberTable +-repackageclasses '' + +-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.app.backup.BackupAgentHelper +-keep public class * extends android.preference.Preference +-keep public class com.android.vending.licensing.ILicensingService +-dontnote com.android.vending.licensing.ILicensingService + +# Explicitly preserve all serialization members. The Serializable interface +# is only a marker interface, so it wouldn't save them. +-keepclassmembers class * implements java.io.Serializable { + static final long serialVersionUID; + private static final java.io.ObjectStreamField[] serialPersistentFields; + private void writeObject(java.io.ObjectOutputStream); + private void readObject(java.io.ObjectInputStream); + java.lang.Object writeReplace(); + java.lang.Object readResolve(); +} + +# Preserve all native method names and the names of their classes. +-keepclasseswithmembernames class * { + native <methods>; +} + +-keepclasseswithmembernames class * { + public <init>(android.content.Context, android.util.AttributeSet); +} + +-keepclasseswithmembernames class * { + public <init>(android.content.Context, android.util.AttributeSet, int); +} + +# Preserve static fields of inner classes of R classes that might be accessed +# through introspection. +-keepclassmembers class **.R$* { + public static <fields>; +} + +# Preserve the special static methods that are required in all enumeration classes. +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +-keep public class * { + public protected *; +} + +-keep class * implements android.os.Parcelable { + public static final android.os.Parcelable$Creator *; +} +##---------------End: proguard configuration common for all Android apps ---------- + +##---------------Begin: proguard configuration for Gson ---------- +# Gson uses generic type information stored in a class file when working with fields. Proguard +# removes such information by default, so configure it to keep all of it. +-keepattributes Signature + +# For using GSON @Expose annotation +-keepattributes *Annotation* + +# Gson specific classes +-keep class sun.misc.Unsafe { *; } +#-keep class com.google.gson.stream.** { *; } + +# Application classes that will be serialized/deserialized over Gson +-keep class com.google.gson.examples.android.model.** { *; } + +##---------------End: proguard configuration for Gson ---------- diff --git a/examples/android-proguard-example/res/drawable/icon.png b/examples/android-proguard-example/res/drawable/icon.png Binary files differnew file mode 100644 index 00000000..a07c69fa --- /dev/null +++ b/examples/android-proguard-example/res/drawable/icon.png diff --git a/examples/android-proguard-example/res/layout/main.xml b/examples/android-proguard-example/res/layout/main.xml new file mode 100644 index 00000000..0ac46e68 --- /dev/null +++ b/examples/android-proguard-example/res/layout/main.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> + +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + + <TextView android:id="@+id/tv" + android:layout_width="fill_parent" + android:layout_height="wrap_content" /> +</LinearLayout>
\ No newline at end of file diff --git a/examples/android-proguard-example/res/values/strings.xml b/examples/android-proguard-example/res/values/strings.xml new file mode 100644 index 00000000..ba3be81e --- /dev/null +++ b/examples/android-proguard-example/res/values/strings.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="app_name">Gson Proguard Example</string> +</resources> + diff --git a/examples/android-proguard-example/src/com/google/gson/examples/android/GsonProguardExampleActivity.java b/examples/android-proguard-example/src/com/google/gson/examples/android/GsonProguardExampleActivity.java new file mode 100644 index 00000000..bd544311 --- /dev/null +++ b/examples/android-proguard-example/src/com/google/gson/examples/android/GsonProguardExampleActivity.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * 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.google.gson.examples.android; + +import java.util.ArrayList; +import java.util.List; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.TextView; + +import com.google.gson.Gson; +import com.google.gson.examples.android.model.Cart; +import com.google.gson.examples.android.model.LineItem; + +/** + * Activity class illustrating how to use proguard with Gson + * + * @author Inderjeet Singh + */ +public class GsonProguardExampleActivity extends Activity { + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setContentView(R.layout.main); + TextView tv = (TextView) findViewById(R.id.tv); + Gson gson = new Gson(); + Cart cart = buildCart(); + StringBuilder sb = new StringBuilder(); + sb.append("Gson.toJson() example: \n"); + sb.append(" Cart Object: ").append(cart).append("\n"); + sb.append(" Cart JSON: ").append(gson.toJson(cart)).append("\n"); + sb.append("\n\nGson.fromJson() example: \n"); + String json = "{buyer:'Happy Camper',creditCard:'4111-1111-1111-1111'," + + "lineItems:[{name:'nails',priceInMicros:100000,quantity:100,currencyCode:'USD'}]}"; + sb.append("Cart JSON: ").append(json).append("\n"); + sb.append("Cart Object: ").append(gson.fromJson(json, Cart.class)).append("\n"); + tv.setText(sb.toString()); + tv.invalidate(); + } + + private Cart buildCart() { + List<LineItem> lineItems = new ArrayList<LineItem>(); + lineItems.add(new LineItem("hammer", 1, 12000000, "USD")); + return new Cart(lineItems, "Happy Buyer", "4111-1111-1111-1111"); + } +} diff --git a/examples/android-proguard-example/src/com/google/gson/examples/android/model/Cart.java b/examples/android-proguard-example/src/com/google/gson/examples/android/model/Cart.java new file mode 100644 index 00000000..7582036e --- /dev/null +++ b/examples/android-proguard-example/src/com/google/gson/examples/android/model/Cart.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * 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.google.gson.examples.android.model; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; +import java.util.List; + +import com.google.gson.annotations.SerializedName; + +/** + * A model object representing a cart that can be posted to an order-processing server + * + * @author Inderjeet Singh + */ +public class Cart { + public final List<LineItem> lineItems; + + @SerializedName("buyer") + private final String buyerName; + + private final String creditCard; + + public Cart(List<LineItem> lineItems, String buyerName, String creditCard) { + this.lineItems = lineItems; + this.buyerName = buyerName; + this.creditCard = creditCard; + } + + public List<LineItem> getLineItems() { + return lineItems; + } + + public String getBuyerName() { + return buyerName; + } + + public String getCreditCard() { + return creditCard; + } + + @Override + public String toString() { + StringBuilder itemsText = new StringBuilder(); + boolean first = true; + if (lineItems != null) { + try { + Class<?> fieldType = Cart.class.getField("lineItems").getType(); + System.out.println("LineItems CLASS: " + getSimpleTypeName(fieldType)); + } catch (SecurityException e) { + } catch (NoSuchFieldException e) { + } + for (LineItem item : lineItems) { + if (first) { + first = false; + } else { + itemsText.append("; "); + } + itemsText.append(item); + } + } + return "[BUYER: " + buyerName + "; CC: " + creditCard + "; " + + "LINE_ITEMS: " + itemsText.toString() + "]"; + } + + @SuppressWarnings("unchecked") + public static String getSimpleTypeName(Type type) { + if (type == null) { + return "null"; + } + if (type instanceof Class) { + return ((Class)type).getSimpleName(); + } else if (type instanceof ParameterizedType) { + ParameterizedType pType = (ParameterizedType) type; + StringBuilder sb = new StringBuilder(getSimpleTypeName(pType.getRawType())); + sb.append('<'); + boolean first = true; + for (Type argumentType : pType.getActualTypeArguments()) { + if (first) { + first = false; + } else { + sb.append(','); + } + sb.append(getSimpleTypeName(argumentType)); + } + sb.append('>'); + return sb.toString(); + } else if (type instanceof WildcardType) { + return "?"; + } + return type.toString(); + } + +} diff --git a/examples/android-proguard-example/src/com/google/gson/examples/android/model/LineItem.java b/examples/android-proguard-example/src/com/google/gson/examples/android/model/LineItem.java new file mode 100644 index 00000000..5245f448 --- /dev/null +++ b/examples/android-proguard-example/src/com/google/gson/examples/android/model/LineItem.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * 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.google.gson.examples.android.model; + +/** + * A line item in a cart. This is not a rest resource, just a dependent object + * + * @author Inderjeet Singh + */ +public class LineItem { + private final String name; + private final int quantity; + private final long priceInMicros; + private final String currencyCode; + + public LineItem(String name, int quantity, long priceInMicros, String currencyCode) { + this.name = name; + this.quantity = quantity; + this.priceInMicros = priceInMicros; + this.currencyCode = currencyCode; + } + + public String getName() { + return name; + } + + public int getQuantity() { + return quantity; + } + + public long getPriceInMicros() { + return priceInMicros; + } + + public String getCurrencyCode() { + return currencyCode; + } + + @Override + public String toString() { + return String.format("(item: %s, qty: %s, price: %.2f %s)", + name, quantity, priceInMicros/(double)1000000, currencyCode); + } +} |