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

查看:206
本文介绍了如何在Android Project中使用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 Plugin 4.0.0 +)

此库( ThreeTenABP )的开发正在逐渐减少.请考虑在未来几个月内切换到Android Gradle插件4.0,java.time.*及其核心库终止功能.

Attention: Java 8+ API desugaring support (Android Gradle Plugin 4.0.0+)

Development on this library(ThreeTenABP) 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的支持,请更新

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.time ThreeTen-Backport 甚至是 Joda-时间

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


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在Java 7上运行("Android N"是第一个引入该版本的版本Java 8语言功能).因此,除非您仅定位 Android N 牛轧糖 及更高版本,您不能依赖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-Time不是最好的选择.

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 7的Java 8 java.time功能.这对于大多数用例都很好,但是,如

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 Maven Gradle 都是与Java程序一起使用的构建工具(请参见

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 Central Repository ,该功能允许使用与Packagist和Ruby的composer和Ruby的gem等效的功能.与rubygems.org.换句话说,Maven Central Repository就是Maven(和Gradle)Packagist的创作者,这是版本包的权威性和安全性来源.

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 Central信息库来管理Android项目中的依赖项.此外,如 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-or-that'行在自述文件的下载部分.

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仓库时,我不知道该编译行是什么意思,所以我尝试在终端中运行它(出现了明显且可预见的故障).沮丧的是,直到弄清其余部分后,我才回到很长一段时间,最后才意识到这是一条指向com.jakewharton.threetenabp repo的Maven Repo行,而不是org.threeten repo.这就是为什么我认为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.

现在,一切似乎都很容易.通过确保您的[project folder]/app/build.gradle文件在其dependencies部分中包含implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'行,您可以在Android项目中获得现代时间处理功能:

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 Project中使用ThreeTenABP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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