android gradle plugin 3.0.0-alpha2: Error inflating class android.support.v7.widget.FitWindowsLinearLayout - android-gradle-plugin

after upgrading the android gradle plugin from 3.0.0-alpha1 to 3.0.0-alpha2 my application does not start anymore: part of the stacktrace:
05-27 09:14:57.692 3015-3015/com.tmtron.dscontrol2app.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tmtron.dscontrol2app.debug, PID: 3015
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tmtron.dscontrol2app.debug/com.tmtron.dscontrol2.gui.MainActivity}: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class android.support.v7.widget.FitWindowsLinearLayout
Caused by: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class android.support.v7.widget.FitWindowsLinearLayout
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.widget.FitWindowsLinearLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.FitWindowsLinearLayout" on path: DexPathList[[zip file "/data/app/com.tmtron.dscontrol2app.debug-ZE45jTeV6eTnNIeveAjAbw==/base.apk", zip file "/data/app/com.tmtron.dscontrol2app.debug-ZE45jTeV6eTnNIeveAjAbw==/split_lib_dependencies_apk.apk", zip file "/data/app/com.tmtron.dscontrol2app.debug-ZE45jTeV6eTnNIeveAjAbw==/split_lib_slice_0_apk.apk", zip file "/data/app/com.tmtron.dscontrol2app.debug-..."/data/app/com.tmtron.dscontrol2app.debug-ZE45jTeV6eTnNIeveAjAbw==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.tmtron.dscontrol2app.debug-ZE45jTeV6eTnNIeveAjAbw==/lib/x86, /system/lib, /system/vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at com.tmtron.dscontrol2.gui.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6954)
05-27 09:14:57.693 3015-3015/com.tmtron.dscontrol2app.debug E/AndroidRuntime:
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
It seems like the class android.support.v7.widget.FitWindowsLinearLayout is not found anymore.
Any ideas?
BTW: I have already tried to do all kinds of cleanup, invalidate-caches, restart, etc. as mentioned in this SO.

I think this is a temporarily bug.
Proparbly Proguard removes this class because of some reason, therefore this exception occurs.
For now the easiest solution might be to disable Proguard in the build.gradle file. If you still want to keep it, you will have to change your proguard settings.
This is what I did and what worked for me:
-dontwarn android.support.v7.**
-keep class android.support.v7.widget.** { *; }
As I also had issues with constraint and design libray I added this:
-dontwarn android.support.constraint.**
-keep class android.support.constraint.** { *; }
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
I hope it helps you:)

UPDATE
This bug has already been fixed - it works for me with gradle plugin 3.0.0-alpha4
ORIGINAL answer (kept for reference)
Actually I didn't have ProGuard enabled for my debug builds, but the Shrinker of the Android Gradle Plugin (which uses the same proguard files.
Temporary workaround for the issue: add the following instructions to the proguard-rules.pro file:
-keep class android.support.v7.widget.** { *; }
-keep class android.support.v4.widget.** { *; }
-keep class android.support.design.** { *; }
-keep class com.bluelinelabs.conductor.** { *; }

Related

MainApplication.java:9: Error: Packagecom.reactnativecommunity.webview does not exist

