How to EXCLUDE an external library jar from Proguard in Android Studio? - android-studio

The build.gradle of one of the modules in my project is pretty simple:
apply plugin: 'com.android.library'
android {
compileSdkVersion 8
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 8
targetSdkVersion 8
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
}
}
}
dependencies {
compile project(':timsvc')
compile files('libs/jsoup-1.8.2.jar')
}
However, when I build it, it fails to find symbols in the external library jsoup-1.8.2.jar.
Since timsvc is a module of mine, having its own build.gradle and proguard.cfg, I can control its minify level, and so I don't have any problems with it.
But I cannot do the same for jsoup-1.8.2.jar because it is an externally provided jar.
Is there a way to exclude it from Proguard in Android Studio?
If so, how do I accomplish that?

The best is to see the libraries documentations and readme page (like Github).
but in genneral you can add -keep options to your proguard configuration file for the classes in those external jars. For instance:
-keep class example.** { *; }

Related

Library not installed with build make Android Studio but works with CMAKE-GUI

System information:
Android Studio 3.2.1
Linux Ubuntu 16.04
Introduction
I recently came across a problem with my IDE (Android Studio). I use it to develop a Native / Java Application alongside Gradle and CMakeLists.
I use a few CMakeLists to export some functions in C/C++ to use with my app.
So these Cmakelists will create some library that can be used.
Problem
When I "make" my project it will build successfully, but the library doesn't change, whether it's modified, or not.
After that, I thought that I might need to "Clean my project" before doing it, but nothing good happened.
You can see my build.gradle under the post.
Tried Solution
The workaround I found is to compile each library with CMAKE-GUI and then compile my Android project to see the modification previously made.
That's bad and not normal, I think.
Help
I want to know why my project doesn't make again my library and "install" it in the appropriate folder.
It's unnecessary to show my CMakeLists because it works on CLion with Linux and Windows with Visual Studio.
Build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.media.[NAME_OF_MY_PROJET]"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk { abiFilters "armeabi-v7a" }
externalNativeBuild {
cmake {
cppFlags "-O3 -fno-stack-protector -fintegrated-as "
arguments "-DANDROID_ABI=armeabi-v7a", "-DANDROID_TOOLCHAIN=clang",
"-DANDROID_NATIVE_API_LEVEL=24", "-DANDROID_NDK=/Android/Sdk/ndk-bundle",
"-DANDROID_STL=c++_static", "-DANDROID_SDK_ROOT=/Android/Sdk/"
}
}
}
buildTypes {
/* customDebugType {
debuggable true
} */
debug {
jniDebuggable true
}
/* release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}*/
}
externalNativeBuild {
cmake {
path "../CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
}

Gradle Sync failed: Could not find method implementation()

Been trying to resolve this error for hours...cant get it solved. I am a newbie to Android Studio
My project level build.gradle file is as follows
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven { url "https://maven.google.com" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:4.0.1'
}
}
allprojects {
repositories {
jcenter()
maven { url "https://maven.google.com" }
}
}
And my app build.gradle file is as follows
apply plugin: 'com.android.application'
android {
compileSdkVersion 18
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.amazonaws.androidtest"
minSdkVersion 8
targetSdkVersion 18
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
aaptOptions {
cruncherEnabled = false
}
dependencies {
implementation 'com.google.firebase:firebase-core:16.0.1'
}
}
apply plugin: 'com.google.gms.google-services'
Getting the following error when trying to sync
Gradle sync failed: Could not find method implementation() for arguments [com.google.firebase:firebase-core:16.0.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I am using Android Studio 2.3
Tried to upgrade Gradle to version 4 but that requires Android Studio 3. Cant install Android Studio 3 for now.
Also added the maven url's as some answers suggested. Still not working.
Any other way to resolve this?
with build-tools 3.1.4, it should become known (read below):
classpath "com.android.tools.build:gradle:3.1.4"
but if you cannot update Android Studio , use compile instead of implementation.
compile "com.google.firebase:firebase-core:16.0.3"

Gradle cannot find symbol class in Android Studio project

Gradle has trouble resolving a dependency in an Android Studio project. I have a library that has a dependency to a java module called "common". I have the dependency speficied in the buld.gradle (see below), however, I still get "cannot find symbol MyClass" when building. How can I fix this?
The line in error is the import that references the package in "common".
Building works fine with maven, but not with gradle.
This is the build.gradle of my lib.
apply plugin: 'com.android.library'
android {
compileSdkVersion 17
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 17
targetSdkVersion 17
}
buildTypes {
release {
minifyEnabled false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':common')
}

Gradle dependency for MPAndroidChart not working in Android Studio

Trying to install MPAndroidChart, I tried to follow the instructions in the Readme for adding the library as a gradle dependency.
EDIT:
I didn't edit the top-level build.grade file, only the build.gradle file in my app directory. The latter looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.gmail.konstantin.schubert.workload"
minSdkVersion 16
targetSdkVersion 23
versionCode 7
versionName "1.1.5"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.guava:guava:17.0'
compile 'joda-time:joda-time:2.8.2'
compile 'com.android.support:design:23.0.0'
compile 'com.github.PhilJay:MPAndroidChart:v2.2.3'
}
As you can see, I have added the maven repository and the compile instruction in the dependencies.
However, Android Studio gives me an error:
Error:(32, 13) Failed to resolve: com.github.PhilJay:MPAndroidChart:v2.2.3
EDIT: Here are the instructions as given by JitPack: https://jitpack.io/
I believe I followed them to the letter.
What am I doing wrong? What am I forgetting?
EDIT: The question is really following the wrong lead. Looking at my gradle log, I discovered the following error:
* What went wrong:
A problem occurred configuring project ':app'.
Could not resolve all dependencies for configuration ':app:_debugCompile'.
Could not resolve com.github.PhilJay:MPAndroidChart:v2.2.3.
Required by:
workload:app:unspecified
Could not GET 'https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.2.3/MPAndroidChart-v2.2.3.pom'.
> peer not authenticated
This is a much better lead to follow and can easily be googled.
please edit your top level gradle file like this:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
other thing.. take a look at gradle console in the bottom right of android studio, and show me the log.
It is a problem with java/gradle on linux not recognizing the ssl certificate. The easiest solution is to use java 8, see https://www.jitpack.io/docs/FAQ/.
I am on Debian 8.5 Jessie and using openjdk 8 (instead of the stock 7 version) from jessie/backports solved the issue (building gnucash-android) on my system.

