Ibotta / gradle-aspectj-pipeline-plugin

A Gradle plugin for Android projects which performs AspectJ weaving using Android's bytcode manipulation pipeline.
Apache License 2.0
76 stars 14 forks source link

Not available with Hilt #26

Open kaki2015 opened 2 years ago

kaki2015 commented 2 years ago

I want to use your library with together the hilt(DI Library). But, I encountered an error Unable to instantiate application com.example.myapplication.MainApplication. Doesn't recognize the Application Class(MainApplication) when building. please help me, thx.

Reproduction

  1. Create a new Project with defalt.(Empty Compose Activity)
  2. Add the settings at build.gradle (Project: My_Application).

    buildscript {
    
    // Add the following
    repositories {
        google()
        maven { url "https://plugins.gradle.org/m2/" }
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.1'
        classpath 'org.aspectj:aspectjtools:1.9.6'
        classpath 'com.ibotta:plugin:1.2.0'
    }
  3. Add the settings at build.gradle (Module: My_Application.app).
    plugins {
    // Add the following
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'com.ibotta.gradle.aop'
    }

    Add the implementations.

    // aspectj
    implementation 'org.aspectj:aspectjrt:1.9.6'
    // Hilt
    implementation 'com.google.dagger:hilt-android:2.40.1'
    kapt 'com.google.dagger:hilt-android-compiler:2.40.1'
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
  4. Create the Application Class.

    class MainApplication : Application()

    Add android:name="com.example.myapplication.MainApplication" at AndroidManifest.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.aoptestapplication">
    
    <application
        android:name="com.example.myapplication.MainApplication" // add
  5. Build OK
  6. Add the @HiltAndroidApp
    @HiltAndroidApp // add 
    class MainApplication : Application()
  7. Run the app. Unable to instantiate application com.example.myapplication.MainApplication: java.lang.ClassNotFoundException:
    2022-06-23 10:26:09.881 18954-18954/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 18954
    java.lang.RuntimeException: Unable to instantiate application com.example.myapplication.MainApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.myapplication.MainApplication" on path: DexPathList[[zip file "/data/app/com.example.myapplication-ytHMBpWkgDmYGcXCZ6NxFg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myapplication-ytHMBpWkgDmYGcXCZ6NxFg==/lib/arm64, /system/lib64, /system/product/lib64]]
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1226)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6431)
        at android.app.ActivityThread.access$1300(ActivityThread.java:219)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myapplication.MainApplication" on path: DexPathList[[zip file "/data/app/com.example.myapplication-ytHMBpWkgDmYGcXCZ6NxFg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myapplication-ytHMBpWkgDmYGcXCZ6NxFg==/lib/arm64, /system/lib64, /system/product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
        at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1148)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1218)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6431) 
        at android.app.ActivityThread.access$1300(ActivityThread.java:219) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
        Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/myapplication/Hilt_MainApplication;
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
                ... 14 more
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myapplication.Hilt_MainApplication" on path: DexPathList[[zip file "/data/app/com.example.myapplication-ytHMBpWkgDmYGcXCZ6NxFg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myapplication-ytHMBpWkgDmYGcXCZ6NxFg==/lib/arm64, /system/lib64, /system/product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                ... 17 more
vdelricco commented 2 years ago

Thanks for the issue submission @kaki2015, will report back with my findings 👀

vdelricco commented 2 years ago

Hi @kaki2015, in the project you were attempting to add Hilt, could you try adding the following to the project build.gradle file:

hilt {
    enableAggregatingTask = false
    enableExperimentalClasspathAggregation = true
}

In my repro setup, this caused the app to behave properly. This uses a flag which is marked deprecated so there might be some more investigation to be done here but I was wanted to see if it also solves the problem for you.

Sunar commented 2 years ago

@vdelricco I've encountered same error and that setup for Hilt fixed the problem. Thanks!