在Android Pre L设备上使用带有jack编译器的Java 8

时间:2016-12-19 10:38:42

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

我正在更新我的应用程序以使用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)                                                                               at java.lang.ClassLoader.loadClass(ClassLoader.java:497)                                                                               at 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)                                                                               at java.lang.reflect.Method.invokeNative(Native Method)                                                                               在java.lang.reflect.Method.invoke(Method.java:515)

我99%肯定,对Java 8的更改是其原因,因为如果我查看当前的公共版本并且除了包括java 8之外什么也不做更改,那确实会发生这种情况。

不幸的是,我在google找不到任何东西。在google 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文件中却不是这样。

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

提前致谢!

1 个答案:

答案 0 :(得分:0)

首先,如果您使用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