简体   繁体   中英

How do I begin to use this Java library?

Teaching myself Java, still very new. Last night I learned how to import this library into my project in Android Studio. But now I'm confused as to how to actually start using it.

I know that Java works with classes and that supposedly a library is just a collection of classes (and probably others things too...) that you can begin using once you import it. But the author of this library told me to just use this:

BackgroundMail bm = new BackgroundMail(context);
bm.setGmailUserName("sendername@gmail.com");
bm.setGmailPassword("sender_email_password");
bm.setMailTo("receiver@gmail.com");
bm.setFormSubject("Subject");
bm.setFormBody("Body");
bm.send();

But when I try to put that into another one of my classes, I get red errors all over the place. So then I tried to create a Java class within my app's files and I still got red errors. Could someone kindly help me, a total beginner, get started here? I'd like to use this library to send an email in the background of my app.

To import the library:

I followed this answer: https://stackoverflow.com/a/35369267/5241266 and used Approach 2.

MainActivity.Java: this is where I put the import code.

package moviehelper.moviesfree;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

For now, I have not added the block of code that the author told me to use (see above).

The Gradle Console error:

I get the following error when doing Build -> Make Project:

error: package com.creativityapps.gmailbackgroundlibrary does not exist

import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;

My project tree: I think there might be some problem with this structure. It looks like the library was added as its own project? Although I'm not sure.

在此处输入图片说明

If you don't want to use the JitPack approach as documented in the instructions, then look at the settings.gradle file in the Github example.

It includes both modules (app and the library).

Then, once that is setup, you can compile project(:libraryName) in the app/build.gradle file's dependencies section. Again, see the Github sample for the syntax.

With those two steps (plus one to download the library), it should be importable within the app code.

Assuming you followed along with the other steps on the readme, at the top of your code you need to tell the source file to import the classes:

import com.creativityapps.gmailbackgroundlibrary.BackgroundMail;

public class MainActivity extends AppCompatActivity {
    [your code]
}

I would also recommend looking through the sample code included in the github project

You are doing some wrong steps friend. You can follow this

Find your gradle file with name buid.gradle (Project: [ YOUR_PROJECT_NAME ] ) Then find and add this line

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

You are done 50% now. Now press SHIFT+CTRL+ALT+S and wait for a window.

On the Window, select DEPENDENCY TAB on top.

Click on + BUTTON at top right. Click on Library Dependency.

Now paste com.github.yesidlazaro:GmailBackground:1.2.0 there.

Click on OK and exit the window. Gradle recompiles.

Now simply put your code, If RED syntax error showing, Click at the code where error is showing and when a BLUE pop comes, Press ALT+ENTER.

Everything done..!

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