将外部.jar添加到androidstudio项目中

was*_*256 4 java android gradle android-studio

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

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

我用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.+'
  }
Run Code Online (Sandbox Code Playgroud)

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

  Gradle: error: package bsh does not exist
  Gradle: error: cannot find symbol class Interpreter
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我吗?

Sil*_*uti 8

试试这个...

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

1.创建libs文件夹:

在此输入图像描述

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

在此输入图像描述

3.编辑app的build.gradle依赖:

  • 打开app/build.gradle

在此输入图像描述

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

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

在此输入图像描述

快乐的编码....