"Duplicate Zip Entry" (from Proguard) - renderscript-v8.jar - android-studio

I am developing android application in Android Studio. There is no problem when I build the project an export signed APK without Proguard, but when I try to build the project with ProGuard(minifyEnabled true), there is an Proguard build failed with error like this:
Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
> java.io.IOException: Can't write [user\myapplication\app\build\intermediates\transforms\proguard\release\jars\3\1f\main.jar] (Can't read [user]sdkpath\SDK\build-tools\23.0.0\renderscript\lib\renderscript-v8.jar(;;;;;;**/*.class)] (Duplicate zip entry [renderscript-v8.jar:android/support/annotation/Keep.class]))
Here is my build.gradle codes
...
defaultConfig {
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
checkReleaseBuilds false
}
}
dependencies {
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.google.android.gms:play-services-analytics:8.4.0'
compile project(':libraries:gpuimage')
compile 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2'
}
Here is my proguard-rule.pro
-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.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.content.Context {
public void *(android.view.View);
public void *(android.view.MenuItem);
}
-keep class *.R
-keep class *.R$* {*;}
-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }
-keep public class * extends android.support.v4.view.ActionProvider {
public <init>(android.content.Context);
}
## Google AdMob specific rules ##
## https://developers.google.com/admob/android/quick-start ##
-keep public class com.google.ads.** {
public *;
}
## Google Analytics 3.0 specific rules ##
-keep class com.google.analytics.** { *; }
#-keep class it.sephiroth.** {*;}
-dontwarn it.sephiroth.**
Proguard Troubleshooting Say this
Warning: can't write resource ... Duplicate zip entry
Your input jars contain multiple resource files with the same name. ProGuard continues copying the resource files as usual, skipping any files with previously used names. Once more, the warning may be an indication of some problem though, so it's advisable to remove the duplicates. A convenient way to do so is by specifying filters on the input jars. There is no option to switch off these warnings.
android The standard Android build process automatically specifies the input jars for you. There may not be an easy way to filter them to remove these warnings. You could remove the duplicate resource files manually from the input and the libraries.
But i didn't figured out the which jar i added twice in my build..!

This might be related to a bug in renderscript-v8.jar in BuildTools 23.0.0, which included it's own copy of the annotation library and might cause error when used with some other support library.
I would suggest you try use BuildTools 23.0.3 and see if the problem goes away. 23.0.3 also included several other fixes for RenderScript.

Related

HMS Toolkit Conversion (G+H, HMS API 1st) Library (xmsadapter) Not Able to Import

I am trying to port my app from GMS to (G+H) using HMS Toolkit.
After using the Toolkit Convertor, the following line throws an unresolved reference error on import org.xms.f.auth.ExtensionAuth asking to define xms. This obviously means the xmsadapter files (which are generated by the HMS Toolkit Convertor) have not been imported.
How can I fix this error to import the .xms files on import org.xms.f.auth.ExtensionAuth I feel like there's a dependancy that I'm missing?
You can also check your configured obfuscation scripts first.
XMS code generated using the Add HMS API policy will increase the size of your app. You can enable the code reduction function by adding or modifying the following configurations in the app/build.gradle file:
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
If an error message such as "Can't find...", "NoClassDefFoundError", "ClassNotFoundException", "NoSuchFieldException", or "NoSuchMethodException" is displayed after minifyEnabled is set to true, configure the following XMS obfuscation rules in app/proguard-rules.pro:
-ignorewarnings
-keepattributes *Annotation*
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keep class com.hianalytics.android.**{*;}
-keep class com.huawei.updatesdk.**{*;}
-keep class com.huawei.hms.**{*;}
-keep class com.huawei.agconnect.**{*;}
-keep interface com.huawei.agconnect.**{*;}
-keep class com.huawei.hmf.**{*;}
-keep class com.google.android.gms.**{*;}
-keep interface com.google.android.gms.**{*;}
-keep class com.google.firebase.**{*;}
-keep interface com.google.firebase.**{*;}
-keep class org.xms.**{*;}
-keep interface org.xms.**{*;}
For details,you can refer to this Docs.

Retrofit 2 with Proguard

Without Proguard enabled, retrofit2 work properly... but when enabled, application crash...
Here is my Proguard rules:
-dontwarn retrofit2.Platform$Java8
-dontwarn okio.**
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*
-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations
-keepattributes EnclosingMethod
-keepclasseswithmembers class * {
#retrofit2.* <methods>;
}
-keepclasseswithmembers interface * {
#retrofit2.* <methods>;
}
-keepclassmembers class demirci.omer.butun.gazeteler.siteler.newspapers.data.model.** { <fields>; }
-keep class demirci.omer.butun.gazeteler.siteler.newspapers.data.model.** { <fields>; }
-keep class demirci.omer.butun.gazeteler.siteler.newspapers.data.ApiEndPoint { <fields>; }
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class retrofit2.converter.gson.** { *; }
I have read below question:
retrofit2 rule
But I have also use retrofit2 converter with POJO. I think problem occur with this...Found this question:
gson-converter
There is two contidion for where problem occur.
Retrofit2 proguard rules is not suitable
Or Retrofit2 converter GSON with POJO is not convert data if fields obfuscated
I dont know with which condition problem occur...
here is my api interface:
public interface ApiEndPoint {
#FormUrlEncoded
#POST("all_newspaper/get_text.php")
Call<NewsContentClass> getContent(#Field("url") String url);
#FormUrlEncoded
#POST("all_newspaper/add_user_url.php")
Call<Void> addUserUrl(#Field("app_name") String appName,#Field("mobile_url") String mobile_url,#Field("desktop_url") String desktop_url);
}
Try adding
-keepclasseswithmembers class * {
#retrofit2.http.* <methods>;
}
try these rules from the retrofit2 site:
########--------Retrofit + RxJava--------#########
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

JNI RegisterNatives() can't find class method after running ProGuard

If I set minifyEnabled = true in Gradle setting for my Android app, calling the JNI function RegisterNatives() from the JNI shared library doesn't find its target class anymore. I tried a number of ProGuard rules but still can't get it to work.
Java code:
package net.pol_online.hyper;
...
public class Hyper extends Application {
...
public native static void initializeLibrary(Context context, int maxImageMemoryCacheSize);
...
}
JNI code:
static JNINativeMethod _methods[] = {
{"initializeLibrary", "(Landroid/content/Context;I)V", reinterpret_cast<void*>(&_InitializeLibrary)},
...
}
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
...
_hyperClass = env->FindClass("net/pol_online/hyper/Hyper");
jint error = env->RegisterNatives(_hyperClass, _methods, sizeof(_methods) / sizeof(JNINativeMethod));
assert(error == JNI_OK);
...
}
Gradle build settings (using the experimental Gradle NDK plug-in for Android Studio):
android.buildTypes {
release {
minifyEnabled = true
proguardFiles.add(file("proguard-rules.txt"))
ndk.with {
CFlags.add("-Werror")
cppFlags.add("-Werror")
}
}
}
ProGuard rules:
-keep class butterknife.** {
*;
}
-keep class **$$ViewBinder {
*;
}
-keepclasseswithmembernames class * {
#butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
#butterknife.* <methods>;
}
-dontwarn butterknife.internal.**
-keep public class net.pol_online.hyper.**
-dontnote android.support.v4.**
-dontwarn android.support.v4.**
The crash at launch:
Failed to register native method net.pol_online.hyper.Hyper.initializeLibrary(Landroid/content/Context;I)V in /data/app/net.pol_online.hyper-1/base.apk
java.lang.NoSuchMethodError: no static or non-static method "Lnet/pol_online/hyper/Hyper;.initializeLibrary(Landroid/content/Context;I)V"'
You do not include the default Proguard configuration for android:
proguardFiles getDefaultProguardFile('proguard-android.txt')
which includes a configuration to keep all native methods:
-keepclasseswithmembernames class * {
native <methods>;
}
This should fix the issue as well and is highly suggested.

Android Studio - Signed APK for release crashes

I am creating a signed APK for the next update of my app. But when i transfer this APK file to my devices and install it, it crashes on opening the app.
Here is my project level gradle file
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.google.gms:google-services:2.0.0-alpha6'
}
}
allprojects {
repositories {
jcenter()
}
}
And here is my module gradle file code:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.0'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.washkart.activities"
minSdkVersion 15
targetSdkVersion 19
// Enabling multidex support.
multiDexEnabled true
}
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
dataBinding {
enabled = true
}
}
dependencies {
compile files('libs/joda-time-2.4.jar')
compile(name:'acra-4.7.0', ext:'aar')
compile project(':ViewPagerIndicator')
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'com.google.android.gms:play-services-identity:8.4.0'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.facebook.android:facebook-android-sdk:4.6.0'
compile 'com.google.android.gms:play-services-analytics:8.4.0'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:multidex:1.0.0'
}
apply plugin: 'com.google.gms.google-services'
And i have added below lines in my proguard file to avoid the warning/error which i got durng signing the APK:
-keepnames class org.apache.** {*;}
-keep class org.apache.** {*;}
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.app.** { *; }
-dontnote android.support.v4.**
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *;}
-keep class android.support.v7.app.** { *; }
-keep interface android.support.v7.app.** { *; }
-dontskipnonpubliclibraryclassmembers
-dontwarn org.apache.commons.logging.LogFactory
-dontwarn org.apache.http.annotation.ThreadSafe
-dontwarn org.apache.http.annotation.Immutable
-dontwarn org.apache.http.annotation.NotThreadSafe
-dontwarn org.jodatime.time.**
-dontwarn org.joda.convert.**
-dontwarn org.apache.http.**
-dontwarn android.net.**
I have android studio 1.5.1 and testing on Android 4.4.2.
And the procedure which i used to generate in Build>Generate Signed APK>Selecting the module from dropdown > Add keystore file, alias and password> Build type = release > finish
I was facing the same issue, the problem is in your proguard file.
you have enabled the proguard when used minifyEnabled true.
Either disable it to disable to proguard or read about how to use proguard from here as it discards the class files and other resource that are not added in proguard-rules.pro.

Unit tests - run task programmatically

I'm creating custom task for gradle. I don't know how I can create task which will use my custom task class. Is it possible? I want to create this task for functional tests which will be runned on jenkins.
This is my custom task:
package pl.gradle
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class MyCustomTask extends DefaultTask {
public MyCustomTask() {
// do something
}
#TaskAction
def build() {
ant.echo(message: "only for tests")
}
}
And this is my test class:
package pl.gradle
import static org.junit.Assert.*
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.api.Project
import org.junit.Before;
import org.junit.Test
class MyCustomTaskTest {
private Project project;
def task
#Before
public void setUp() {
project = ProjectBuilder.builder().build()
task = project.task("build", type: MyCustomTask)
}
#Test
public void taskCreatedProperly() {
assertTrue(task instanceof MyCustomTask)
}
#Test
public void shouldRunTask() {
// task.execute() // how to run this task? I want to run build() method from MyCustomTask class which is #TaskAction
}
}
ProjectBuilder is meant for lower-level tests that don't execute tasks. Its sweet spot is testing plugins. In addition you'd write higher-level tests that execute real builds (and therefore also tasks). You'd typically use the Gradle tooling API to kick off these builds. Check out the tooling API samples in the full Gradle distribution.
Call Action.execute yourself for each of the task actions:
project.tasks.getByName("myTask").with { Task task ->
assertEquals task.group, 'My Group'
task.actions.each { Action action ->
action.execute task
}
}
I've previously mentioned this here: https://stackoverflow.com/a/63613564/410939

Resources