在Android Pre L设备上使用Java 8和Jack编译器的方法

3
我正在更新我的应用程序以便与Java 8兼容。因此,我正在使用jack编译器。
这对于每个具有Android 5.0或更高版本的设备都完美地工作。在旧版Pre Lollipop设备上,我遇到了以下异常:
由于:java.lang.ClassNotFoundException: 在路径上未找到类“de.gelbeseiten.android.GelbeSeitenApplication”: DexPathList[[zip文件“/data/app/de.gelbeseiten.android-1.apk”],nativeLibraryDirectories=[/data/app-lib/de.gelbeseiten.android-1,/vendor/lib,/system/lib,/system/lib/arm]] 在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)中引起 在java.lang.ClassLoader.loadClass(ClassLoader.java:497)中加载类 在java.lang.ClassLoader.loadClass(ClassLoader.java:457)中加载类 在android.app.Instrumentation.newApplication(Instrumentation.java:981)中 在android.app.LoadedApk.makeApplication(LoadedApk.java:511)中创建应用程序 在android.app.ActivityThread.handleBindApplication(ActivityThread.java:4722)中处理绑定应用程序 在android.app.ActivityThread.access$1600(ActivityThread.java:172)中访问 在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)中 在android.os.Handler.dispatchMessage(Handler.java:102)中分发消息 在android.os.Looper.loop(Looper.java:146)中循环 在android.app.ActivityThread.main(ActivityThread.java:5653)中 在java.lang.reflect.Method.invokeNative(Native Method)中调用反射方法 在java.lang.reflect.Method.invoke(Method.java:515)中调用方法
我99%确定,这是Java 8的改变导致的,如果我只检出当前公共版本并仅包括Java 8,则会发生这种情况。
不幸的是,我在谷歌上找不到任何有关此事的信息。在谷歌Android网站上,有关Java 8支持的页面中,并没有写明需要最低的Android版本。 https://developer.android.com/guide/platform/j8-jack.html 我认为完整的build.gradle文件并没有用处,但这里有一段代码片段:
android {
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        [...]
        multiDexEnabled = true
        jackOptions {
            enabled true
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

我从未手动更改过proguard-rules.txt文件,但是这里有它的内容:

# Add project specific ProGuard rules here.
# By teilnehmer, the flags in this file are appended to flags     specified
# in /Applications/Android Studio.app/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

##---------------Begin: proguard configuration common for all Android apps ----------
-dontobfuscate
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations             !code/simplification/arithmetic,!field/*,!class/merging/*

-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses
-libraryjars libs

# The official support library.
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }

# Library JARs.
-keep class de.greenrobot.dao.** { *; }
-keep interface de.greenrobot.dao.** { *; }
-keep class org.joda.** { *; }
-keep interface org.joda.** { *; }
-keep class com.loopj.android.http.** { *; }
-keep interface com.loopj.android.http.** { *; }

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService

# Espress: java.lang.NoSuchMethodError:     android.support.test.espresso.Espresso
-keep class android.support.v7.widget.RecyclerView { *; }

# adjust
-keep class com.adjust.sdk.** { *; }
-keep class com.google.android.gms.common.** { *; }
-keep class com.google.android.gms.ads.identifier.** { *; }

-keep class com.adjust.sdk.plugin.MacAddressUtil {
    java.lang.String getMacAddress(android.content.Context);
}
-keep class com.adjust.sdk.plugin.AndroidIdUtil {
    java.lang.String getAndroidId(android.content.Context);
}
-keep class com.google.android.gms.common.ConnectionResult {
    int SUCCESS;
}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient {
    com.google.android.gms.ads.identifier.AdvertisingIdClient$Info getAdvertisingIdInfo (android.content.Context);
}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info {
    java.lang.String getId ();
    boolean isLimitAdTrackingEnabled();
}

#keep all classes that might be used in XML layouts
-keep public class * extends android.view.View
-keep public class * extends android.app.Fragment
-keep public class * extends android.support.v4.Fragment

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

-keepclassmembers class fqcn.of.javascript.interface.for.webview {
   public *;
}


-keep class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}

# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
native <methods>;
}


-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}

# Preserve static fields of inner classes of R classes that might be     accessed
# through introspection.
-keepclassmembers class **.R$* {
public static <fields>;
}

# Preserve the special static methods that are required in all     enumeration classes.
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keep public class * {
public protected *;
}

-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
##---------------End: proguard configuration common for all Android apps ----------

##---------------Begin: proguard configuration for Gson ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by teilnehmer, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson ----------

Stackoverflow将文件的一部分显示为注释,但在我的Android Studio中的proguard文件中并非如此。
那么...有人对这个话题有更多的信息吗?或者甚至有一个修复方案可以使应用程序在Pre L设备上运行吗?
提前致谢! Ben

你是否在使用Proguard和Multidex?请发布你的Gradle和Proguard文件。 - AndroidRuntimeException
我正在使用multidex,是的。已将两者都添加到原始帖子中。 - Benjamin Menrad
我认为Jack需要ART。ART在KitKat上可用,但需要激活(不是默认VM)。 - JSON
1个回答

0
首先,如果您正在使用即时运行,请禁用它。目前不支持Java 8。
此外,您应该添加:
-keep class de.gelbeseiten.android.** { *; }

同时确保 de.gelbeseiten.android.GelbeSeitenApplication 在你的主dex文件中(可能是 classes.dex)。

如果不在这里,你应该做一些像这样的事情:

android {
    buildTypes {
        debug {
            ...
            multiDexEnabled true
            multiDexKeepFile file('multidex_keep_file.txt')
        }
    }
}

在你的 multidex_keep_file.txt 文件中,你需要指定哪些类将会在你的主 dex 中。
 de/gelbeseiten/android/GelbeSeitenApplication.class
 de/gelbeseiten/android/MainActivity.class (for example)

这是一个关于编程的文本文件,它指定了将被编译到主dex文件中的额外类。文件中指定的类将会被添加到使用aapt计算出的主dex类之后。如果设置了该文件,则应按照以下格式,在每行中包含一个类:com/example/MyClass.class。

很遗憾,这不起作用。因为该应用程序适用于搭载Android 5或更高版本的设备,所以必须将应用程序和活动包含在.dex文件中。 - Benjamin Menrad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接