使用gradle在命令行上编译时,Android Library项目无法导入另一个库项目的R类

时间:2013-09-05 21:20:25

标签: android eclipse gradle r.java-file build.gradle

我的android项目有这个结构:

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  

我可以使用eclipse和Android Studio编译Android应用程序。 LibraryA通过"导入LibraryB的R文件; import com.LibraryB.R;"我还在代码中使用com.LibraryB.R.layout....类型引用,只要我在IDE中就可以了。

我正在尝试从CI服务器的命令行构建内容,并且我已经尝试了ant和gradle,并且最终在每个中都获得了相同的构建错误。

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

我甚至将LibraryB作为本地aar文件发布并使用它来构建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'  

我需要能够从其他库项目和主应用程序访问LibraryB的资源。我似乎无法弄清楚我做错了什么。任何帮助都会很棒。 Fyi使用eclipse生成的gradle脚本给出了同样的问题。

2 个答案:

答案 0 :(得分:7)

你好我有同样的问题,我的android库项目,使用另一个库(actionBarSherlock)。它无法解析导入com.actionbarsherlock.R; 当我在gradle.build中更改行时:  apply plugin:'android-library' 至:  apply plugin:'android' 那就顺利了。

但我需要它就像一个图书馆。

解决方案: 解决方案是否为库生成了包含库中的R文件。 而是导入com.actionbarsherlock.R; 使用import com.myprojectalsolibrary.R;

答案 1 :(得分:0)

你试过包括

吗?
include ':LibraryB'
在你的settings.gradle中