% npx react-native run-android Starting JS server... Building and
installing the app on the device (cd android && ./gradlew
installDebug)...
Configure project :react-native-reanimated WARNING: The specified Android SDK Build Tools version (28.0.2) is ignored, as it is below
the minimum supported version (29.0.2) for Android Gradle Plugin
4.1.0. Android SDK Build Tools 29.0.2 will be used. To suppress this warning, remove "buildToolsVersion '28.0.2'" from your build.gradle
file, as each version of the Android Gradle Plugin now has a default
version of the build tools. Warning: Mapping new ns
http://schemas.android.com/repository/android/common/02 to old ns
http://schemas.android.com/repository/android/common/01 Warning:
Mapping new ns
http://schemas.android.com/repository/android/generic/02 to old ns
http://schemas.android.com/repository/android/generic/01 Warning:
Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02
to old ns http://schemas.android.com/sdk/android/repo/addon2/01
Warning: Mapping new ns
http://schemas.android.com/sdk/android/repo/repository2/02 to old ns
http://schemas.android.com/sdk/android/repo/repository2/01 Warning:
Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02
to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01
Configure project :react-native-linear-gradient WARNING: Configuration 'provided' is obsolete and has been replaced with
'compileOnly'. It will be removed in version 5.0 of the Android Gradle
plugin. For more information, see
http://d.android.com/r/tools/update-dependency-configurations.html.
Configure project :react-native-orientation WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and
'api'. It will be removed in version 5.0 of the Android Gradle plugin.
For more information, see
http://d.android.com/r/tools/update-dependency-configurations.html.
Configure project :app WARNING: The option 'android.useDeprecatedNdk' is deprecated. The current default is
'false'. It has been removed from the current version of the Android
Gradle plugin. NdkCompile is no longer supported WARNING:
Configuration 'compile' is obsolete and has been replaced with
'implementation' and 'api'. It will be removed in version 5.0 of the
Android Gradle plugin. For more information, see
http://d.android.com/r/tools/update-dependency-configurations.html.
WARNING: API 'variant.getMergeResources()' is obsolete and has been
replaced with 'variant.getMergeResourcesProvider()'. It will be
removed in version 5.0 of the Android Gradle plugin. For more
information, see
https://d.android.com/r/tools/task-configuration-avoidance. To
determine what is calling variant.getMergeResources(), use
-Pandroid.debug.obsoleteApi=true on the command line to display more information. WARNING: API 'variant.getPackageApplication()' is
obsolete and has been replaced with
'variant.getPackageApplicationProvider()'. It will be removed in
version 5.0 of the Android Gradle plugin. For more information, see
https://d.android.com/r/tools/task-configuration-avoidance. To
determine what is calling variant.getPackageApplication(), use
-Pandroid.debug.obsoleteApi=true on the command line to display more information. WARNING: API 'variant.getMergeAssets()' is obsolete and
has been replaced with 'variant.getMergeAssetsProvider()'. It will be
removed in version 5.0 of the Android Gradle plugin. For more
information, see
https://d.android.com/r/tools/task-configuration-avoidance. To
determine what is calling variant.getMergeAssets(), use
-Pandroid.debug.obsoleteApi=true on the command line to display more information.
Task :app:compileDebugJavaWithJavac FAILED /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:9:
Error: Packagecom.reactnativecommunity.webview does not exist import
com.reactnativecommunity.webview.RNCWebViewPackage;
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:18:
Error: Packagecom.beefe.picker does not exist import
com.beefe.picker.PickerViewPackage;
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:28:
Error: Packagecom.reactnativenavigation does not exist import
com.reactnativenavigation.NavigationApplication;
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:29:
Error: Packagecom.reactnativenavigation.react does not exist import
com.reactnativenavigation.react.NavigationReactNativeHost;
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:30:
Error: Packagecom.reactnativenavigation.react does not exist import
com.reactnativenavigation.react.ReactGateway;
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:36:
Error: Symbol not found public class MainApplication extends
NavigationApplication {
^ シンボル: Class NavigationApplication
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:45:
Error: Symbol not found
protected ReactGateway createReactGateway() {
^ シンボル: Class ReactGateway 場所: Class MainApplication
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:8:
Error: Packageandroid.support.annotation does not exist import
android.support.annotation.Nullable;
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:12:
Error: Packagecom.reactnativenavigation does not exist import
com.reactnativenavigation.NavigationActivity;
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:18:
Error: Symbol not found public class MainActivity extends
NavigationActivity {
^ シンボル: Class NavigationActivity /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:21:
Error: Symbol not found
protected void onCreate(#Nullable Bundle savedInstanceState) {
^ シンボル: Class Nullable 場所: Class MainActivity
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:44:
Error: Method does not override super
#Override
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:46:
Error: Symbol not found
ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
^ シンボル: Class NavigationReactNativeHost 場所: Class MainApplication
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:47:
Error: Method does not override super
#Override
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:52:
Error: Symbol not found
return new ReactGateway(this, isDebug(), host);
^ シンボル: Class ReactGateway 場所: Class MainApplication
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:55:
Error: Method does not override super
#Override
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:68:
Error: Symbol not found
new PickerViewPackage(),
^ シンボル: Class PickerViewPackage 場所: Class MainApplication
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:70:
Error: Class FBSDKPackageのコンストラクタ FBSDKPackageは指定された型に適用できません。
new FBSDKPackage(mCallbackManager),
^ 期待値: 引数がありません 検出値: CallbackManager 理由: 実引数リストと仮引数リストの長さが異なります
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:72:
Error: Symbol not found
new RNCWebViewPackage(),
^ シンボル: Class RNCWebViewPackage 場所: Class MainApplication
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/MainApplication.java:79:
Error: Method does not override super
#Override
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:20:
Error: Method does not override super
#Override
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:22:
Error: Symbol not found
super.onCreate(savedInstanceState);
^ シンボル: 変数 super 場所: Class MainActivity /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:26:
Error: Symbol not found
PackageInfo info = getPackageManager().getPackageInfo(
^ シンボル: メソッド getPackageManager() 場所: Class MainActivity
/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:48:
Error: Method does not override super
#Override
^ /Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.java:50:
Error: Symbol not found
super.onActivityResult(requestCode, resultCode, data);
^ シンボル: 変数 super 場所: Class MainActivity ノート:/Users/tenna/Downloads/aitem/android/app/src/main/java/com/aitem/com/aitem/MainActivity.javaは推奨されないAPIを使用またはオーバーライドしています。
ノート:詳細は、-Xlint:deprecationオプションを指定して再コンパイルしてください。 Error25個
FAILURE: Build failed with an exception.
What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it
incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation
warnings and determine if they come from your own scripts or plugins.
See
https://docs.gradle.org/7.3.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 7s 145 actionable tasks: 2 executed, 143 up-to-date
Could not install the app on the device, read the error above for
details. Make sure you have an Android emulator running or a device
connected and have set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
Command failed: ./gradlew installDebug
Error: Command failed: ./gradlew installDebug
at checkExecSyncError (node:child_process:707:11)
at Object.execFileSync (node:child_process:726:15)
at runOnAllDevices (/Users/tenna/Downloads/aitem/node_modules/react-native/local-cli/runAndroid/runAndroid.js:299:19)
at buildAndRun (/Users/tenna/Downloads/aitem/node_modules/react-native/local-cli/runAndroid/runAndroid.js:135:12)
at /Users/tenna/Downloads/aitem/node_modules/react-native/local-cli/runAndroid/runAndroid.js:65:12
at processTicksAndRejections (node:internal/process/task_queues:96:5)
click on android folder
click on app folder
click on src folder
click on java folder
click on MainApplication.java file and remove Packagecom.reactnativecommunity.webview in line 9
use latest updated one
npm i react-native-webview

How do I set up Proguard with Kotlin JVM (not android) and Gradle

I would like to ask if you know a guide or if you know how to use Proguard with Kotlin and Gradle and would like to share your knowledge, I would really appreciate it. Already searched Stack Overflow, but couldn't find a single (answered) question about using Proguard + Kotlin JVM (not android!) + Gradle.
I only found guides on the Internet regarding this matter for android, but I'm not using Kotlin for android, I'm building a Java Plugin (in other words, JVM) with Kotlin and I would like to use Proguard to minify and obfuscate my code. Note that my project is using Gradle Shadow to shadow its dependencies into the final jar (these dependencies don't need to be obfuscated but can be minified, and definitively do need to exist in the obfuscated jar created by Proguard).
I would like to know all the steps, things like how to step up Gradle to automatically minify & obfuscate my code (with a custom task), how to remove Kotlin metadata from java compiled classes, common issues & solutions to that issues, and anything else that you think it can be useful to know, everything is helpful. Thank you very much.
I got this working some time ago, refer to the Proguard manual to learn about the syntax of its configuration file. Also, for some reason, sometimes Proguard just says that the current obfuscated jar is "up-to-date" even when there's no jar, seems to be a bug that I could not find the steps to reproduce. If that happens to you, just change the Proguard version you're using and reload Gradle dependencies.
build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
// add this
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.guardsquare:proguard-gradle:7.1.1") {
exclude("com.android.tools.build")
}
}
}
plugins {
kotlin("jvm") version "1.5.30"
id("com.github.johnrengelman.shadow") version "7.0.0"
}
group = "com.yourgroup"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven("https://oss.sonatype.org/content/groups/public/")
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0")
// your dependencies here
}
tasks.test {
useJUnitPlatform()
}
// disables the normal jar task
tasks.jar { enabled = false }
// and enables shadowJar task
artifacts.archives(tasks.shadowJar)
tasks.shadowJar {
archiveFileName.set(rootProject.name + ".jar")
val dependencyPackage = "${rootProject.group}.dependencies.${rootProject.name.toLowerCase()}"
// your relocations here
exclude("ScopeJVMKt.class")
exclude("DebugProbesKt.bin")
exclude("META-INF/**")
}
tasks.register<proguard.gradle.ProGuardTask>("proguard") {
// here is where you configure your Proguard stuff, you can include libraries directly
// through here, or in the configuration file, I usually just use the configuration file
// to do everything (it can be any name and extension you want, just using .pro here cause
// that's what Android uses)
configuration("proguard-rules.pro")
}
// keep this if you want run proguard task automatically after building
tasks.build.get().finalizedBy(tasks.getByName("proguard"))
tasks.withType<JavaCompile> {
sourceCompatibility = "16"
targetCompatibility = "16"
options.encoding = "UTF-8"
}
tasks.withType<KotlinCompile> { kotlinOptions.jvmTarget = "16" }
proguard-rules.pro: This is just an example file used to minify a Minecraft plugin, but the logic applies no matter what kind of jar you're dealing with.
# injars = your shadowed jar
-injars build/libs/YourShadowedJarHere.jar
# outjars = the name of the new obfuscated/minified jar
-outjars build/libs/YourShadowedJarHere-min.jar
# important! you need to add this "rt" file from your JDK as libraryjars!
-libraryjars "C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar"
# add here the jar to any compileOnly dependency you might have (or add "dontwarn" to make proguard ignore the missing classes)
-libraryjars "PathTo\YourProject\SomeLocalLibraries\SomeLocalLibrary.jar"
-dontwarn net.minecraft.**
-dontwarn org.bukkit.**
-dontwarn com.google.**
-dontwarn com.comphenix.**
-dontwarn android.**
-dontwarn org.hibernate.**
-dontwarn com.sk89q.worldedit**
-dontwarn com.sk89q.worldguard**
-dontwarn net.milkbowl.vault.economy**
-keep class yourpackage.dependencies.yourproject.hikari.metrics.**
-dontwarn com.codahale.metrics.**
-keep class com.codahale.metrics.**
-dontwarn **hikari.metrics**
-dontwarn javax.crypto.**
-dontwarn javassist.**
-dontwarn **slf4j**
-dontwarn io.micrometer.core.instrument.MeterRegistry
-dontwarn org.codehaus.mojo.**
-dontwarn **prometheus**
-dontwarn **configurate.**
-dontwarn **koin.core.time.**
-dontwarn net.Indyuce.**
-dontwarn **xseries.**
-keepnames class kotlin.coroutines.** { *; }
-dontwarn **kotlinx.coroutines.**
-dontwarn **org.apache.commons.codec**
#-dontshrink
#-dontobfuscate
#-dontoptimize
# Keep your main class
-keep,allowobfuscation,allowoptimization class * extends org.bukkit.plugin.java.JavaPlugin { *; }
# Keep event handlers
-keep,allowobfuscation,allowoptimization class * extends org.bukkit.event.Listener {
#org.bukkit.event.EventHandler <methods>;
}
# Keep main package name
// -keeppackagenames "your.package"
# Keep public enum names
-keepclassmembers public enum yourpackage.** {
<fields>;
public static **[] values();
public static ** valueOf(java.lang.String);
}
# Keep all ProtocolLib packet listeners (this was rough to get working, don't turn on optimization, it ALWAYS breaks the sensible ProtocolLib)
-keepclassmembers class yourpackage.yourproject.** {
void onPacketSending(com.comphenix.protocol.events.PacketEvent);
void onPacketReceiving(com.comphenix.protocol.events.PacketEvent);
}
# Keep static fields in custom Events
-keepclassmembers,allowoptimization class yourpackage.yourproject.** extends org.bukkit.event.Event {
#yourpackage.dependencies.kotlin.jvm.JvmStatic <fields>;
public static final <fields>;
#yourpackage.dependencies.kotlin.jvm.JvmStatic <methods>;
public static <methods>;
}
# Remove dependencies obsfuscation to remove bugs factor
#-keep,allowshrinking class yourpackage.dependencies.** { *; }
# If your goal is obfuscating and making things harder to read, repackage your classes with this rule
-repackageclasses yourpackage.yourproject
-allowaccessmodification
-mergeinterfacesaggressively
# You can parse any resource files that might contain a reference of your classes here (so they are updated according to the modifications made by Proguard)
-adaptresourcefilecontents **.yml,META-INF/MANIFEST.MF
# Some attributes that you'll need to keep (warning: removing *Annotation* might break some stuff)
-keepattributes Exceptions,Signature,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
#-keepattributes Exceptions,Signature,Deprecated,LineNumberTable,*Annotation*,EnclosingMethod
#-keepattributes LocalVariableTable,LocalVariableTypeTable,Exceptions,InnerClasses,Signature,Deprecated,LineNumberTable,*Annotation*,EnclosingMethod

Proguard and Kotlin-Reflect/Kotlin Annotations

Looking for some help from someone who puts the pro in proguard.
Annotations used by kotlin-reflect (required dependency for jackson-module-kotlin v v2.8.8) are getting stripped out after upgrading to kotlin 1.1.2-3. The error from proguard is:
Warning:kotlin.reflect.jvm.internal.impl.descriptors.CallableDescriptor: can't find referenced class org.jetbrains.annotations.ReadOnly
This is happening for a few annotations, not just ReadOnly. We have tried adding a good ol' catch all but the error still exists:
-keep class org.jetbrains.kotlin.** { *; }
-keep class org.jetbrains.annotations.** { *; }
-keepclassmembers class ** {
#org.jetbrains.annotations.ReadOnly public *;
}
Looking at the source for ReadOnly it is an #interface with java.lang.annotations.* imported for #Documented, #RetentionPolicy.CLASS, #Target
Or a shorter version:
-dontwarn kotlin.reflect.jvm.internal.**
The fix for us was to add dontwarn for the reflect warnings.
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.CallableDescriptor
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.ClassDescriptor
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.ClassifierDescriptorWithTypeParameters
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.annotations.AnnotationDescriptor
-dontwarn kotlin.reflect.jvm.internal.impl.descriptors.impl.PropertyDescriptorImpl
-dontwarn kotlin.reflect.jvm.internal.impl.load.java.JavaClassFinder
-dontwarn kotlin.reflect.jvm.internal.impl.resolve.OverridingUtil
-dontwarn kotlin.reflect.jvm.internal.impl.types.DescriptorSubstitutor
-dontwarn kotlin.reflect.jvm.internal.impl.types.DescriptorSubstitutor
-dontwarn kotlin.reflect.jvm.internal.impl.types.TypeConstructor
These annotations exist in kotlin-compiler which is why proguard can't find them. Just ignore the warning instead of adding kotlin-compiler as a dependency (as this issue suggests Cannot resolve symbol #ReadOnly and #Mutable in Kotlin 1.1.0 compilation).
This may be a bug in kotlin-reflect; they should provide proguard rules to hide this from integrating apps.

Why am I getting a ClassNotFoundException from Play Services lib when using Proguard?

I just turned on ProGuard on my build and now I'm getting a
java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.chimera.GmsModuleInitializer" on path: DexPathList[[zip file "/system/app/PlayGames.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
The docs say that everything that I need to use Proguard with Play Services should be included by the Android Gradle plugin:
Note: ProGuard directives are included in the Play services client
libraries to preserve the required classes. The Android Plugin for
Gradle automatically appends ProGuard configuration files in an AAR
(Android ARchive) package and appends that package to your ProGuard
configuration. During project creation, Android Studio automatically
creates the ProGuard configuration files and build.gradle properties
for ProGuard use. To use ProGuard with Android Studio, you must enable
the ProGuard setting in your build.gradle buildTypes. For more
information, see the ProGuard guide.
This the important part of my app module build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
...
}
...
buildTypes {
...{
applicationIdSuffix ".debug"
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
...
//google play services
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.google.android.gms:play-services-analytics:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
}
This is my top level build.gradle file:
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
What am I missing?
ClassNotFoundException usually occurs when an application tries to load in a class through its string name but no definition for the class with the specified name could be found.
From this forum, you can fix it by adding com.google.android.gms.** { *; }.
Just add to your proguard-project.txt:
keep class com.google.android.gms.** { *; }
dontwarn com.google.android.gms.**
You can also check on the suggested comment in this SO question.
Google Play Services aars contain proguard.txt with the necessary clauses. So the setting shouldn't really be necessary. You can investigate what happened with the fragment in ProGuard output files. Check app/build/output/mapping/{buildVariant}/usage.txt and mapping.txt. The fragment should be mentioned in one of those.
Hope this helps!
I know this is a rather old post but I would like to highlight the following should it help someone in the future.
Like #abielita mentioned, the ClassNotFoundException is caused by an unhandled case of reflection.
When using broad -keep options (ending with .** { *; }), you will instruct ProGuard not to shrink, optimize or obfuscate all classes and classmembers for the package name mentioned before the wildcards. This will eventually lead in a poorly optimized project. Therefore, it's better to narrow down such -keep option to only target the missing class. In OP's example, adding a -keep option for the missing class like shown below will tackle this particular issue;
-keep class com.google.android.gms.chimera.GmsModuleInitializer
ProGuard can help you set up narrowed down -keep options if you add -addconfigurationdebugging to the configuration file, more details on this feature is documented in the ProGuard manual, here.
Recently the ProGuard Playground was released, you can quickly visualise the effect of the broad -keep options vs the narrowed down ones. A nice benefit here is that you do not need to continuously (re-)build the project.

Unable to resolve class org.gradle.util.TestUtil in Gradle

This error means that class TestUtil is not on the classpath and the compiler can not find it. I came across such error hundred times before and there was missing Jar or wrong written class names, but now I just don't know what is wrong.
In my buildSrc dir I have custom Task and I made test for it:
package com.example.core.tasks;
import spock.lang.Specification
import org.gradle.api.Project
import org.gradle.util.TestUtil
public class GetInfoTaskTest extends Specification {
def "check files"(){
given:
def project = TestUtil.createRootProject()
when:
GetInfoTask getInfo = project.tasks.create("getInfo", GetInfoTask)
then:
getInfo instanceof GetInfoTask.class
}
}
In the build script in the buildSrc dir:
dependencies {
compile localGroovy()
testCompile 'org.spockframework:spock-core:0.7-groovy-1.8'
testCompile gradleApi()
}
Error:
GetInfoTaskTest.groovy: 4: unable to resolve class org.gradle.util.TestUtil
# line 3, column 1.
import org.gradle.util.TestUtil
^
I checked that this TestUtil is not internal. I still don't know why gradle can not find it.
TestUtil is an internal class. You can't use it in your own code.