Skip to content

Commit

Permalink
optimz cronet proguard
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackforid committed Apr 16, 2018
1 parent f22fbd3 commit 21ab65f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 113 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
}

debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro', 'proguard-rules-cronet.pro'

}
}
Expand Down
66 changes: 66 additions & 0 deletions app/proguard-rules-cronet.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Proguard config for apps that depend on cronet_impl_native_java.jar.

# This constructor is called using the reflection from Cronet API (cronet_api.jar).
-keep class org.chromium.net.impl.NativeCronetProvider {
public <init>(android.content.Context);
}

# Suppress unnecessary warnings.
-dontnote org.chromium.net.ProxyChangeListener$ProxyReceiver
-dontnote org.chromium.net.AndroidKeyStore
# Needs 'void setTextAppearance(int)' (API level 23).
-dontwarn org.chromium.base.ApiCompatibilityUtils
# Needs 'boolean onSearchRequested(android.view.SearchEvent)' (API level 23).
-dontwarn org.chromium.base.WindowCallbackWrapper

# Generated for chrome apk and not included into cronet.
-dontwarn org.chromium.base.library_loader.NativeLibraries
-dontwarn org.chromium.base.multidex.ChromiumMultiDexInstaller
-dontwarn org.chromium.base.metrics.CachedMetrics
-dontwarn org.chromium.base.library_loader.LibraryLoader
-dontwarn org.chromium.base.SysUtils

# Objects of this type are passed around by native code, but the class
# is never used directly by native code. Since the class is not loaded, it does
# not need to be preserved as an entry point.
-dontnote org.chromium.net.UrlRequest$ResponseHeadersMap

# This class should be explicitly kept to avoid failure if
# class/merging/horizontal proguard optimization is enabled.
-keep class org.chromium.base.CollectionUtil


# Proguard config for apps that depend on cronet_impl_platform_java.jar.

# This constructor is called using the reflection from Cronet API (cronet_api.jar).
-keep class org.chromium.net.impl.JavaCronetProvider {
public <init>(android.content.Context);
}

-keepattributes Signature,InnerClasses,SourceFile,LineNumberTable
-dontwarn io.netty.**
-keep class io.netty.** { *; }

# Do not obfuscate this class for testing since some of the tests check the class
# name in order to check that an instantiated engine is the Java one.
-keepnames class org.chromium.net.impl.JavaCronetEngine


# Generated for chrome apk and not included into cronet.
-dontwarn org.chromium.base.library_loader.NativeLibraries

#-keep class org.chromium.base.AnimationFrameTimeHistogram
#-keep class org.chromium.base.metrics.RecordUserAction
#-keep class org.chromium.base.JNIUtils

-keep class org.chromium.** {
native <methods>;
}

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keepclasseswithmembers class * {
@org.chromium.base.annotations.CalledByNative <methods>;
}

83 changes: 0 additions & 83 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -36,86 +36,3 @@
-keepattributes Exceptions


# 删除代码中Log相关的代码
-assumenosideeffects class android.util.Log {
public static boolean isLoggable(java.lang.String, int);
public static int v(...);
public static int i(...);
public static int w(...);
public static int d(...);
public static int e(...);
}

# Proguard config for apps that depend on cronet_impl_native_java.jar.

# This constructor is called using the reflection from Cronet API (cronet_api.jar).
-keep class org.chromium.net.impl.NativeCronetProvider {
public <init>(android.content.Context);
}

# Suppress unnecessary warnings.
-dontnote org.chromium.net.ProxyChangeListener$ProxyReceiver
-dontnote org.chromium.net.AndroidKeyStore
# Needs 'void setTextAppearance(int)' (API level 23).
-dontwarn org.chromium.base.ApiCompatibilityUtils
# Needs 'boolean onSearchRequested(android.view.SearchEvent)' (API level 23).
-dontwarn org.chromium.base.WindowCallbackWrapper

# Generated for chrome apk and not included into cronet.
-dontwarn org.chromium.base.library_loader.NativeLibraries
-dontwarn org.chromium.base.multidex.ChromiumMultiDexInstaller
-dontwarn org.chromium.base.metrics.CachedMetrics
-dontwarn org.chromium.base.library_loader.LibraryLoader
-dontwarn org.chromium.base.SysUtils

# Objects of this type are passed around by native code, but the class
# is never used directly by native code. Since the class is not loaded, it does
# not need to be preserved as an entry point.
-dontnote org.chromium.net.UrlRequest$ResponseHeadersMap
# https://android.googlesource.com/platform/sdk/+/marshmallow-mr1-release/files/proguard-android.txt#54
-dontwarn android.support.**

# This class should be explicitly kept to avoid failure if
# class/merging/horizontal proguard optimization is enabled.
-keep class org.chromium.base.CollectionUtil



# Proguard config for apps that depend on cronet_impl_platform_java.jar.

# This constructor is called using the reflection from Cronet API (cronet_api.jar).
-keep class org.chromium.net.impl.JavaCronetProvider {
public <init>(android.content.Context);
}

-keepattributes Signature,InnerClasses,SourceFile,LineNumberTable
-dontwarn io.netty.**
-keep class io.netty.** { *; }
# Keep ChromiumNativeTestSupport & ChromiumPlatformOnlyTestSupport since they are
# instantiated through Reflection by the smoke tests.
-keep class org.chromium.net.smoke.ChromiumNativeTestSupport
-keep class org.chromium.net.smoke.ChromiumPlatformOnlyTestSupport

# https://android.googlesource.com/platform/sdk/+/marshmallow-mr1-release/files/proguard-android.txt#54
-dontwarn android.support.**

