【问题标题】:Usage of Java 8 with jack compiler on Android Pre L devices在 Android Pre L 设备上使用带有 jack 编译器的 Java 8
【发布时间】:2016-12-19 10:38:41
【问题描述】:

我目前正在更新我的应用程序以使用 java 8。因此我正在使用 jack 编译器。

这适用于所有搭载 Android 5.0 或更高版本的设备。在具有 Pre Lollipop 版本的旧设备上,我收到以下异常:

原因:java.lang.ClassNotFoundException:找不到类 路径上的“de.gelbeseiten.android.GelbeSeitenApplication”: DexPathList[[压缩文件 "/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 之外什么都不更改,就会发生这种情况。

不幸的是,我在谷歌上找不到任何东西。在支持 java 8 的 google android 网站上,没有任何关于所需最低 android 版本的信息。 https://developer.android.com/guide/platform/j8-jack.html

我不认为完整的 build.gradle 文件有用,但这里有一个 sn-p:

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 文件中并非如此。

Soo...有人有关于这个主题的更多信息吗?或者甚至可以修复让应用在 Pre L 设备上运行?

提前致谢!

【问题讨论】:

  • 正在使用 proguard 和 multidex 吗?发布你的 gradle 和 proguard 文件。
  • 我正在使用multidex,是的。将两者都添加到原始帖子中。
  • 我认为杰克需要 ART。 ART 在 kitkat 上可用,但需要激活(不是默认 VM)

标签: android java-8 android-5.0-lollipop android-4.4-kitkat


【解决方案1】:

首先,如果您使用的是 Instant Run,请禁用它。目前不适用于 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 文件。

文件中指定的类被附加到主 dex 类 使用 aapt 计算。

如果设置,文件应该每行包含一个类,如下所示 格式:com/example/MyClass.class

【讨论】:

  • 不幸的是,这不起作用。由于该应用可在 Android 5 或更高版本的设备上运行,因此必须将应用和活动包含在 .dex 文件中。
猜你喜欢
  • 2015-01-15
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-15
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
相关资源
最近更新 更多