Kotlin / kotlin-examples

Various examples for Kotlin
3.21k stars 1.04k forks source link

Anything about ProGuard? #37

Closed chpengzh closed 7 years ago

chpengzh commented 8 years ago

I'd like to try kotlin in my project, but I just can't handle it for no proguard rules.

chpengzh commented 8 years ago

It seems there are some problems if name class with upper char when i do proguard task. code like this:

@Suppress("UNCHECKED_CAST")
class Bind<T, V : View>(val id: Int, val rootView: View? = null,
                        val onClick: ((View) -> Unit)? = null,
                        val onLongClick: ((View) -> Boolean)? = null,
                        val init: ((V) -> Unit)? = null) : ReadOnlyProperty<T, V> {

    override fun getValue(thisRef: T, property: KProperty<*>): V {
        val injectView: V
        if (rootView != null) injectView = rootView.findViewById(id) as V
        else injectView = when (thisRef) {
            is Activity -> thisRef.findViewById(id) as V

            is Fragment -> throw BindResourceError(id, "unknown rootView")

            is android.app.Fragment -> throw BindResourceError(id, "unknown rootView")

            is RecyclerView.ViewHolder -> thisRef.itemView.findViewById(id) as V

            is Dialog -> thisRef.findViewById(id) as V

            is View -> thisRef.findViewById(id) as V

            else -> throw BindResourceError(id, "unknown rootView")
        }
        if (onClick != null) injectView.setOnClickListener { onClick }
        if (onLongClick != null) injectView.setOnLongClickListener { onLongClick.invoke(injectView) }
        init?.invoke(injectView)
        return injectView
    }
}

class BindString<T>(id: Int, rootContext: Context? = null) : BindResource<T, String>(
        id = id, rootContext = rootContext, getResource = { it.getString(id) })

class BindInt<T>(id: Int, rootContext: Context? = null) : BindResource<T, Int>(
        id = id, rootContext = rootContext, getResource = { it.resources.getInteger(id) })

class BindStringArray<T>(id: Int, rootContext: Context? = null) : BindResource<T, Array<String>>(
        id = id, rootContext = rootContext, getResource = { it.resources.getStringArray(id) })

open class BindResource<T, R>(val id: Int, val rootContext: Context? = null,
                              val getResource: (Context) -> R) : ReadOnlyProperty<T, R> {
    override fun getValue(thisRef: T, property: KProperty<*>): R {
        if (rootContext != null)
            return getResource(rootContext)
        else
            return getResource(thisRef!!.getSupportedContext() ?:
                    throw BindResourceError(id, "unknown rootContext"))
    }
}

fun Any.getSupportedContext(): Context? = when (this) {
    is Context -> this
    is View -> this.context
    is Fragment -> this.context
    is Dialog -> this.context
    is RecyclerView.ViewHolder -> this.itemView.context
    else -> null
}

class BindResourceError(id: Int, msg: String) : Exception("cant'bind resource $id for $msg")

and build with fail

Note: the configuration refers to the unknown class 'com.google.vending.licensing.ILicensingService'
Note: the configuration refers to the unknown class 'com.android.vending.licensing.ILicensingService'
Note: the configuration refers to the unknown class 'com.android.vending.licensing.ILicensingService'
Note: the configuration refers to the unknown class 'butterknife.InjectView'
Note: the configuration refers to the unknown class 'butterknife.InjectView'
Note: the configuration refers to the unknown class 'sun.misc.Unsafe'
Warning: butterknife.BindInt$1: can't find enclosing method 'BindInt(int,android.content.Context)' in program class butterknife.BindInt
Warning: butterknife.BindString$1: can't find enclosing method 'BindString(int,android.content.Context)' in program class butterknife.BindString
Note: android.support.v4.media.ParceledListSliceAdapterApi21: can't find dynamically referenced class android.content.pm.ParceledListSlice
Note: android.support.v4.text.ICUCompatApi23: can't find dynamically referenced class libcore.icu.ICU
Note: android.support.v4.text.ICUCompatIcs: can't find dynamically referenced class libcore.icu.ICU
Note: android.support.v7.widget.DrawableUtils: can't find dynamically referenced class android.graphics.Insets
Note: com.google.gson.internal.UnsafeAllocator: can't find dynamically referenced class sun.misc.Unsafe
Note: com.loc.bx: can't find dynamically referenced class android.telephony.MSimTelephonyManager
Note: com.loc.bx: can't find dynamically referenced class android.telephony.TelephonyManager2
Note: com.qiniu.android.dns.local.AndroidDnsServer: can't find dynamically referenced class android.os.SystemProperties
Note: com.sina.weibo.sdk.utils.AidTask: can't find dynamically referenced class android.os.SystemProperties
Note: com.squareup.picasso.Utils: can't find dynamically referenced class com.squareup.okhttp.OkHttpClient
Note: com.xingdian.seeyou.ui.view.DragItemLayout: can't find dynamically referenced class com.android.internal.R$dimen
Note: io.agora.rtc.internal.Connectivity: can't find dynamically referenced class android.os.SystemProperties
Note: kotlin.jvm.internal.Reflection: can't find dynamically referenced class kotlin.reflect.jvm.internal.ReflectionFactoryImpl
Note: okhttp3.internal.AndroidPlatform: can't find dynamically referenced class com.android.org.conscrypt.SSLParametersImpl
Note: okhttp3.internal.AndroidPlatform: can't find dynamically referenced class org.apache.harmony.xnet.provider.jsse.SSLParametersImpl
Note: okhttp3.internal.Platform: can't find dynamically referenced class sun.security.ssl.SSLContextImpl
Note: there were 6 references to unknown classes.
      You should check your configuration for typos.
      (http://proguard.sourceforge.net/manual/troubleshooting.html#unknownclass)
Note: there were 16 unresolved dynamic references to classes or interfaces.
      You should check if you need to specify additional program jars.
      (http://proguard.sourceforge.net/manual/troubleshooting.html#dynamicalclass)
Warning: there were 2 unresolved references to program class members.
         Your input classes appear to be inconsistent.
         You may need to recompile the code.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
Warning: Exception while processing task java.io.IOException: Please correct the above warnings first.
:app:transformClassesAndResourcesWithProguardForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesAndResourcesWithProguardForDebug'.
> java.io.IOException: Please correct the above warnings first.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED
chpengzh commented 8 years ago

but if I change class name starting from lowercase(Bind -> bind, BindString -> bindString), it fuckly work!

Dwite commented 7 years ago

@chpengzh did you resolve this issue?

chpengzh commented 7 years ago

yeap! ReadWriteProperty and ReadOnlyProperty implements class should be start with lowercase. But I just don't know why