Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Library project can't import R class of another library project when using gradle to compile on command line

I have this structure for my android project:

ProjectDir  
settings.gradle  
MyApp(depends on LibraryA and LibraryB)  
-->build.gradle  
-->All the other android code  
LibraryA  (depends on LibraryB)  
-->build.gradle  
-->All the other android code  
LibraryB (Has lots of resources that are used in LibraryA and MyApp)  
-->build.gradle  
-->All the other android code  

I can compile the android app just fine using both eclipse and Android Studio. LibraryA imports the R file of LibraryB by doing " import com.LibraryB.R;" I also make use of com.LibraryB.R.layout.... type references in code and as long as I'm in the IDE things are fine.

I am trying to get things to build from the command line for our CI server and I have tried both ant and gradle and I end up getting the same build error in each.

LibraryA/example.java:10:error:cannot find symbol import com.LibraryB.R

I have even gone to the extent of publishing LibraryB as a local aar file and using it to build LibraryA

LibraryB build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

group = 'com.libraryb'
version = '1.0'

apply plugin: 'android-library'
apply plugin: 'maven'

uploadArchives {
        repositories {
                mavenDeployer {
                        repository url: 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
                }
        }
}

task install(dependsOn: uploadArchives)

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

LibraryA build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.libraryb:LibraryB:1.0'
}

android {
    compileSdkVersion 17
    buildToolsVersion "18.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}

MyApp build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

group = 'com.myapp'
version = '1.0'

apply plugin: 'android-library'
apply plugin: 'maven'

uploadArchives {
        repositories {
                mavenDeployer {
                        repository url: 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
                }
        }
}

task install(dependsOn: uploadArchives)

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':LibraryA')
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}

settings.gradle

include ':MyApp'
include ':LibraryA'  

I need to be able to access the resources of LibraryB from other library projects and the main app. I can't seem to figure out what I'm doing wrong. Any help would be great. Fyi, using the generated gradle scripts from eclipse give the same issue.

like image 378
Jeremy Horton Avatar asked Sep 05 '13 21:09

Jeremy Horton


People also ask

How do I sync gradle with Android studio?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.

How do I import an imported project into Android Studio?

Launch Android Studio, and click File > New > Import Project. Locate your project directory, click the build. gradle file you created above to select it, and then click OK to import your project.

What is AAR file in Android?

AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods. AAR files can contain C/C++ libraries for use by the app module's C/C++ code.

How do I add a dependency in gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.


1 Answers

Hello I had same problem, my android library project, uses another library (actionBarSherlock). It can't resolve import com.actionbarsherlock.R; when I changed the line in gradle.build: apply plugin: 'android-library' to: apply plugin: 'android' then it goes well.

But I needed it have like a library.

SOLUTION HERE: Solution Is that R files from included library are generated to your library. instead import com.actionbarsherlock.R; use import com.myprojectalsolibrary.R;

like image 180
user3058154 Avatar answered Oct 20 '22 09:10

user3058154