AndroidAnnotations + Android Studio - The generated null.R class cannot be found
Asked Answered
F

7

5

I have setup ActiveAndroid as per the wiki instructions using latest version of AndroidStudio. I am using product Flavours. This is my gradle build file:

apply plugin: 'android'
apply plugin: 'android-apt'

apt {
    arguments {
        androidManifestFile variant.processResources.manifestFile
        resourcePackageName android.defaultConfig.packageName
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
        a {
            packageName "com.a.a"
        }

        b {
            packageName "com.a.b"
        }

        c {
            packageName "com.a.c"
        }
    }
}

dependencies {
    apt "org.androidannotations:androidannotations:3.0+"
    compile "org.androidannotations:androidannotations-api:3.0+"
    compile 'com.android.support:support-v4:19.0.1'
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Gradle build files but when I compile/go to debug on the device I receive two errors:

Error:: The generated null.R class cannot be found

and

Error:Execution failed for task ':ml:compileADebugJava'.

Compilation failed; see the compiler error output for details.

I have tried numerous setups for my build file but cannot for the life of me get it work. Also when I try and change my AndroidManifest from:

android:name="com.a.a.MainActivity"

to

android:name="com.a.a.MainActivity_"

it states that class cannot be found.

I am using latest version of Gradle and latest version of ActiveAndroid.

Any help would be much appreciated.

Fieldpiece answered 28/3, 2014 at 15:24 Comment(1)
You should define packageName in default config. In other flavors you can override it. But default value should be set necessarily.Uzzi
A
8

I know its late, but it might help someone.

This happens when you modify the applicationId. The script provided in example assumes that you have declared "android.defaultConfig.applicationId". In most of the cases its null, and hence it generated null.R. Either you can define the variable or change the code to following:

defaultConfig {

    // Rest of Config

    javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["resourcePackageName": "<Original Package Name>"]
            }
    }
}

Note that original Package name should be same as the location of R in your activity.

Hope it helps!

Aeonian answered 25/5, 2017 at 11:50 Comment(0)
I
4

I have just got into this same problem, solved by adding packageName to the defaultConfig:

Sample:

defaultConfig {
    packageName "com.example.myapp"
    minSdkVersion 8
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}

Change com.example.myapp according to the name specified in your AndroidManifest.xml.

Hope this helps.

Inesita answered 1/6, 2014 at 11:43 Comment(1)
“Could not find method packageName() for arguments” — didn't work."Earhart
Z
4
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.your.app"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}



apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName android.defaultConfig.applicationId
    }
}

It works for me.

Zosima answered 3/6, 2015 at 7:36 Comment(0)
Q
3

I was facing this problem, so I just removed this line:

resourcePackageName android.defaultConfig.packageName

and it worked.

Quirinal answered 28/3, 2014 at 21:55 Comment(1)
I did not remove it but added the real name of my base package, like resourcePackageName 'org.trandroid' (instead of resourcePackageName 'org.trandroid.full')Wommera
D
3
apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName android.defaultConfig.applicationId
    }

In my case, It Worked.

Dragline answered 9/4, 2016 at 0:6 Comment(0)
D
1

I had the same problem. I've update the build.gralde file as follow.

For Java I've added:

android {
    defaultConfig {
        applicationId "<your.application.id>"
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath true
                arguments = ["resourcePackageName": android.defaultConfig.applicationId]
                //arguments = ['androidManifestFile': variant.outputs[0].processResources.manifestFile]
            }
        }
    }
}

For Koltin I've added:

kapt {
    arguments {
        arg("resourcePackageName", android.defaultConfig.applicationId)
        //arg("androidManifestFile", variant.outputs[0]?.processResources?.manifestFile)
    }
    correctErrorTypes = true // add this if you use data binding
}
Dobbins answered 20/11, 2019 at 13:2 Comment(1)
No success: “Could not find method includeCompileClasspath() for arguments [true]”Earhart
K
0

change the applicationId==package in the manifest. 100% success.

Kortneykoruna answered 6/7, 2015 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.