Android Studio 库“错误:包不存在”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26422860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:22:30  来源:igfitidea点击:

Android Studio library "error: package does not exist"

androidandroid-studioandroid-gradle-plugin

提问by Robertas Setkus

I have created Android library as Android Studio module. Added as dependency to my root module. While coding I can import any class from library package but while I'm trying run the application I'm getting an error package some.mylibrary.project does not exist.

我已经创建了 Android 库作为 Android Studio 模块。添加为我的根模块的依赖项。在编码时,我可以从库包中导入任何类,但是在尝试运行应用程序时出现错误package some.mylibrary.project does not exist

build.gradleroot module

build.gradle根模块

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}
apply plugin: 'com.android.application'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'com.google.android.gms:play-services:5.+'
    compile project(':libraries:mylibrary')
}

android {
    compileSdkVersion 17
    buildToolsVersion "20.0.0"

    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
        abortOnError false
    }

    ***
}

build.gradlelibrary module

build.gradle库模块

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'idea'

android {
    compileSdkVersion 17
    buildToolsVersion "20.0.0"

     *****    
}

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

settings.gradle

设置.gradle

include ':libraries:mylibrary'

P.S. I have to mention that the project was exported from Eclipse IDE so the project structure is different from default one.

PS我不得不提到该项目是从Eclipse IDE导出的,因此项目结构与默认结构不同。

回答by CodeToLife

For Android Studio 2.2.2

对于 Android Studio 2.2.2

Yes, in library module, it can't use the apply plugin: com.android.applicationstatement in the module definition, yes, use apply plugin: com.android.libraryinstead. (still in lib module)

是的,在库模块中,它不能使用com.android.application模块定义中的 apply plugin:语句,是的,使用 apply plugin:com.android.library代替。(仍在 lib 模块中)

But then you have to do the following:

但是,您必须执行以下操作:

  1. Expose the same SDK versions in Gradle files for both modules.
  2. Right click on your projects "app" module folder and click on -> open module settings
  3. Click on the "dependencies" tab
  4. Click on the + sign to add a new dependency and select "Module Dependency"
  5. Look for the library you need and add it.
  1. 在 Gradle 文件中为两个模块公开相同的 SDK 版本。
  2. 右键单击您的项目“app”模块文件夹,然后单击 -> 打开模块设置
  3. 单击“依赖项”选项卡
  4. 单击 + 号添加新的依赖项并选择“模块依赖项”
  5. 查找您需要的库并添加它。

Also while naming your lib module avoid capitals.

此外,在命名您的 lib 模块时,请避免使用大写字母。

回答by Scott Barta

If you have a library module, it can't use the apply plugin: 'com.android.application'statement in the module definition, or the build will silently fail as you're seeing. use apply plugin: 'com.android.library'instead.

如果您有一个库模块,它不能使用apply plugin: 'com.android.application'模块定义中的语句,否则构建将如您所见地默默地失败。使用apply plugin: 'com.android.library'来代替。

A bug has been filed to request that the build system fail loudly instead of silently when this happens: https://code.google.com/p/android/issues/detail?id=76725

已提交一个错误,要求构建系统在发生这种情况时大声失败而不是静默失败:https: //code.google.com/p/android/issues/detail?id=76725

回答by O Th?nh Ldt

In build-gradle app, add this row:

在 build-gradle 应用程序中,添加这一行:

implementation project(":your_name_library_here")

回答by Mehmet Agah Balbay

The answer above is somewhat lacking If you project java add in Kotlin getting get this error

上面的答案有些欠缺如果你在 Kotlin 中项目 java add 得到这个错误

  • Tools Tab Kotlin and Configure Kotlin
  • (Select Android with Gradle) after select with Modules
  • 工具选项卡 Kotlin 和配置 Kotlin
  • (选择 Android with Gradle)后选择模块

Project build.gradle add

项目 build.gradle 添加

ext.kotlin_version = ‘1.3.21' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

ext.kotlin_version = '1.3.21' 类路径“org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”

Apps build.gradle

应用程序 build.gradle

apply plugin: 'kotlin-android-extensions'

应用插件:'kotlin-android-extensions'

apply plugin: 'kotlin-android'

应用插件:'kotlin-android'

//Kotlin

//科特林

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

实现“org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version”

Referance : https://medium.com/mindorks/enabling-kotlin-support-for-a-current-only-java-android-project-98c75e04384a

参考:https://medium.com/mindorks/enabling-kotlin-support-for-a-current-only-java-android-project-98c75e04384a

回答by Rishabh Sagar

If you are facing this issue while using Kotlinand have

如果您在使用Kotlin 时遇到这个问题并且有

kotlin.incremental=true
kapt.incremental.apt=true

in the gradle.properties, then you need to remove this temporarily to fix the build.

gradle.properties 中,那么您需要暂时删除它以修复构建。

After the successful build, you can again add these properties to speed up the build time while using Kotlin.

成功构建后,您可以再次添加这些属性以加快使用 Kotlin 时的构建时间。

Reference: https://stackoverflow.com/a/58951797/3948854

参考:https: //stackoverflow.com/a/58951797/3948854