简体   繁体   English

Android Studio外部库

[英]Android Studio external library

I'm starting to do some projects with this IDE and still I find several anoying situations that where very easy to solve with eclipse but that I don't know how to face here. 我开始使用此IDE进行一些项目,但仍然发现一些烦人的情况,使用eclipse可以很容易地解决这些问题,但是我不知道该如何面对。

I'm implementing mail sending functionality to my app. 我正在向我的应用程序实现邮件发送功能。 This must be transparent for the user, so I'm using javax.mail. 这对于用户必须是透明的,因此我正在使用javax.mail。

For this, I have downloaded the "activation.jar", "additionnal.jar" and "mail.jar" files and I have save them in the libs folder. 为此,我下载了“ activation.jar”,“ additionnal.jar”和“ mail.jar”文件,并将它们保存在libs文件夹中。

Now I'm creating the custom sender class, which extendes javax.mail.Authenticator . 现在,我正在创建自定义发送程序类,该类扩展了javax.mail.Authenticator But it does not find the import for the javax.mail , mail is appearing in red. 但是它找不到javax.mail的导入,邮件显示为红色。 I'm triying to import it from the libs but it does not find them. 我正在尝试从库中导入它,但是找不到它们。

I have also included this in gradle.build: 我也将此包含在gradle.build中:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

I have readed in several questions like this, that the way is to right click on the library and click on Add as library , but this option is not appearing for me. 我已经读过几个这样的问题,那就是右键单击库,然后单击“ 添加为库” ,但是这个选项对我而言并不显示。

So, whats the way of making Android Studio recognize these libraries? 那么,使Android Studio识别这些库的方法是什么?

在此处输入图片说明

I would encourage you to use an artifactory like Sonatype Nexus is for instance. 我鼓励您使用人工制品,例如Sonatype Nexus。 In here you would deploy and retrive your artifacts and artifacts from other remote artifactories. 在这里,您将部署并从其他远程工件检索工件。 But for now may just use MavenCentral. 但目前可能仅使用MavenCentral。

In your top level build.gradle you would need to configure a repository where your artifacts reside. 在顶层build.gradle您需要配置工件所在的存储库。

repositories {
    mavenCentral() //a public artifactory
    maven {
        url "https://your.privaterepo.com"
    }
}

In your project level build.gradle you would add a dependency for javax.mail like 在您的项目级别build.gradle您将为javax.mail添加一个依赖项,例如

dependencies {
    compile 'javax.mail:mail:1.5.0-b01' // G:A:V
}

For looking up GAV-coordinates of artifacts ( G roup A rtifact V ersion) you could use just google, which often points you to http://mvnrepository.com/ where you can easily pick up dependencies in gradle notation. 为了查找文物GAV坐标(G roup 一个 rtifact V版为)你可以使用只是谷歌,这往往指向你http://mvnrepository.com/在那里你可以很容易地拿起gradle这个符号的依赖。

You then will never need to download artifacts manually, gradle will handle this for you. 然后,您将不需要手动下载工件,gradle会为您处理。 All you would have to do is, to declare the repository and the dependencies. 您要做的就是声明存储库和依赖项。

For deploying artefacts from gradle, I use a goal uploadArchives in my module level build.gradle: 从gradle这个部署文物,我用一个进球uploadArchives在我的模块级的build.gradle:

apply plugin: 'maven'
//noinspection GroovyMissingReturnStatement
uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: 'http://our.private.nexus/nexus/content/repositories/releases') {
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD);
            }
            snapshotRepository(url: 'http://our.private.nexus/nexus/content/repositories/snapshots') {
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD);
            }
            pom.groupId = "the.groupid.for.this.artifact"
            pom.artifactId = "artifactid"
            pom.version = "1.2.3"
        }
    }
}

if you have added libraries into lib folder in src . 如果您已将库添加到src lib文件夹中。 It will not going to work. 它不会起作用。

Click on android in project showcase and select Project , make new folder as libs and then add all third party libraries into it after that when you will right click on it, you will get option to add it as a library. 在项目展示区中单击android并选择Project ,将新文件夹创建为libs ,然后将所有第三方库添加到其中,然后右键单击它,您将获得将其添加为库的选项。

You also dont need to write in gradle file after adding it. 添加完后,您也不需要在gradle文件中写入文件。 Android Studio automatically add line itself. Android Studio会自动添加行本身。

I usually do a trick with import *.jar libs. 我通常会使用导入* .jar库来欺骗。 After put *.jar files in libs folder. 将* .jar文件放入libs文件夹后。 I delete some characters in compile fileTree(dir: 'libs', include: ['*.jar']) to compile fileTree(dir: 'libs', include: ['*.j']) and rewrite it to compile fileTree(dir: 'libs', include: ['*.jar']) . 我删除了compile fileTree(dir: 'libs', include: ['*.jar'])某些字符以compile fileTree(dir: 'libs', include: ['*.j'])并将其重写以compile fileTree(dir: 'libs', include: ['*.jar']) It's a way to sync my build.gradle :) 这是一种同步我的build.gradle的方法:)

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

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