简体   繁体   English

将.java库添加到Android项目

[英]Adding .java library to an Android project

I have a library I want to add to my Android Application. 我有一个要添加到我的Android应用程序中的库。 But the problem is that the library is separated into .java files. 但是问题在于该库被分为.java文件。 Now I have tried to add a module to do the job but it does not work. 现在,我尝试添加一个模块来完成这项工作,但是它不起作用。 My problem is I do not know how to add a bunch of .java files to my Android project to use throughout my app. 我的问题是我不知道如何在我的Android项目中添加一堆.java文件以在整个应用中使用。 I am new to Android dev and I have looked around but cannot find a clear explanation of how to add my type of library to an Android project. 我是Android开发人员的新手,我环顾四周,但是找不到如何将类库添加到Android项目的清晰说明。 在此处输入图片说明

I can see a couple of problems in the screenshot that you just posted above. 在上面刚刚发布的屏幕截图中,我可以看到一些问题。

1) It seems that you're not telling Gradle (and therefore Android Studio) that you have Java files that need to be compiled. 1)似乎您并没有告诉Gradle(因此也没有告诉Android Studio)您有需要编译的Java文件。 You can fix that by adding this to the build.gradle of the TMDB-Lib module: 您可以通过将其添加到TMDB-Lib模块的build.gradle中来解决此问题:

sourceSets {
  main {
    java {
      srcDir 'libs'
    }
  }
}

and then adding the module to your settings.gradle . 然后将该模块添加到您的settings.gradle

2) It seems that the Tmdb classes don't have a package (ie their first line isn't a package declaration) so the right way to import them into your Java code is by using just their class name: 2) Tmdb类似乎没有包(即,它们的第一行不是包声明),因此将它们导入到Java代码中的正确方法是仅使用它们的类名:

import TmdbApi;

instead of: 代替:

import TMDB-lib.libs.TmdbApi;

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

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