# Do not obfuscate this class for testing since some of the tests check the class
# name in order to check that an instantiated engine is the Java one.
-keepnames class org.chromium.net.impl.JavaCronetEngine

# These classes should be explicitly kept to avoid failure if
# class/merging/horizontal proguard optimization is enabled.
# NOTE: make sure that only test classes are added to this list.
-keep class org.chromium.base.test.** {
*;
}

-keep class org.chromium.net.TestFilesInstaller
-keep class org.chromium.net.MetricsTestUtil

# Generated for chrome apk and not included into cronet.
-dontwarn org.chromium.base.library_loader.NativeLibraries


-keep class org.chromium.** {*;}
-dontwarn org.chromium.**
59 changes: 31 additions & 28 deletions app/src/main/java/com/smilehacker/quictest/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import android.os.AsyncTask
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Button
import android.widget.TextView
import com.smilehacker.quicdroid.QUICDroid
import com.smilehacker.quicdroid.QUICInterceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okio.Okio
import org.chromium.net.CronetEngine
import java.io.File
import java.net.HttpURLConnection
import java.net.URL
import java.util.concurrent.TimeUnit


class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -60,34 +63,34 @@ class MainActivity : AppCompatActivity() {

QUICDroid.init(this@MainActivity)
//
// mEngine = CronetEngine.Builder(applicationContext)
// .enableQuic(true)
// .build()
//
// mOkhttpClentWithQUIC = OkHttpClient.Builder()
// .readTimeout(30, TimeUnit.SECONDS)
// .writeTimeout(30, TimeUnit.SECONDS)
// .connectTimeout(30, TimeUnit.SECONDS)
// .cache(null)
// .addInterceptor(QUICInterceptor())
// .build()
// mOkhttpClent = OkHttpClient.Builder()
// .readTimeout(30, TimeUnit.SECONDS)
// .writeTimeout(30, TimeUnit.SECONDS)
// .connectTimeout(30, TimeUnit.SECONDS)
// .cache(null)
// .build()
//
// findViewById<Button>(R.id.btn_quic_test).setOnClickListener {
// doTest(TEST_TYPE_QUIC)
// }
//
// findViewById<Button>(R.id.btn_http_test).setOnClickListener {
// doTest(TEST_TYPE_HTTP)
// }
// findViewById<Button>(R.id.btn_quic_ok_test).setOnClickListener {
// doTest(TEST_TYPE_QUIC_OVER_OKHTTP)
// }
mEngine = CronetEngine.Builder(applicationContext)
.enableQuic(true)
.build()

mOkhttpClentWithQUIC = OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.cache(null)
.addInterceptor(QUICInterceptor())
.build()
mOkhttpClent = OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.cache(null)
.build()

findViewById<Button>(R.id.btn_quic_test).setOnClickListener {
doTest(TEST_TYPE_QUIC)
}

findViewById<Button>(R.id.btn_http_test).setOnClickListener {
doTest(TEST_TYPE_HTTP)
}
findViewById<Button>(R.id.btn_quic_ok_test).setOnClickListener {
doTest(TEST_TYPE_QUIC_OVER_OKHTTP)
}
}

@SuppressLint("StaticFieldLeak")
Expand Down
49 changes: 49 additions & 0 deletions demo/proguard-cronet.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Keep annotations used by chromium to keep members referenced by native code
-keep class org.chromium.base.annotations.*Native*
-keep class org.chromium.base.annotations.JNINamespace
-keepclasseswithmembers class org.chromium.** {
@org.chromium.base.annotations.AccessedByNative <fields>;
}
-keepclasseswithmembers class org.chromium.** {
@org.chromium.base.annotations.*Native* <methods>;
}

# TODO(mef) remove unnecessary classes from base, so we don't have to preserve
# their methods
-keepclasseswithmembers class org.chromium.** {
native <methods>;
}

# TODO(xunjieli): Find an alternative to explictly preserving public classes.
-keep public class org.chromium.net.*

-keep class org.chromium.net.impl.CronetUrlRequest$HeadersList
-keep class org.chromium.net.impl.ChromiumUrlRequest$ResponseHeadersMap

# Needed so that multiple optimization passes will detect annotations
-keepattributes *Annotation*

# Keep methods used by reflection
-keep class org.chromium.base.annotations.UsedByReflection
-keep @org.chromium.base.annotations.UsedByReflection class *
-keepclassmembers class * {
@org.chromium.base.annotations.UsedByReflection *;
}

# Suppress unnecessary warnings.
-dontnote org.chromium.net.ProxyChangeListener$ProxyReceiver
-dontnote org.chromium.net.AndroidKeyStore
# Needs 'void setTextAppearance(int)' (API level 23).
-dontwarn org.chromium.base.ApiCompatibilityUtils
# Needs 'boolean onSearchRequested(android.view.SearchEvent)' (API level 23).
-dontwarn org.chromium.base.WindowCallbackWrapper
# Generated for chrome apk and not included into cronet.
-dontwarn org.chromium.base.library_loader.NativeLibraries
# Generated for chrome apk and not included into cronet.
-dontwarn org.chromium.base.multidex.ChromiumMultiDexInstaller
# Objects of this type are passed around by native code, but the class
# is never used directly by native code. Since the class is not loaded, it does
# not need to be preserved as an entry point.
-dontnote org.chromium.net.UrlRequest$ResponseHeadersMap
# https://android.googlesource.com/platform/sdk/+/marshmallow-mr1-release/files/proguard-android.txt#54
-dontwarn android.support.**

0 comments on commit 21ab65f

Please sign in to comment.