Android Studio库项目引用未解析的符号

我终于将我的
eclipse
android项目迁移到
Android Studio.我创建了一个包含两个模块的项目.一个模块是一个图书馆项目.另一个是普通的活动项目,其中包括库模块.

Project/
   app/     (Activity Project)
   br_lib/  (Library Project)

在应用程序的模块设置中,我向lib模块添加了一个dependency.
lib模块项目正常构建,但每次我引用其中一个lib模块时,我的app模块构建都会失败:

import org.apache.http.client.HttpClient;
import org.cddevlib.breathe.setup.Utils <--  // lib refrence;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.convertName(""); // <--- lib module static class;
    }
}

错误:

Error:(9, 34) error: cannot find symbol class Utils
Error:(25, 9) error: cannot find symbol variable Utils

怎么了?我错过了什么?谢谢

这是应用程序模块gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

defaultConfig {
    applicationId "pro.breathe.cddev.org.br_gold"
    minSdkVersion 22
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
        }
    }
}

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

这是lib模块gradle:

    apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"

defaultConfig {
    minSdkVersion 9
    targetSdkVersion 9
}

dependencies {
    compile 'com.android.support:support-v4:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:cardview-v7:22.2.1'
    compile 'com.android.support:gridlayout-v7:22.2.1'
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.google.android.gms:play-services:7.8.0'
}

buildTypes {
    release {
        minifyEnabled true
        proguardFiles 'proguard.cfg'
      }
    }
}

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

这是项目的gradle:

buildscript {
    repositories {
    jcenter()
    }
dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()
}
}

设置gradle是:

include ':app', ':br_lib'

最佳答案 今天在一个有5个模块的项目中遇到了同样的问题(其中2个是com.android.library模块).在对build.gradle文件进行了大量试验和错误更改之后,解决方案最终涉及在库模块的gradle文件中包含publishNonDefault true属性.

我发现这篇文章非常有帮助:
StackOverflow – Product Flavors and Library Publication

我的应用程序gradle文件最终看起来像(故意省略代码):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'NOTICE'
        exclude 'META-INF/LICENSE'
    }

    defaultConfig {
    }

    productFlavors {
        funapp {
            applicationId "com.funapp.package"
        }
        funappbeta {
            applicationId "com.funapp.beta"
        }
    }

    signingConfigs {
        googleplay {
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            debuggable true
            signingConfig signingConfigs.debug
        }
        release {
            debuggable false
            signingConfig signingConfigs.googleplay
        }
    }
}

configurations {
    funappDebugCompile
    funappReleaseCompile
    funappbetaDebugCompile
    funappbetaReleaseCompile

    // Establish debug compile dependencies for build variants
    [funappDebugCompile, funappbetaDebugCompile].each {
        it.extendsFrom commonFunSdkDebugCompile
    }
    // Establish release compile dependencies for build variants
    [funappReleaseCompile, funappbetaReleaseCompile].each {
        it.extendsFrom commonFunSdkReleaseCompile
    }
    // Common Android and JUnit testing dependencies (JUnit tests can be run
    // using only a JVM; Android tests require a device or emulator)
    [androidTestCompile, testCompile].each {
        it.extendsFrom commonTestCompile
    }
}

dependencies {
    commonFunSdkDebugCompile project   (path: ':fun-sdk', configuration: 'debug')
    commonFunSdkReleaseCompile project (path: ':fun-sdk', configuration: 'release')

我的库gradle文件看起来像:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

    publishNonDefault true // <-- This happened to be the key component
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    defaultConfig {
    }

    buildTypes {
        debug {
        }
        release {
        }
    }
}

configurations {
    debug
    release
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
点赞