如何在 Android 项目中使用 ThreeTenABP [英] How to use ThreeTenABP in Android Project

查看:34
本文介绍了如何在 Android 项目中使用 ThreeTenABP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个问题是因为我是 Java 和 Android 的新手,我搜索了几个小时试图解决这个问题.答案来自相关答案的组合,所以我想我会记录我所学到的东西,供其他可能遇到困难的人使用.查看答案.

I'm asking this question because I'm new to Java and Android and I searched for hours trying to figure this out. The answer came from a combination of related answers, so I figured I would document what I learned for anyone else who may be struggling. See answer.

我使用的是 Android Studio 2.1.2,我的 Java 设置如下:

I'm using Android Studio 2.1.2 and my Java setup is the following:

>java -version
> openjdk version "1.8.0_91"
> OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~15.10.1-b14)
> OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

推荐答案

注意:这个答案虽然在技术上是正确的,但现在已经过时了

Java 8+ API 脱糖支持现在可通过 Android Gradle 插件 4.0.0+ 获得

(另见Basil Bourque 在下面的回答)

ThreeTenABP 库 的开发即将结束.请考虑在未来几个月内改用 Android Gradle 插件 4.0、java.time.* 及其核心库脱糖功能.

Development on the ThreeTenABP Library is winding down. Please consider switching to Android Gradle plugin 4.0, java.time.*, and its core library desugaring feature in the coming months.

要在任何版本的 Android 平台上启用对这些语言 API 的支持,请更新 Android 插件到 4.0.0(或更高版本) 并在模块的 build.gradle 文件中包含以下内容:

To enable support for these language APIs on any version of the Android platform, update the Android plugin to 4.0.0 (or higher) and include the following in your module’s build.gradle file:

android {
  defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled true
  }

  compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.5'
}


原答案

首次发现:为什么必须使用 ThreeTenABP 而不是 java.timeThreeTen-Backport 甚至 Joda-时间

这是定义新标准的漫长过程的一个非常简短的版本.所有这些包几乎都是一样的:为 Java 提供良好的现代时间处理功能的库.这些差异很微妙但很重要.


Original Answer

First Discovery: Why You Have To Use ThreeTenABP Instead of java.time, ThreeTen-Backport, or even Joda-Time

This is a really short version of the VERY LONG PROCESS of defining a new standard. All of these packages are pretty much the same thing: libraries that provide good, modern time handling functionality for Java. The differences are subtle but important.

最明显的解决方案是使用内置的 java.time 包,因为这是在 Java 中处理时间和日期的新标准方法.它是 JSR 310 的实现,这是一个新的时间处理标准提案基于 Joda-Time 库.

The most obvious solution would be to use the built-in java.time package, since this is the new standard way to deal with time and dates in Java. It is an implementation of JSR 310, which was a new standard proposal for time handling based on the Joda-Time library.

