Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Library: Release .aar getting classes.jar empty when using proguard

I'm trying to generate a library with minifyEnabled true but, inside the release .aar, classes.jar is getting empty.

I have checked my proguard-rules.pro and it seems to be all right.

I've even created a new module with the default .gradle files and when i set minifyEnable true the release version still gets the classes.jar with no class inside.

After all, is it possible to generate an android library obfuscating the code?

EDIT 1: Adding module build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.0'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
    }

    lintOptions {
        abortOnError false
    }
}

repositories {
    maven { url 'https://maven.google.com' }
    jcenter()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'org.apache.httpcomponents:httpcore:4.3.3'
    compile('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
    }

    compile 'fr.bmartel:jspeedtest:1.25'
    compile "com.android.support:appcompat-v7:27.0.0"
    testCompile 'junit:junit:4.12'
}
like image 988
Danilo Avatar asked Dec 05 '17 11:12

Danilo


People also ask

How to create Jar file from Android library project?

Click File > New > New Module. In the Create New Module window that appears, click Android Library, then click Next. There's also an option to create a Java Library, which builds a traditional JAR file.

How to ignore warnings in proguard?

There is no option to switch off these warnings. 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.

What is consumerProguardFiles?

consumerProguardFiles 'consumer-rules.pro'} A consumer proguard rules file is like any other proguard rules files with the caveat that the rules inside it are automatically applied to the consuming application when the application is being built in a proguard enabled mode.

What is minify in Android?

minify is an Android tool that will decrease the size of your application when you go to build it. It's extremely useful as it means smaller apk files! It detects any code or libraries that aren't being used and ignores them from your final apk.


1 Answers

Ok, after some time I solved my problem.

I copy/paste a default proguard rules configuration (library.pro) to my proguard-rules.pro. You can find this file and more examples in path-to-your-sdk/tools/proguard/examples.

For more information, read this.

In my build.gradle I chagend:

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
    release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
}

to:

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
     release {
         minifyEnabled true
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         consumerProguardFiles 'proguard-rules.pro' //added this line
     }
 }

Thanks for the help!

like image 133
Danilo Avatar answered Oct 21 '22 11:10

Danilo