简体   繁体   English

我如何开始使用此Java库?

[英]How do I begin to use this Java library?

Teaching myself Java, still very new. 自学Java,还是很新的。 Last night I learned how to import this library into my project in Android Studio. 昨晚,我学习了如何将此库导入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. 我知道Java与类一起使用,并且据说库只是类的集合(也许还有其他东西……),一旦导入它就可以开始使用。 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. 因此,我尝试在应用程序的文件中创建Java类,但仍然出现红色错误。 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. 我遵循以下答案: https : //stackoverflow.com/a/35369267/5241266并使用了方法2。

MainActivity.Java: this is where I put the import code. MainActivity.Java:这是我放置import代码的地方。

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: Gradle控制台错误:

I get the following error when doing Build -> Make Project: 在执行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. 如果您不想按照说明中的说明使用JitPack方法,请查看Github示例中的settings.gradle文件。

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. 然后,一旦设置好,您就可以在app/build.gradle文件的依赖项部分中compile project(:libraryName) Again, see the Github sample for the syntax. 再次,请参见Github示例以获取语法。

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 我还建议您仔细阅读github项目中包含的示例代码

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 查找名称为buid.gradle的 gradle文件(项目:[YOUR_PROJECT_NAME]),然后查找并添加以下行

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

You are done 50% now. 您现在完成了50%。 Now press SHIFT+CTRL+ALT+S and wait for a window. 现在按SHIFT + CTRL + ALT + S并等待一个窗口。

On the Window, select DEPENDENCY TAB on top. 在窗口上,选择顶部的DEPENDENCY TAB

Click on + BUTTON at top right. 点击右上角的+按钮 Click on Library Dependency. 单击库依赖性。

Now paste com.github.yesidlazaro:GmailBackground:1.2.0 there. 现在在其中粘贴com.github.yesidlazaro:GmailBackground:1.2.0

Click on OK and exit the window. 单击确定,然后退出窗口。 Gradle recompiles. Gradle重新编译。

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. 现在,只需输入您的代码,如果显示RED语法错误,请单击显示错误的代码,并在出现蓝色弹出消息时,按ALT + ENTER。

Everything done..! 一切都完成了!

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

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