Android Studio库"错误:包不存在"

Rob*_*kus 34 android android-studio android-gradle-plugin

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

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
    }

    ***
}
Run Code Online (Sandbox Code Playgroud)

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'])
}
Run Code Online (Sandbox Code Playgroud)

settings.gradle

include ':libraries:mylibrary'
Run Code Online (Sandbox Code Playgroud)

PS我必须提到项目是从Eclipse IDE导出的,因此项目结构与默认项目结构不同.

Sco*_*rta 14

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

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

  • 嗯,我的库模块中出现了这个错误.它不使用`apply plugin:'com.android.application' (14认同)

Cod*_*ife 14

适用于Android Studio 2.2.2

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

但是你必须做以下事情:

  1. 在两个模块的Gradle文件中公开相同的SDK版本.
  2. 右键单击项目"app"模块文件夹,然后单击 - >打开模块设置
  3. 单击"依赖项"选项卡
  4. 单击+号以添加新依赖项并选择"Module Dependency"
  5. 寻找您需要的库并添加它.

同时命名您的lib模块时请避免使用大写字母.


小智 5

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

  • 工具选项卡 Kotlin 和配置 Kotlin
  • (选择 Android with Gradle)后选择模块

项目 build.gradle 添加

ext.kotlin_version = ‘1.3.21’
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Run Code Online (Sandbox Code Playgroud)

应用程序 build.gradle

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-android'
Run Code Online (Sandbox Code Playgroud)

科特林

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Run Code Online (Sandbox Code Playgroud)

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