将外部.jar添加到android studio项目中

时间:2013-08-29 21:00:04

标签: java android gradle android-studio

我已经通过进入bsh-2.0b4.jar将外部库Project structure -> Modules -> myProject -> Tab-Dependencies -> + Sign添加到a​​ndroid-studio中的android项目中,然后添加.jar文件。 我还尝试将文件复制到/ libs目录,然后右键单击工作室和add as library...(两种方法独立!!)。我插入以下代码作为测试

  import bsh.Interpreter;
  ...
  Interpreter interpreter = new Interpreter();
  interpreter.eval("result = (7+21*6)/(32-27)");
  return interpreter.get("result").toString();

我使用android中的buildin按钮进行编译。 build.gradle看起来像:

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

  repositories {
    mavenCentral()
  }

  android {
     compileSdkVersion 17
     buildToolsVersion "17.0.0"

     defaultConfig {
        minSdkVersion 9
        targetSdkVersion 17
     }
  }

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

但是当我编译所有内容时,我收到了错误

  Gradle: error: package bsh does not exist
  Gradle: error: cannot find symbol class Interpreter

任何人都可以帮我吗?

2 个答案:

答案 0 :(得分:8)

试试这个......

  1. 在应用程序文件夹下创建libs文件夹。
  2. 将.jar文件添加到libs文件夹。
  3. 然后将.jar文件添加到app的build.gradle依赖项。
  4. 最后使用Gradle文件同步项目。
  5. 1.创建libs文件夹:

    enter image description here

    2.将.jar添加到libs文件夹:

    enter image description here

    3.编辑应用的build.gradle依赖关系:

    • 打开app / build.gradle

    enter image description here

    4.使用Gradle文件的同步项目:

    • 最后将.jar文件添加到您的应用程序中。

    enter image description here

    快乐的编码......

答案 1 :(得分:3)

更改您的依赖关系

dependencies {
   compile 'com.android.support:support-v4:13.0.+'
   compile 'org.beanshell:bsh:2.0b4'
}

您现在可以删除libs目录中手动下载的依赖项。

您所做的只是向Android Studio项目添加库。您应该始终将它们添加到Gradle构建文件中,因为这只有Android Build Tools解释。

还有新版本的构建工具18.0.1,您可以在build.gradle中安装它们并更改版本。据我所知,他们可以更好地处理aar依赖关系。