Android gradle build includes jni libs twice - android

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/

Related

Building android project with ant

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.

'Conversion to Dalvik format failed with error 1' and 'java.lang.IllegalArgumentException: already added: '

I'm having this problem since I installed the new SDKs. I've read about this happening when I try to link the same class to the build twice.
But here is the peculiar thing about it:
I have an Android library project that includes a class folder on the build path.
I have this library project included in one of my regular Android projects.
I have to include the class folder in this regular project as well to make it build.
This when the error occurs.
But when I do either of the following:
Remove the class folder from the library project: The library project won't build, so the regular project won't either.
Remove the class folder from the regular project: The library project builds, but the regular project won't because it misses the class files (?!).
I seem to be stuck in some unsolvable paradigm here.
Maybe there are build rules to circumvent this?
Any help would be greatly appreciated!
Apparently the way Android library projects are linked has changed.
Before, the library's source folder would be linked to the build path.
With the new solution the library is apparently built to a jar file in its own 'bin' folder.
This is then linked as an external jar to the build path of the project using the library.
To make the conflict dissappear, do two things:
Remove the source folder link in Project properties->Java Build Path->Source
Clean the project. Perhaps clear your projects 'bin' folder manually.
Now it should work again! :)

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)

JNI sources in the APK

I have an Android project with an NDK/JNI library in Eclipse Ganymede. Inexplicably, the bin folder contains a copy of all my C++ sources from the jni folder. So does the APK, if you rename it to ZIP and examine. If I delete those files from bin, then clean and rebuild, they're there again. What going on, please? I have no intention of distributing the sources along with the app.
Kinda resolved. For some reason, jni was listed in project properties under "Java build path". Once I've removed that, it was fine. No sure how did this condition come to be.

How to build an android app with external libraries using ant?

I have an existing project that builds fine using my IDE. I'd like to use the "android update" command to generate an ant buildfile for this project.
The buildfile is generated fine, but the build fails because it's not building with some jarfiles I have in my libs directory.
I'd like to figure out the proper way to tell ant to build with some external jar files in my libs directory. How should I do this? Is it a property in build.properties? Do I need to modify build.xml somehow? Or is there a different solution entirely?
but the build fails because it's not
building with some jarfiles I have in
my libs directory.
And your error message is...what? I suspect you may be misinterpreting the error message.
I'd like to figure out the proper way
to tell ant to build with some
external jar files in my libs
directory. How should I do this?
Just put them in libs/, as Ant will add everything in there to your build path. See this project, and this project, and this project for examples.
I spent some time trying to get the Facebook API to work with ant. The trick for me was to add this to my default.properties files.
android.library.reference.1=../Facebook
Where ../Facebook contains AndroidManifest.xml, etc. The real key being the relative path. Don't use an absolute path because Ant seems to treat your project directory as the root.
This should hold true for other library projects that you are including from source code.
I was dealing with similar issue. I'm building Android project on Jenkins using standard Ant build.xml (generated by Android SDK). I also have reference to another Java project with some shared domain classes. In Eclipse there is no problem with it. The domain project is a project reference. However on Jenkins this domain.jar is built by Maven and it was not accessible by Android project.
I have finally solved it by adding this at the end of build.xml:
<target name="-pre-build">
<copy todir="${jar.libs.dir}">
<fileset
dir="../path-to-another-project/target"
includes="*.jar" />
</copy>
</target>
This copies all jars from the target directory of another project into "libs" directory of my Android project. The -pre-build Ant target is automatically called before Android compilation starts.
I agree with Mark, however, if you're planning to modify your build script further - than you need to make it custom. Bring tasks from android/platforms/android-PLATFORMVERSION/templates/android_rules.xml to your build.xml and modify whatever you want to modify. Including location for external libs.

Categories

Resources