java.lang.UnsatisfiedLinkError: Couldn't load from loader dalvik.system.PathClassLoader findLibrary returned null

I understand this is asked many times, but I have done thorough research before posting my query.
I have updated Android Studio to latest version as of March 2nd 2015, version 1.1.0. I think still grade does not package .so files on its own.
I have written NewNDK.c in /jni folder. Ran the ndk-build script and .so files were created in /libs. As per suggestion in one of the posts, I modified libs to lib.
Even then, I am getting java.lang.UnsatisfiedLinkError: Couldn't load from loader dalvik.system.PathClassLoader findLibrary returned null error.
The posts does not say about which files to modify. I am new to Android, request your help.
I have narrowed down the problem to: 1. Gradle is not packaging 2. Gradle scripts should be modified.
The build.gradle (Module: app) is as below:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.raghu.newndk"
minSdkVersion 17
targetSdkVersion 17
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
The build.gradle (project) is as below:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Please let me know what is missing.
Thank You!
As per suggestion in one of the posts, I modified libs to lib.
It doesn't really make sense to do that. lib is the folder where your .so files will go inside the APK, but this step will be handled directly by the packaging tools if your project is correctly organized.
Inside an Android Studio project, you should put the .so files inside jniLibs/<ABI> where <ABI> is the target architecture of each .so file (armeabi, x86... the same way that they are generated from the NDK inside libs).
So you can either rename your libs folder to jniLibs, or configure your build so your .so files from libs will be directly integrated:
android {
...
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
}
}
Open the project properties > go to Andriod options > select Advance tab > just uncheck "armeabi","armeabi-v7a","arm64-v8a" and save the project.
The Error will be removed.

Resources