Skip to content

Commit

Permalink
混淆配置移到模块的 consumer-rules.pro 内避免手动配置
Browse files Browse the repository at this point in the history
  • Loading branch information
twiceyuan committed Dec 6, 2020
1 parent 58f8fb7 commit 6b4f6d2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 47 deletions.
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,18 @@ dependencies {

## ProGuard

已经包含在 consumer-rules 文件中,不必单独手动添加。文件内容为:
```
-keep class * extends com.twiceyuan.safetyparams.library.SafetyParams { *; }
-keep class * extends android.os.Parcelable { *; }
-keep class * extends java.io.Serializable { *; }
```
-keep class * extends com.twiceyuan.safetyparams.library.SafetyParams {
<init>(...);
*;
}
-keep class * implements android.os.Parcelable { *; }
-keep class * implements java.io.Serializable { *; }

-dontwarn org.jetbrains.annotations.**
-dontwarn kotlin.reflect.jvm.internal.**
需要注意的是,如果 SafetyParams 的子类定义为 Fragment 的内部类,并且没有定义 keep Fragment 成员配置的话,该子类仍然会被混淆,原因暂不清楚(尝试把该内部类定义在其他 Class 下均未发现该现象)。

-keep class kotlin.reflect.jvm.internal.** { *; }
-keep class kotlin.Metadata { *; }
-keep public class kotlin.reflect.jvm.internal.impl.builtins.* { public *; }
```
如果需要混淆避免该情况,有两种建议的做法:
1. 把 SafetyParams 定义在外部,例如和 Fragment 平级的 class
2. 添加 keep Fragment 的配置:`-keep class * extends androidx.fragment.app.Fragment { *; }`

## Java 兼容性

Expand Down
8 changes: 3 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ android {
versionCode 1
versionName "1.0"

testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
consumerProguardFiles 'consumer-rules.pro'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Expand All @@ -41,7 +40,7 @@ tasks.withType(JavaCompile) {
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
archiveClassifier.set('sources')
}
task javadoc(type: Javadoc) {
failOnError false
Expand All @@ -51,8 +50,7 @@ task javadoc(type: Javadoc) {
}
// build a jar with javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
//noinspection GroovyAccessibility
archiveClassifier.set('javadoc')
from javadoc.destinationDir
}
artifacts {
Expand Down
9 changes: 8 additions & 1 deletion library/proguard-rules.pro → library/consumer-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# 继承 SafetyParams 的数据类需要保持原有的参数名称
-keep class * extends com.twiceyuan.safetyparams.library.SafetyParams { *; }
-keep class * extends android.os.Parcelable { *; }
-keep class * extends java.io.Serializable { *; }

# Fragment 的内部类不被上条规则影响,仍然会被混淆,找到好的解决方法之前先 keep fragment 的子类
# -keep class * extends androidx.fragment.app.Fragment { *; }
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import androidx.fragment.app.Fragment
import java.io.Serializable
import java.lang.reflect.Field
import java.lang.reflect.ParameterizedType
import java.util.*
import kotlin.collections.ArrayList
import kotlin.reflect.KParameter
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.javaType
import kotlin.reflect.jvm.jvmName

/**
* Created by twiceYuan on 2018/3/26.
Expand All @@ -35,7 +39,8 @@ private fun <TargetClass : Class<*>> SafetyParams.genericTypeParam(): TargetClas
*/
abstract class ActivityParams<ActivityType : Activity> : SafetyParams() {

private val targetClass: Class<ActivityType> by lazy { genericTypeParam() }
private val targetClass: Class<ActivityType>
get() = genericTypeParam()

fun launch(context: Context) {
context.startActivityWithArgs(targetClass, this)
Expand All @@ -49,12 +54,13 @@ abstract class ActivityParams<ActivityType : Activity> : SafetyParams() {

abstract class FragmentParams<FragmentType : Fragment> : SafetyParams() {

private val targetClass: Class<FragmentType> by lazy { genericTypeParam() }
private val targetClass: Class<FragmentType>
get() = genericTypeParam()

@Suppress("UNCHECKED_CAST")
fun newInstance(): FragmentType {
val bundle = toBundle()
return targetClass.newInstance().apply {
arguments = this@FragmentParams.toBundle()
arguments = bundle
}
}
}
Expand Down Expand Up @@ -219,15 +225,12 @@ inline fun <reified Data : SafetyParams> Fragment.parseParams(): Lazy<Data> {

inline fun <reified Data : SafetyParams> Bundle.toArgs(): Data {

val dataClass = Data::class.java

val constructorMap = hashMapOf<KParameter, Any?>()
val dataClass = Data::class

val primaryConstructor = dataClass.kotlin.constructors.firstOrNull()
?: throw DataBeanNotLegalException("数据类应该有一个主构造器")
val constructorMap = mutableMapOf<KParameter, Any?>()

if (primaryConstructor.parameters.isEmpty())
throw DataBeanNotLegalException("数据类应该至少有一个构造器参数")
val primaryConstructor = dataClass.primaryConstructor
?: throw DataBeanNotLegalException("${dataClass.jvmName} 数据类应该有一个主构造器")

primaryConstructor.parameters.forEach {

Expand Down
4 changes: 3 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ android {
}

release {
minifyEnabled false
minifyEnabled true
debuggable true
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand Down
16 changes: 4 additions & 12 deletions sample/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class * extends com.twiceyuan.activityargs.library.SafetyParams {
<init>(...);
*;
}

-keep class * implements android.os.Parcelable { *; }
-keep class * implements java.io.Serializable { *; }
# Fragment 的内部类不被 consumer-rules 里的规则影响,仍然会被混淆,找到好的解决方法之前先 keep fragment 的子类
-keep class * extends androidx.fragment.app.Fragment { *; }

-dontwarn org.jetbrains.annotations.**
-dontwarn kotlin.reflect.jvm.internal.**

-keep class kotlin.reflect.jvm.internal.** { *; }
-keep class kotlin.Metadata { *; }
-keep public class kotlin.reflect.jvm.internal.impl.builtins.* { public *; }
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import com.twiceyuan.safetyparams.sample.bean.ParcelableBean
class ReceiverFragment : DialogFragment() {

data class Params(
val age: Int,
val name: String,
val phone: String,
val nestedBean: Father,
val emails: ArrayList<String>?,
val parcelableBean: ParcelableBean
val parcelableBean: ParcelableBean,
val nestedBean: Father,
val age: Int
) : FragmentParams<ReceiverFragment>()

private val params by parseParams<Params>()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Expand Down

0 comments on commit 6b4f6d2

Please sign in to comment.