然而,java.time 是在 Java 8.Android 最高 Marshmallow 在 Java 7 上运行(Android N"是第一个引入 Java 的版本8 种语言功能).因此,除非您只针对 Android N Nougat 及更高版本,您不能依赖 Java 8 语言功能(我实际上不确定这是否 100% 正确,但我是这样理解的).所以java.time出来了.

However, java.time was introduced in Java 8. Android up to Marshmallow runs on Java 7 ("Android N" is the first version to introduce Java 8 language features). Thus, unless you're only targeting Android N Nougat and above, you can't rely on Java 8 language features (I'm not actually sure this is 100% true, but this is how I understand it). So java.time is out.

下一个选项可能是 Joda-Time,因为JSR 310 基于 Joda-Time.但是,正如 ThreeTenABP 自述文件 指出的那样,出于多种原因,Joda-时间不是最好的选择.

The next option might be Joda-Time, since JSR 310 was based on Joda-Time. However, as the ThreeTenABP readme indicates, for a number of reasons, Joda-Time is not the best option.

接下来是 ThreeTen-Backport,它向后移植了很多(但不是全部)Java 8 java.time 功能到 Java 7.这适用于大多数用例,但是,如 ThreeTenABP 自述文件,它在 Android 上存在性能问题.

Next is ThreeTen-Backport, which back-ports much (but not all) of the Java 8 java.time functionality to Java 7. This is fine for most use cases, but, as indicated in the ThreeTenABP readme, it has performance issues with Android.

所以最后一个看似正确的选项是 ThreeTenABP.

So the last and seemingly correct option is ThreeTenABP.

由于编译程序——尤其是使用一堆外部库的程序——很复杂,Java 几乎总是使用 构建工具"来管理该过程.制作Apache AntApache MavenGradle 都是与 Java 程序一起使用的构建工具(参见 this发布进行比较).如下所述,Gradle 是 Android 项目的首选构建工具.

Since compiling a program -- especially one using a bunch of external libraries -- is complex, Java almost invariably uses a "build tool" to manage the process. Make, Apache Ant, Apache Maven, and Gradle are all build tools that are used with Java programs (see this post for comparisons). As noted further down, Gradle is the chosen build tool for Android projects.

这些构建工具包括依赖管理.Apache Maven 似乎是第一个包含集中式包存储库的.Maven 引入了 Maven 中央存储库,它允许使用 Packagist 和 php 的 composer 等价功能Ruby 的 gem 与 ruby​​gems.org.换句话说,Maven Central Repository 之于 Maven(和 Gradle),就像 Packagist 之于 Composer —— 版本化软件包的权威且安全的来源.

These build tools include dependency management. Apache Maven appears to be the first to include a centralized package repository. Maven introduced the Maven Central Repository, which allows functionality equivalent to php's composer with Packagist and Ruby's gem with rubygems.org. In other words, the Maven Central Repository is to Maven (and Gradle) what Packagist is to composer -- a definitive and secure source for versioned packages.

在我的待办事项列表中最重要的是阅读 Gradle 文档这里,包括他们的免费电子书.如果我在这几周前开始学习 Android 时阅读了这些内容,我肯定会知道 Gradle 可以使用 Maven 中央存储库来管理 Android 项目中的依赖项.此外,如 this StackOverflow 答案中所述,从 Android Studio 0.8.9 开始,Gradle 通过 Bintray 的 JCenter 隐式使用 Maven Central Repository,这意味着您无需进行任何额外的配置来设置存储库——您只需列出依赖项即可.

High on my to-do list is to read the Gradle docs here, including their free eBooks. Had I read these weeks ago when I started learning Android, I would surely have known that Gradle can use the Maven Central Repository to manage dependencies in Android Projects. Furthermore, as detailed in this StackOverflow answer, as of Android Studio 0.8.9, Gradle uses Maven Central Repository implicitly through Bintray's JCenter, which means you don't have to do any extra config to set up the repo -- you just list the dependencies.

同样,对于那些在 Java 中使用过 Gradle 的人来说是显而易见的,但我花了一段时间才弄明白.如果您看到人们说哦,只需添加 compile 'this-or-that.jar'"或类似的东西,要知道 compile 是该 build.gradle 文件中的一个指示编译时依赖项的指令.这里是关于依赖管理的官方 Gradle 页面.

Again, obvious to those who have any experience using Gradle in Java, but it took me a while to figure this out. If you see people saying "Oh, just add compile 'this-or-that.jar'" or something similar, know that compile is a directive in that build.gradle file that indicates compile-time dependencies. Here's the official Gradle page on dependency management.

还有一个问题,我花了太多时间弄明白.如果您在 Maven Central 中查找 ThreeTen,您将只会看到 threetenbp 的包,而不是 threetenabp.如果您访问 ThreeTenABP 的 github 存储库,您会看到臭名昭著的 compile 'this-或者-那个' 行在自述文件的下载部分下.

Yet another issue I spent too much time figuring out. If you look for ThreeTen in Maven Central, you'll only see packages for threetenbp, not threetenabp. If you go to the github repo for ThreeTenABP, you'll see that infamous compile 'this-or-that' line under the Download section of the Readme.

当我第一次点击这个 github repo 时,我不知道那条编译行是什么意思,我试图在我的终端中运行它(明显且可预测的失败).沮丧的是,直到我想通了其余部分后,我才回到它,最终意识到这是一个指向 com.jakewharton.threetenabp 存储库的 Maven Repo 行,而不是 org.threeten 存储库.这就是为什么我认为 ThreeTenABP 包不在 Maven 存储库中的原因.

When I first hit this github repo, I didn't know what that compile line meant, and I tried to run it in my terminal (with an obvious and predictable failure). Frustrated, I didn't return to it until long after I figured the rest out, and finally realized that it's a Maven Repo line pointing to the com.jakewharton.threetenabp repo, as opposed to the org.threeten repo. That's why I thought the ThreeTenABP package wasn't in the Maven repo.

现在一切看起来都很简单.通过确保您的 [项目文件夹]/app/build.gradle 文件具有 implementation 'com.jakewharton.threetenabp:threetenabp:1.2,您可以在 Android 项目中获得现代时间处理功能.1' 行在其 dependencies 部分:

Now it all seems pretty easy. You can get modern time handling functions in an Android project by making sure your [project folder]/app/build.gradle file has the implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1' line in its dependencies section:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "me.ahuman.myapp"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:23.4.0'
    implementation 'com.android.support:design:23.4.0'
    implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'
}

还要将此添加到 Application 类:

Also add this to Application class:

public class App extends Application {    
    @Override
    public void onCreate() {
        super.onCreate();
        AndroidThreeTen.init(this);
        //...
    }
}

这篇关于如何在 Android 项目中使用 ThreeTenABP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