简体   繁体   中英

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.

I'm implementing mail sending functionality to my app. This must be transparent for the user, so I'm using 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.

Now I'm creating the custom sender class, which extendes javax.mail.Authenticator . But it does not find the import for the javax.mail , mail is appearing in red. I'm triying to import it from the libs but it does not find them.

I have also included this in 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?

在此处输入图片说明

I would encourage you to use an artifactory like Sonatype Nexus is for instance. In here you would deploy and retrive your artifacts and artifacts from other remote artifactories. But for now may just use MavenCentral.

In your top level build.gradle you would need to configure a repository where your artifacts reside.

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

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.

You then will never need to download artifacts manually, gradle will handle this for you. 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:

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 . 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.

You also dont need to write in gradle file after adding it. Android Studio automatically add line itself.

I usually do a trick with import *.jar libs. After put *.jar files in libs folder. 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']) . It's a way to sync my build.gradle :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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