Android library project cannot 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 an Android app just fine using both eclipse and Android Studio. LibraryA imports the R file LibraryB, making " import com.LibraryB.R; ". I also use links like com.LibraryB.R.layout.... in the code, and while I'm in the IDE, everything is fine.

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

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

I even got to publishing LibraryB as a local aar file and used it to create LibraryA

Library B 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 have access to LibraryB resources from other library projects and the main application. I cannot understand what I am doing wrong. Any help would be great. Fyi, using the generated gradle scripts from eclipse, gives the same problem.

+4
source share
2 answers

Hi, I had the same problem, my android library project uses a different library (actionBarSherlock). It cannot allow the import of com.actionbarsherlock.R; when I changed the line in gradle.build: apply the plugin: 'android-library' to: apply the plugin: 'android' then this goes well.

But I needed to have a library.

SOLUTION HERE: Solution. Are R files created from the included library in your library. import com.actionbarsherlock.R instead; use import com.myprojectalsolibrary.R;

+6
source

Have you tried turning on

 include ':LibraryB' 

in your .gradle settings?

0
source

Source: https://habr.com/ru/post/1500817/


All Articles