AndroidBaseUtils icon indicating copy to clipboard operation
AndroidBaseUtils copied to clipboard

Use ExtrasBinder for library project

Open ugurcany opened this issue 8 years ago • 21 comments

Inside my ViewPagerFragment, which extends support.v4.app.Fragment, while invoking ExtrasBinder.bind(this);, I am getting the following err:

java.lang.ClassNotFoundException: com.accenture.android.test.fragment.ViewPagerFragment$$ExtraBinder

What can cause this problem?

ugurcany avatar Mar 14 '16 14:03 ugurcany

My first guess would be that you've forgotten to add the @Extra annotation to your member variables. If so, then that's why the $$ExtraBinder was never generated for your ViewPagerFragment class.

That is unless I've overlooked something important while reading the source code. If my guess doesn't solve your problem then @TheFinestArtist will likely be able to give us the correct answer.

rgson avatar Mar 14 '16 16:03 rgson

Nope, @Extra annotation was added.

ugurcany avatar Mar 14 '16 16:03 ugurcany

@ugurcany Thanks for reporting an issue! I will take a look and try to regenerate the bug!

TheFinestArtist avatar Mar 14 '16 21:03 TheFinestArtist

@ugurcany I just found out why! I think your ExtraBinder class is proguarded. Therefore, the ExtraBinder wouldn't be able to find the class for you!

screen shot 2016-03-15 at 6 32 30 am

Check if your ExtraBinder class is created after you build your app! If it does, I am pretty sure that java.lang.ClassNotFoundException caused by the proguard. Please add following proguard option to keep ExtraBinder class to be proguarded.

-keep class com.thefinestartist.annotations.** { *; }
-keep class **$$ExtraBinder { *; }
-keepclasseswithmembernames class * {
    @com.thefinestartist.annotations.Extra <fields>;
}

Let me know if you have any more issues or questions!

TheFinestArtist avatar Mar 14 '16 21:03 TheFinestArtist

Proguard is not enabled for my project. I am having these lines in my proguard rules file anyway.

ugurcany avatar Mar 14 '16 21:03 ugurcany

@ugurcany Can you share me part of your code so that I can regenerate the error? Is your com.accenture.android.test.fragment.ViewPagerFragment$$ExtraBinder class created?

TheFinestArtist avatar Mar 14 '16 22:03 TheFinestArtist

Here is my ViewPagerFragment class:

public class ViewPagerFragment extends Fragment {

    @Extra("TEXT")
    String text;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_viewpager, container, false);
        ButterKnife.bind(this, rootView);
        ExtrasBinder.bind(this);

        acn_textview.setText(text);

        return rootView;
    }

    @Bind(R.id.acn_textview)
    AcnTextView acn_textview;

}

And here is the error in more details:

Caused by: java.lang.ClassNotFoundException: Didn't find class "acn.android.test.fragment.ViewPagerFragment$$ExtraBinder" on path: DexPathList[[zip file "/data/app/acn.android.test-1/base.apk"],nativeLibraryDirectories=[/data/app/acn.android.test-1/lib/x86, /vendor/lib, /system/lib]]

ugurcany avatar Mar 15 '16 07:03 ugurcany

I checked under my build folder. ViewPagerFragment$$ExtraBinder is not created. What should I do to fix this?

ugurcany avatar Mar 15 '16 14:03 ugurcany

Honestly, I couldn't really regenerate your issue. I added Butterknife in my sample app and copied your class and for me ExtraBinder worked fine. Seems like you are using Butterknife. I have few more question.

  1. There is two kinds of dependencies for Butterknife. Are you using compile 'com.jakewharton:butterknife:7.0.1'?
  2. Have you added apt 'com.thefinestartist:compilers:0.8.5' in your dependencies?
    • If you haven't, the compiler won't generate any ExtraBinder class in build time!
  3. Maybe your device API version?
  4. It would be really helpful if you share me any project that keep causing this error!

I really thanks for your contribution for the library!

TheFinestArtist avatar Mar 16 '16 04:03 TheFinestArtist

  1. Yes, I am using compile 'com.jakewharton:butterknife:7.0.1'.
  2. Yes, apt 'com.thefinestartist:compilers:0.8.5' and apply plugin: 'com.neenbedankt.android-apt' are added to module-level gradle file. And classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' is added to project-level gradle file.
  3. My device API ver is 21.

ugurcany avatar Mar 16 '16 11:03 ugurcany

Oh Okay, I think I really need any sample app that causes this ClassNotFoundException.

TheFinestArtist avatar Mar 16 '16 11:03 TheFinestArtist

Here it is: https://github.com/ugurcany/ACN-Android-Framework

test module contains the ViewPagerFragment class that I shared.

ugurcany avatar Mar 16 '16 11:03 ugurcany

@ugurcany Thanks so much! I think the annotation processor doesn't work properly if it is added in other than main module! I will take a look on this!

TheFinestArtist avatar Mar 16 '16 12:03 TheFinestArtist

@ugurcany Seems like it work fine if apply plugin: 'com.neenbedankt.android-apt' is added in main project module. With APT plugin, I think the android processor work fine.

I will still looking for way to use ExtraBinder without adding apply plugin: 'com.neenbedankt.android-apt' line in your main project module.

TheFinestArtist avatar Mar 16 '16 12:03 TheFinestArtist

Thank you for the solution. I will try.

ugurcany avatar Mar 16 '16 12:03 ugurcany

Could you please let me know when it is possible to solve the problem without a need to apply the plugin to main module?

ugurcany avatar Mar 18 '16 11:03 ugurcany

@ugurcany I tried to understand Android annotation deeply, I think I need to understand how Butterknife is working for such case. I can't really say when would I be able to add this, I will do ASAP! Let me try this for this weekend and I will let you know how much it would take!

TheFinestArtist avatar Mar 18 '16 13:03 TheFinestArtist

@ugurcany I think this might take longer than I thought! Sorry about this~

TheFinestArtist avatar Mar 20 '16 07:03 TheFinestArtist

Have you found any solution?

ugurcany avatar Mar 24 '16 13:03 ugurcany

@ugurcany Not much I was busy with updating other apps. Do you have any idea for this?

TheFinestArtist avatar Mar 24 '16 15:03 TheFinestArtist

Nope. Sorry.

ugurcany avatar Mar 25 '16 06:03 ugurcany