Shrink library code - java

I want to hide real code from aar file. My library's Gradle file looks like this.
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
}
And I am getting following exception while running this app.
Warning:Exception while processing task java.io.FileNotFoundException: E:\MyProjects\TestApp\testLibrary\build\intermediates\proguard-rules\release\aapt_rules.txt (The system cannot find the path specified)
Error:Execution failed for task ':testLibrary:transformClassesAndResourcesWithProguardForRelease'.
> Job failed, see logs for details
If I changed the minifyEnabled true to minifyEnabled false then this app runs without fail but I can see the Java files from generated arr file.
So I want to hide Java code.

for a library you should not supply proguard-rules.pro but some proguard-consumer-rules.pro... which contains the configuration rules, which are being applied, when the library is compiled into an APK package (contrary to the usual behavior, when the library is being build).
// These rules will be applied when a consumer of this library sets 'minifyEnabled true'.
consumerProguardFiles 'proguard-consumer-rules.pro'
the library is not directly being obfuscated like that, but only later on - without having every single consumer of the library having to define their own ProGuard rules configuration. see the question I've linked above, one of it's answers provides a basic configuration, in order to keep the public methods (so that one can still use the library, once it had been merely obfuscated).
and also see the Android Testing template for: module-android-library.
I would assume, that this AAPT error stems from supplying an empty ProGuard configuration file.

Related

How to have ALL Android Studio 3.5 source files NOT be in usual location in project tree but on a separate drive

