google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.42k stars 2.01k forks source link

Application class annotated with @HiltAndroidApp has to be defined in an android application project #3647

Closed crescent7272 closed 1 year ago

crescent7272 commented 1 year ago

I am trying to implement multi module hilt project. However I am having trouble to make it work. I got this error and googled but I could not find any result.

Here is the error I get:

C:\Users\hilal\StudioProjects\commerce_###\app\build\tmp\kapt3\stubs\debug\br\com\###\###\MyApplication.java:10: error: [Hilt]
public final class MyApplication extends android.app.Application {
             ^
  Application class annotated with @HiltAndroidApp has to be defined in an android application project
  [Hilt] Processing did not complete. See error above for details.

This is my application class.

@HiltAndroidApp
class MyApplication: Application() {
    override fun onCreate() {
        super.onCreate()
        MultiDex.install(this)
    }
}

This is my top level build.gradle file // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.7.0'
    repositories {
        google()
        mavenCentral()
        maven { url='https://jitpack.io'}

    }
    ext {
        compose_version = '1.2.1'
        /* Http */
        retrofitVersion = "2.9.0"
        okHttpVersion = "4.10.0"
        okHttpInterceptorVersion = "4.10.0"
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20'
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:1.7.20"
        classpath "com.apollographql.apollo:apollo-gradle-plugin:2.4.6"
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.44.2'/**/
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        flatDir {
            dirs 'libs'
        }
        maven { url "https://jitpack.io" }

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And the following is app/build.gradle (which is library)

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.protobuf'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'kotlin-android-extensions'
    id 'com.google.dagger.hilt.android'
}
apply plugin: 'kotlin-android'

android {
    compileSdk 33

    defaultConfig {
        minSdk 26
        targetSdk 33
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
        dataBinding true
    }
    packagingOptions{
        pickFirst "androidsupportmultidexversion.txt"
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    hilt {
        enableAggregatingTask = true
    }
    // Allow references to generated code
    kapt {
        correctErrorTypes true
    }
    namespace 'br.com.martonis.commerceTmobPos'
}

dependencies {
    def lifecycle_version = "2.5.1"
    def roomVersion = "2.4.3"
    def nav_version = "2.5.3"

    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
    implementation 'androidx.multidex:multidex:2.0.1'

    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.2.1'

    //Material Design
    implementation 'com.google.android.material:material:1.6.1'

    def fragments_version = "1.5.2"
    implementation "androidx.fragment:fragment-ktx:$fragments_version"
    // ViewModel
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
    // ViewModel utilities for Compose
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version")
    // LiveData
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
    // Lifecycles only (without ViewModel or LiveData)
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")
    // Annotation processor
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    // To use Kotlin annotation processing tool (kapt)
    implementation("androidx.room:room-runtime:$roomVersion")
    kapt("androidx.room:room-compiler:$roomVersion")
    // optional - Kotlin Extensions and Coroutines support for Room
    implementation("androidx.room:room-ktx:$roomVersion")
    // Coroutines
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.2")

    // Kotlin
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    kapt "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.5.0"

    //Criptografia
    implementation 'com.madgag.spongycastle:bctls-jdk15on:1.58.0.0'

    //AWS SDK for Android
    implementation 'com.amazonaws:aws-android-sdk-core:2.51.0'
    implementation 'com.amazonaws:aws-android-sdk-auth-core:2.51.0'
    implementation 'com.amazonaws:aws-android-sdk-iot:2.36.0'

    implementation 'com.amplifyframework:aws-auth-cognito:1.37.1'

    implementation 'com.jcraft:jsch:0.1.55'

    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

    implementation "androidx.compose.ui:ui:1.2.1"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'

    // Coil
    implementation "io.coil-kt:coil-compose:1.4.0"

    //Protobuf
    apply plugin: 'com.google.protobuf'
    implementation 'com.google.protobuf:protobuf-lite:3.0.0'

    //Barber
    implementation("app.cash.barber:barber:0.3.3")

    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    // G800 Lib
    implementation(name: 'libgedi-0.190121.gpos800', ext:'aar')
    implementation(name: 'SatGerLib', ext: 'aar')

    //Dagger Hilt
    implementation 'com.google.dagger:hilt-android:2.44'
    kapt 'com.google.dagger:hilt-compiler:2.44'

    implementation 'com.airbnb.android:lottie:3.4.0'

    implementation ('io.socket:socket.io-client:2.0.1') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }

    implementation 'com.loopj.android:android-async-http:1.4.11'

    //Encryption
    implementation "androidx.security:security-crypto:1.0.0"

    //UNZIP
    implementation "org.apache.commons:commons-compress:1.21"
    implementation "androidx.core:core-ktx:+"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    //GPS
    implementation 'com.google.android.gms:play-services-location:18.0.0'

    //QR CODE GENERATOR
    implementation 'com.google.zxing:core:3.4.1'

    //Gson
    implementation 'com.google.code.gson:gson:2.9.1'

    /* Retrofit */
    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
    implementation "com.squareup.okhttp3:logging-interceptor:$okHttpInterceptorVersion"
    implementation "com.squareup.retrofit2:converter-scalars:2.4.0"
    implementation 'com.android.support:multidex:1.0.3'
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite { }
            }
        }
    }
}
repositories {
    mavenCentral()
}

And the following is dev/build.gradle (which is a module I created)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'kotlin-android-extensions'

}

android {
    namespace 'br.com.####.dev'
    compileSdk 33

    defaultConfig {
        applicationId "br.com.####.dev"
        minSdk 26
        targetSdk 33
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions{
        pickFirst "androidsupportmultidexversion.txt"
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    hilt {
        enableAggregatingTask = true
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation project(':app')
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    implementation(name: 'libgedi-0.190121.gpos800', ext:'aar')
    implementation(name: 'SatGerLib', ext: 'aar')
    implementation 'com.google.dagger:hilt-android:2.44'
    kapt 'com.google.dagger:hilt-compiler:2.44'
}

This is my manifest of the main module


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"
        tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Material3.Light.NoActionBar"
        tools:replace="android:icon,android:label,android:theme">
        <activity
            android:name="br.com.###.MainActivity"
            android:exported="true"
            >
        </activity>
        <activity
            android:name=".presentation.launchScreen.LaunchScreenActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>

</manifest>

This is my manifest of the module dev


<?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="br.com.###.dev">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Material3.Light.NoActionBar"
        tools:replace="android:icon,android:label,android:theme"
        >
        <activity
            android:name=".MainActivity"
            android:exported="true"
            tools:ignore="Instantiatable">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

Following is settings.gradle

rootProject.name = "TmobPOS"
include ':app',
        ':dev'

I have no idea why I could not manage a create a multi module Hilt project. I must have doing something wrong. I searched but there is no result in google. This is my post at stackoverflow => https://stackoverflow.com/questions/74534344/the-compiler-option-dagger-hilt-android-internal-projecttype-is-not-a-recogniz

Thanks in advance.

bcorso commented 1 year ago

"Application class annotated with @HiltAndroidApp has to be defined in an android application project"

This means that your @HiltAndroidApp annotated class is currently defined in a library module (i.e. a build.gradle file with plugins { id 'com.android.library' }, which is not allowed. Instead, you need to move that class into the application module (i.e. a build.gradle file with plugins {id 'com.android.application'}.