【问题标题】:Packages from library module not found in main module在主模块中找不到来自库模块的包
【发布时间】:2017-03-21 19:12:26
【问题描述】:

我有一个从 Eclipse 导入到 Android Studio 的项目。在 Eclipse 中一切正常。

它包含一个主模块(Eclipse 中的项目),它使用库模块(Eclipse 中的库项目)中的包。由于迁移不顺利,我手动创建了一个库模块,只是将所有源代码复制到新创建的模块中。

问题是主模块似乎没有从库模块中找到包,当我重建项目时,我收到“包 bla bla 不存在”之类的错误。

这里是主要模块 gradle.build:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "com.pointer.mamagoose"
        minSdkVersion 9
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile project(':linphoneclean')
    compile 'com.android.support:support-v4:25.0.0'
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.google.android.gms:play-services:9.4.0'
    compile files('libs/firebase-client-android-2.5.0.jar')
    compile files('libs/apache-httpcomponents-httpclient.jar')
    compile files('libs/apache-httpcomponents-httpcore.jar')
    compile files('libs/android-support-v7-recyclerview.jar')
}

linphoneclean 是库模块。

整个项目的settings.gradle:

include ':linphoneclean'
include ':tigris'

这是库模块的build.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.0'
    testCompile 'junit:junit:4.12'
    compile 'com.google.code.gson:gson:2.6.2'
    compile files('libs/commons-lang3-3.4.jar')
    compile files('libs/linphone.jar')
    compile files('libs/firebase-client-android-2.5.0.jar')
}

库模块的结构包括例如文件夹: src/main/com/pointer/linphone(里面有所有的java文件,减速为package com.pointer.linphone,但我仍然收到错误消息>“com.pointer.linphone包不存在)。

我做错了什么?

【问题讨论】:

  • 据我所知,您没有为 linphoneclean 和 tigris 指定模块项目的路径。正如您从我的回答中看到的那样,您要求查看此问题 settings.gradle 包含两件事,首先是“包含...”第二个模块路径。示例:包括 ':facebook' project(':facebook').projectDir = new File(settingsDir, '../dependencies/FacebookSDK')
  • 你有没有找到根本原因?我有一个类似的问题:(。
  • 对不起,但没有。在这种情况下,我有一个足够小的库,所以我只是将它添加到主项目中。
  • 我遇到了这个确切的问题。我有一个工作项目,几个月后我在一个带有更新版本的 Android Studio 的新系统中打开了它。我试过清理/重建,但没有任何效果。为了测试这一点,我创建了一个全新的项目。创建一个 Common 库模块(具有 google guava 依赖项),将 Common 模块包含在主 App 项目中。但是 App 项目看不到任何 guava 包。这太疯狂了!

标签: android android-studio module android-library


【解决方案1】:

在同一个问题斗争了几个小时之后,这对我有用。

我创建了一个带有空白活动的新项目,添加了一个带有虚拟类的库模块,定义了依赖项。通过在应用程序中导入虚拟类来验证它是否有效。然后我从真实项目中复制了所有相关代码。

我的想法是,这可能是 IDE 的 iml 文件的问题,因为从头开始复制内容过度工作。

【讨论】:

  • 自从我提出这个问题以来已经过了这么长时间,我现在不会测试它,但由于它唯一的答案似乎合乎逻辑,我会接受它。
【解决方案2】:

正确查看源文件包含 java 文件夹和 res 文件夹,在 java 包中添加 java 文件,在 res 文件夹中添加 res。在 Manifest 文件中添加 Activity 名称,请参见此处,在 Gradle 文件中添加必要的库文件。清理并重建项目Android 工作室。

https://developer.android.com/guide/topics/manifest/manifest-element.html

【讨论】:

  • 我可能在你的回答中遗漏了一些东西,但我的 java 文件当然在 src 文件夹中,而资源在 res 文件夹中..
【解决方案3】:

也许你可以检查库的 AndroidManifest 是否正确定义了包,例如:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
          package="com.pointer.linphone">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 2016-09-13
    • 2020-10-25
    • 2017-07-29
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多