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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18520875/
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-12 08:43:34  来源:igfitidea点击:

Adding external .jar to androidstudio project

javaandroidgradleandroid-studio

提问by wasp256

I've added the external library bsh-2.0b4.jarto an android project in android-studio by going into Project structure -> Modules -> myProject -> Tab-Dependencies -> + Signand then add the .jar file. I also tried to copy the file into the /libs directory and then rightclick in studio and add as library...(both methods independently!!). I inserted the following code as a test

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

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

I compile with the buildin button in android. The build.gradle looks like:

我用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.+'
  }

But when I compile everything I receive the error

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

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

Can anyone help me pls?

有人可以帮我吗?

采纳答案by Grzegorz ?ur

Change your dependencies like that

像这样更改您的依赖项

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

You can now remove manually downloaded dependencies in libsdirectory.

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

What you did was adding libraries to Android Studio project only. You should always add them to Gradle build files as only this is interpreted by Android Build Tools.

您所做的只是将库添加到 Android Studio 项目中。您应该始终将它们添加到 Gradle 构建文件中,因为 Android 构建工具仅对此进行解释。

There is also new version of build tools 18.0.1, you can install them and change version in you build.gradle. As far as I know they can handle aardependencies better.

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

回答by Silambarasan Poonguti

Try this...

尝试这个...

  1. Create libs folder under your application folder.
  2. Add .jar files to libs folder.
  3. Then add .jar files to app's build.gradle dependency.
  4. Finally Sync project with Gradle files.
  1. 在您的应用程序文件夹下创建 libs 文件夹。
  2. 将 .jar 文件添加到 libs 文件夹。
  3. 然后将 .jar 文件添加到应用程序的 build.gradle 依赖项中。
  4. 最后将项目与 Gradle 文件同步。

1.Create libs folder:

1.创建libs文件夹:

enter image description here

在此处输入图片说明

2.Add .jar to libs folder:

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

enter image description here

在此处输入图片说明

3.Edit app's build.gradle dependency:

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

  • Open app/build.gradle
  • 打开 app/build.gradle

enter image description here

在此处输入图片说明

4.Sync project with Gradle files:

4.将项目与 Gradle 文件同步:

  • Finally add .jar files to your application.
  • 最后将 .jar 文件添加到您的应用程序中。

enter image description here

在此处输入图片说明

Happy coding....

快乐编码....