简体   繁体   English

Android Studio导入模块Gradle构建错误

[英]Android Studio Import Module Gradle Build Error

I am attempting to add a directory as a dependency in Android Studio(GameBaseUtils). 我试图在Android Studio(GameBaseUtils)中添加一个目录作为依赖项。 I have seen other SO answers simply posting the correct gradle configuration for their particular issue, however I don't understand how I can adapt their answers to my situation. 我已经看到其他SO答案只是为他们的特定问题发布了正确的gradle配置,但是我不明白我如何能够根据我的情况调整他们的答案。

Here is what I did: 这是我做的:

Step one: File-> Import Module ->Navigate to directory and select it. 第一步:文件 - >导入模块 - >导航到目录并选择它。

Step Two-: File-> Project Structure-> Modules-> Select my application->Dependencies->Add the module as a dependency to my project. 第二步 - 文件 - >项目结构 - >模块 - >选择我的应用程序 - >依赖项 - >将模块添加为项目的依赖项。

Now my code doesn't have any red lines indicating an error importing the module. 现在我的代码没有任何红线表示导入模块时出错。 However when I select build I get the following errors: 但是,当我选择构建时,我收到以下错误:

Gradle: package com.google.example.games.basegameutils does not exist
Gradle: cannot find symbol class BaseGameActivity
Gradle: cannot find symbol variable super
...

Here is the build.gradle file for my application 这是我的应用程序的build.gradle文件

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

dependencies {
compile files('libs/android-support-v4.jar')
}

android {
compileSdkVersion 17
buildToolsVersion "17.0.0"

defaultConfig {
    minSdkVersion 8
    targetSdkVersion 17
}
}

How can I correctly import this external library and can you please explain how and why your solution works? 如何正确导入此外部库,您能解释一下解决方案的工作原理和原因吗?

so here is how I solved my problem: 所以这就是我如何解决我的问题:

instead of adding 而不是添加

dependencies {
    compile files('libs/android-support-v4.jar')
    compile project(':Module')
}

You have to write: 你必须写:

dependencies {
    compile files('libs/android-support-v4.jar', '../Module')
}

the 2 dots say that the Module (or directory) can be found in 1 directory above the actual one. 2个点表示模块(或目录)可以在实际的目录上方的1个目录中找到。 so if you want to access a module which is 2 directories above you just have to write: '../../ModuleName' 所以如果你想访问上面2个目录的模块,你只需要写: '../../ModuleName'

You have to add the modules manually to the build.gradle because Android Studio is still in development and doesn't have finished the UI for editing the Project Structure. 您必须手动将模块添加到build.gradle,因为Android Studio仍在开发中,并且尚未完成用于编辑项目结构的UI。

If this does not solve your problem try to make it like this: (I would recommend this method. This is how I actually do it) 如果这不能解决你的问题,试着这样做:(我会推荐这种方法。这就是我实际做的方法)

Examplestructure: Examplestructure:

  • Project 项目

    • libraries (normal folder) 库(普通文件夹)
      • Module2 单词数
    • Module1 模块1

settings.gradle settings.gradle

include ':Module1', ':libraries:Module2'

build.gradle (Module1) build.gradle (Module1)

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

dependencies {
    compile project(':libraries:Module2')
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 11
    }
}

build.gradle (Module2) build.gradle (Module2)

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

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 11
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

This should work well now. 这应该现在很好用。 To make everything work 100% follow this steps: 要使一切正常工作,请按照以下步骤操作:

  1. delete .idea folder 删除.idea文件夹
  2. delete all *.iml files 删除所有*.iml文件
  3. Restart Android Studio and press Import Project 重启Android Studio并按“ Import Project
  4. Select the directory with your gradle project 选择gradle项目的目录
  5. Import project from external model > Gradle > next > finish Import project from external model > Gradle> next>完成

With this steps everything should work well. 通过这些步骤,一切都应该很好。 If there are any problems just tell me :) 如果有任何问题请告诉我:)

Do not add modules through the Studio interface. 不要通过Studio界面添加模块。 Always make the changes directly in build.gradle and then reimport into Studio. 始终直接在build.gradle中进行更改,然后重新导入Studio。

Also, update the plugin dependency to com.android.tools.build:gradle:0.4.+ to get the latest 0.4.* version. 此外,将插件依赖项更新为com.android.tools.build:gradle:0.4.+以获取最新的0.4。*版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM