Building android project with ant - android

I am trying to build my android project using ant in command line mode.
It works with eclipse and it used to work with ant until i installed the latest android sdk.
I run ant release -buildfile projectdir\build.xml. The compilation process is ok, and after aligning the apk, it fails at runtime with ClassDefNotFoundError acra.ACRA.
I have tried to remove any reference to acra from the project, but it will fail at runtime when trying to execute any code coming from a library jar.
My jars are in the libs folder at the root of the project. And I sort of understood this would cause ant to link them into the apk. But it doesn't.
When I used a previous version of the android sdk, I had a file named build.properties with an attribute referencing the jars folder external.libs.dir=libs. But now I can't make this to work any more, even if I use the new attributes names jar.libs.dir=libs and the new attributes file name ant.properties.
I read that ClassDefNotFoundError is caused because at compile time the librairies are found, but they are not linked into the apk, so they can't be found at runtime.
How can I link the external jars in the apk please ?

The compilation process is ok, and after aligning the apk, it fails at runtime with ClassDefNotFoundError acra.ACRA
There is no acra.ACRA class in ACRA, at least not in the current edition. It's org.acra.ACRA.
My jars are in the libs folder at the root of the project. And I sort of understood this would cause ant to link them into the apk. But it doesn't.
Yes, it does. Then ProGuard is removing them, unless you teach ProGuard not to.

Related

Android gradle build includes jni libs twice

I've been trying to solve a problem about Android build, but couldn't figure out how to solve it.
Basically, I am trying to build an Android project using Gradle. It works perfect, but the size of the final apk is 7MB more than when I builded with Eclipse.
When I unzip the apk, I see that the JNI Libs are included twice. Which is different from the Eclipse build that includes it only one time. Here is the paths I can find them in the APK:
lib/armeabi
lib/armeabi-v7a
main/jniLibs/armeabi
main/jniLibs/armeabi-v7a
In my project, those two files are in:
android/app/src/main/jniLibs/armeabi
android/app/src/main/jniLibs/armeabi-v7a
I have two different build.graddle files in:
android/
android/app/
None of them contains anything related to the JNI Libs.
To build a release, I use the command:
./gradlew assembleRelease
And everything works fine. I use Gradle 1.10.
I was wondering if someone ever encountered the problem and find a solution to avoid to the JNI Libs to be included twice in the APK.
Thanks :)
Put .so files in...
/src/main/jniLibs/armeabi-v7a
/src/main/jniLibs/x86
directories and gradle will correctly package the .so files into the correct app and it won't include the duplicates
This is a good reference..
http://www.shaneenishry.com/blog/2014/08/17/ndk-with-android-studio/

Can an Android Library Project (APKLIB) be obfuscated by Proguard?

I am attempting to obfuscate an Android Library Project (APKLIB), allowing the library to distributed to potential clients for test development without compromising the majority of the source code.
Unfortunately, there is no documentation and no examples that Google can provide, and the only references to obfuscated APKLIBs are question posts with no solutions.
I have attempted to use the android-maven-plugin as well as the maven-proguard-plugin with Maven to obfuscate the APKLIB, but the end result is not obfuscated. I also tried moving the proguard goal to the process-classes and prepare-package phases of maven with no success.
My last resort is splitting the source code out of the APKLIB and obfuscating it as a JAR, while leaving the APKLIB as AIDL files and resources with a dependency on the obfuscated JAR. However, I would like to avoid this if possible since our project has a large number of libraries already.
Does anyone know if it is even possible to obfuscate an APKLIB? And if so, how?
Hi are you asking about creating a obfuscated jar file?
You can do that by adding build.xml file to it and making changes in your proguard-project.txt file and running "ant release" from command line and you will find the obfuscated.jar under a proguard folder in your bin folder of project.
Steps;
-> To add build.xml in your project in linux open a terminal and go to android/tools/ant folder and run ./android update project -p /path of your android library project. And you will find build.xml in your project root directory.
-> To enable Obfuscation in your project in your editor open a project.properties file and remove comment from proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
-> Add your changes to proguard-project.txt file and in terminal go to your project and run ant release

Discrepancy between ant release and Eclipse export of Android jar?

I'm trying to create an Android library JAR and would like to use ant for an automated build.
I noticed that when I use ant release, however, the jar file gets added an extra android/annotation folder with TargetApi.class and SuppressLint.class. This causes problems when I try to run an app with the jar, I get:
java.lang.IllegalArgumentException: already added: Landroid/annotation/SuppressLint;
How can I have ant not try to add these classes which appear redundant?
Specifically, the problem seems to be that ant is including annotations.jar in my lib, then when my other project imports that lib it encounters conflict with its own annotations.jar.
edit:Determined that this is ProGuard-related, as the original.jar it creates doesn't have the annotations.jar classes, but obfuscated.jar does. Still not sure how to bypass this step.
In the ProGuard config, I have it only keep the files from my package name.

Correct way to add android libraries with ADT 14 >

After updating to ADT 14/15 I started having a couple problems with our build.
With one Android based libraries (call it Framework) which is added into the actual android project app (call it App). The Framework project has a couple jar files in its own lib folder which is added to its own build path. Those same jar files are required to be added to the App's build path as well.
So jar files on the Framework build path
Framework/libs roboguice
Framework/libs gson
Those same jar files on the App build path
Framework/libs roboguice
Framework/libs gson
Also the Framework.apk is added via the Android Library panel in the App's project properties.
Both projects are targeting the same Android.
Now when I build the project I seem to have resolved the errors however when running it, at times I receive the Missing Framework.apk in the console window.
So based on this scenario any thoughts on how to correct this build? I have a feeling its still setup incorrectly.
This should help you out!
http://android-developers.blogspot.com/2011/10/changes-to-library-projects-in-android.html

How to include JAR in APK without Eclipse?

I maintain an Android app and am not using Eclipse. I am not using Eclipse. I am using ant and build.xml and build.properties.
I have places my .jar file into the libs/ directory. My code compiles just dandy. But when I run it on the emulator, the output APK does not include the .jar, so I get a runtime stacktrace:
ERROR/AndroidRuntime(470): java.lang.NoClassDefFoundError: com.google.ads.AdView
my build.properties looks like this:
jar.libs.dir=libs
And the libs/ directory contains my .jar file.
What needs to be in build.xml so that the external .jar file is included in the APK?
Edit: In theory this answer should work, but it doesn't for me. Is it out of date? What gives? How to add external jar libraries to an android project from the command line
I just came over a similar problem and noticed that libraries should not be placed in "myprojectdir\lib". When I moved them to "myprojectdir\libs" everything started to work.
It turns out that I needed to upgrade the version of ant I was using to 1.8. During the compile process, I had been getting this error message:
Warning: Reference out.dex.jar.input.ref has not been set at runtime,
but was found duringbuild file parsing, attempting to resolve. Future
versions of Ant may support referencing ids defined in non-executed
targets.
I googled it, and found that I needed to upgrade Ant, and now I don't get this warning, and my application does not force close.
What needs to be in build.xml so that the external .jar file is included in the APK?
Just putting it in libs/ is sufficient.
my build.properties looks like this:
That line should not be necessary. It does not appear in my build.properties files that build successfully with JAR files.
If you use dexdump -f classes.dex from your project's bin/ directory, you will be able to determine whether com.google.ads.AdView made it in there. If it did not, then something is strange with your build scripts. If it did, then perhaps there is a dependent JAR that you are missing (though I would expect a VerifyError in that case).
You use 3rd party library, but you seem didn't run DX on it. Make sure that not only your code processed by DX tool (I assume Ant does it), but also all 3rd party libraries you use. You can look in 7Bee script I use to convert web applications to Android davlik format, so it can work for you too. You can find more about the script on Atjeews page.
Solution:
right click on the project in project tree and select Project
properties
select Java Build Path
select TAB Order
and Export
check GoogleAdMobAdsSdk-4.0.4.jar (or your
version SDK)
press OK
clean project by menu Project
-> Clean
rebuild project (Project – Build Automatically)

Categories

Resources