findViewById为依赖项

时间:2016-09-12 08:34:48

标签: android android-studio android-gradle

我有一个使用库C的应用程序A.库C包含一个需要来自库D的资源的活动。

我希望D中的资源捆绑到C中,但在C中执行findViewById(R.id.resource_from_d)时得到null。我怎样才能使它工作?

C没有默认的项目结构,但C本身不包含任何资源。我指定了 C的build.gradle中的sourceSets { main { java.srcDirs = ['src'] }}

我还将proguard和minifyEnabled设置为false,以避免错误地删除任何资源。

我还尝试为所有传递依赖项设置transitive = true,但没有运气。我该怎么做才能让它发挥作用?

这是C的build.gradle,它取决于D的资源。

apply plugin: 'com.android.library'

android {
    compileSdkVersion 17
    buildToolsVersion "23.0.2"

    compileOptions.encoding = 'windows-1252'

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 17
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }

    lintOptions {
        abortOnError false
    }

    sourceSets {
        main {
            java.srcDirs = ['src']
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }

    dexOptions {
        preDexLibraries = false
    }
}

dependencies {
    compile project(':d')
}

C的项目结构不是默认值,因此我为源添加了sourceSets。但是,C不包含任何自己的资源。

1 个答案:

答案 0 :(得分:0)

您应该能够同时使用R s

import com.apackage.R;

//...

int aResource = R.id.resource_from_a;
int dResource = com.dpackage.R.id.resource_from_d;