{"id":952,"date":"2023-03-11T19:01:20","date_gmt":"2023-03-11T11:01:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=952"},"modified":"2023-04-29T15:28:38","modified_gmt":"2023-04-29T07:28:38","slug":"using-lambda-expressions-in-android-projects","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/using-lambda-expressions-in-android-projects\/","title":{"rendered":"Android\u9879\u76ee\u4e2d\u4f7f\u7528lambda\u8868\u8fbe\u5f0f"},"content":{"rendered":"<h2>app module\u4e2d\u4f7f\u7528Lambda<\/h2>\n<p>Android\u9879\u76ee\u4e2d\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u6216Java8\u65b0\u7279\u6027\uff0c\u9700\u8981\u5728<code>app\/build.gradle<\/code>\u4e2d\u6dfb\u52a0\u5982\u4e0b\u914d\u7f6e<\/p>\n<p><!-- more --><\/p>\n<pre><code>android {\n    ...\n    defaultConfig {\n        ...\n        jackOptions.enabled = true\n    }\n    compileOptions{\n        sourceCompatibility JavaVersion.VERSION_1_8\n        targetCompatibility JavaVersion.VERSION_1_8\n    }\n}<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\uff1aJack\u7279\u6027\u53ea\u80fd\u5728<code>app module<\/code>\u4e2d\u5f15\u7528\uff0c\u4e0d\u80fd\u5728<code>library modlue<\/code>\u4e2denable<\/p>\n<\/blockquote>\n<p>\u5982\u679c\u5c06\u4e0b\u9762\u4ee3\u7801\u5199\u5728<code>modlue\/build.gradle<\/code>\u4e2d\uff1a<\/p>\n<pre><code>jackOptions {\n    enabled true\n}<\/code><\/pre>\n<p>\u4f1a\u62a5\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre><code>Error:Library projects cannot enable Jack. Jack is enabled in default config.<\/code><\/pre>\n<h2>library module\u4e2d\u4f7f\u7528Lambda<\/h2>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/stackoverflow.com\/questions\/37975128\/is-there-way-to-use-java-8-features-with-android-library-project\">https:\/\/stackoverflow.com\/questions\/37975128\/is-there-way-to-use-java-8-features-with-android-library-project<\/a><\/p>\n<pre><code>\/\/ Java8 not fully supported in library projects yet, https:\/\/code.google.com\/p\/android\/issues\/detail?id=211386\n\/\/ this is a temporary workaround to get at least lambdas compiling\ngradle.projectsEvaluated {\n    tasks.withType(JavaCompile) {\n        options.compilerArgs &lt;&lt; &quot;-Xbootclasspath\/a:&quot; + System.properties.get(&quot;java.home&quot;) + &quot;\/lib\/rt.jar&quot;\n    }\n}\n\nandroid {\n    ...\n    compileOptions {\n        sourceCompatibility JavaVersion.VERSION_1_8\n        targetCompatibility JavaVersion.VERSION_1_8\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>app module\u4e2d\u4f7f\u7528Lambda Android\u9879\u76ee\u4e2d\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u6216Java8\u65b0\u7279\u6027\uff0c\u9700\u8981\u5728a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[202],"tags":[259],"class_list":["post-952","post","type-post","status-publish","format-standard","hentry","category-android-build","tag-lambda"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=952"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/952\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}