I'd like to keep my java source files (i.e., those that I will create or edit) on an external or a second internal drive, separated from the hundreds (or thousands) of files created during compilation. Let all project files be on drive C: except my editable source files.
Let's call the drive to put the sources on X:.
Here is a normal AS 3.5 project tree for C:\users\me\AndroidStudioProjects\myProject:
and here is the ...\app\src folder:
I don't want to have the entire app folder on drive X: because app (notably app\build\intermediates) will contain hundreds (likely thousands) of files and folders.
I just want the very small ...\app\src folder on X: partly for the purpose of having a 90% smaller backup size for projects.
It seems like this would be a common thing for an AS user to want, but lots of Googling hasn't shown much in the way of concrete specific examples.
For better or worse, here's what my build.gradle file has (d)evolved into:
apply plugin: 'com.android.application'
buildscript
{
repositories
{
jcenter()
google()
}
dependencies
{
classpath 'com.android.tools.build:gradle:3.5.0'
}
}
android
{
compileSdkVersion 26
defaultConfig
{
applicationId "com.dslomer64.sqhell"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "3.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes
{
release
{
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies
{
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:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
}
I didn't write any of it. I only took what compilation produced, changing only whatever the latest build error caused me to change.
Yes, I need to learn gradle. But not today, please.
I suspect sourceSets may be where to stick something like X:\AndroidStudioProjects\myProject\src, where src will contain the main folder shown in the second picture. And I gather that the 'src' or 'srcdir' keyword should precede that path.
EDIT
Here's what I've come up with so far:
sourceSets
{
main
{
java { srcDir 'X:\\AndroidStudioProjects\\myProject\\src\\main' }
}
}
End Edit
Surely there are a relatively few lines inside a new block in my existing build.gradle file that could be added to accomplish the task. Surely someone on SO has done this very thing. Please provide or share the required block and any other changes that may also be required.
I will most certainly appreciate the gift.

Android Studio and openimaj

I am currently trying to build an android studio project using a dependency from the openimaj Java library.
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.mapinguari.myapplication"
minSdkVersion 15
targetSdkVersion 22
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:22.2.0'
compile 'org.openimaj:openimaj:1.3.1'
}
Is my module build file. It reports no errors when syncing to the IDE.
However when I try to construct any of the classes in one of the source files the Android Studio does not recognize any of the classes from the openimaj dependency.
Any Help much appreciated.
Thank you!
I think it might be because you've specified a non-jar OpenIMAJ dependency (specifically you've told it to link against the OpenIMAJ master pom file, which only contains references to the different sub-modules). You probably need to actually choose the specific modules that you want - for example if your application is doing image processing, then add the dependency org.openimaj:image-processing:1.3.1.
Edit:
It seems that the batik svg libraries have a circular dependency somewhere that breaks Gradle (see https://issues.apache.org/jira/browse/BATIK-1098). This is what causes an eventual StackOverflowError. Additionally, something is pulling in xml-apis which will conflict with Android. Assuming you don't mind not have SVG image support, then the following should work:
repositories {
mavenCentral()
maven {
url "http://maven.openimaj.org"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:18.+'
compile('org.openimaj:image-processing:1.3.1') {
exclude group: 'org.apache.xmlgraphics'
exclude group: 'xml-apis'
}
}
You might also want to add additional exclusions for dependencies that are not needed in your app - it seems that just including org.openimaj:image-processing pulls in lots of things that are almost certainly not going to be needed (I've created an issue for that here: https://github.com/openimaj/openimaj/issues/97).

Gradle Error: Could not find property file

I had posted a previous question and ask this in the comments, but received no response. I have been struggling with finding a solution to this for a couple weeks now.
I am trying to add .jar files to my program for uploading a document in an android app. However, it says gradle was unsuccessful and it could not find property 'file'.
Here is the error:
Error:(25, 0) Could not find property 'file' on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated#41762346.
Here is the code
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.ashleygreen.healthcareapp"
minSdkVersion 15
targetSdkVersion 21
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'
compile file 'libs/httpclient-4.2.2.jar.zip'
compile file 'libs/httpmime-4.0.jar.zip'
}
Firstly, Gradle cannot work with zipped jar files - you'll have to unzip the jar files first.
When you include the line
compile fileTree(dir: 'libs', include: ['*.jar'])
This means that any file ending in .jar will be included as a dependency as long as it is in the libs directory. Therefore there is no need to use either of the last lines (as those libraries are already included via the first line).
However, if you did want to include a single jar file from another directory, you're missing the parentheses after file:
compile file('libs/httpmime-4.0.jar')
I faced the same error. However, the following worked for me (in android studio 1.5):
compile files ('libs/httpclient-4.2.2.jar.zip')
compile files('libs/httpmime-4.0.jar.zip')
Notice it's files with an extra 's' with parentheses. Only file is not recognized.

Run modules independently with source dependencies in Android Studio (Intellij?)

I am using Android studio but this may be a general Intellij question (I'm used to eclipse). This seems like it should be super simple, but I can't figure it out.
I want to be able to run either module in my project independently, which is from what I understand the purpose of a module. This has worked fine until now when I want to call code from my "app" module in my "testmodule" module. Android Studio lets me import the code I need with no problems until I try to build the module. Then I get the expected "package not found" error. So then I try adding this in my "testmodule" build.gradle:
main.java.srcDirs '../app/src/main/java'
This causes two package import statements in my "app" module to fail. One of the failing packages is the package defined in my "app" manifest and the other is specified in my "app" build.gradle
sourceSets {
main.java.srcDirs += '../../connectorAPI/trunk/src'
}
I also tried to make one module depend on another but from what I've read in stack overflow (and the vague Android Studio error) one module must be a library file. This obviously doesn't work since I want to be able to run either. I would love to just copy the code I need but then I'd need to change both every time in version control, which is a nightmare.
In eclipse all you have to do is right click and link source. I can't imagine this is impossible or even out of the ordinary, so I must be missing something simple... can someone please help me out?
build.gradle testmodule
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.XXXXX.testmodule"
minSdkVersion 15
targetSdkVersion 21
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'
compile 'com.google.android.gms:play-services:+'
}
build.gradle app
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.XX.XXX.XXX.XXX"
minSdkVersion 15
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
packagingOptions {
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
sourceSets {
main.java.srcDirs += '../../connectorAPI/trunk/src'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:19.1.0'
compile 'com.google.android.gms:play-services:+'
// Add the ArcGIS Android 10.2.5 API
compile 'com.esri.arcgis.android:arcgis-android:10.2.5'
compile 'com.googlecode.json-simple:json-simple:1.1'
}
I might be late by a few years but here is what I would do : Declare both modules as library, and create another two as application. Then you can easily import the two libriaries while still having two applications, if that is what you want.
As for the duplicate dependency issue that will arise, you can just add an exclude statement in the build.gradle respectively.

AndroidStudio cannot resolve R and layout files

When I upgraded AndroidStudio from 0.8 (or 0.9) to 1.0.2 AndroidStudio was not able to resolve my R file and suggests importing it (import my.package.R). I'm using SDK Tools 24.0.2 and SDK Platform-tools 21.
I've tried cleaning, resynching and rebuilding the project, as well as checking the layout files for errors, but I didn't find anything.
Another weird thing is that when typing "R.layout" AndroidStudio suggests either "android.R" or "my.package.app.R". When chosing the former I'm not able to use my layout files, when chosing the later it automatically imports "my.package.app.R"
When importing R a co-worker of mine is even able to run the app...
This is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 16
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "my.package.app"
minSdkVersion 14
targetSdkVersion 20
versionCode 6
versionName '0.1.6'
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
I think it could be of use to some of you here.
Thanks!
Edit: I was told that it is perfectly normal to import one's own R class. Sorry for the inconvenience...
If is showing error (red signed) and is not compiling I'm pretty sure there is something wrong in at least one XML file (like a tag unclosed).
I had same kind of error in Android Studio , the reason it gives Red Mark is due to some small errors , ending or opening tags are not been properly closed or opened!

Categories

Resources