JNI Library cause mismatch in the apk certificate

Put problems with the Android NDK in this forum.

JNI Library cause mismatch in the apk certificate

Postby bemipefe » Mon Apr 09, 2012 4:37 pm

Hi !

I'm trying to understand how build and run a apk package with native library. I have build the "hello-jni" sample obtaning the libs/armeabi/libhello-jni.so library. I'm using Netbeans 7.1.1 so googling a bit i found this xml code to add on the build.xml file:
Code: Select all
    <target name="-post-jar">
    <zip update="true" destfile="${dist.apk}">
    <zipfileset dir="libs/armeabi" includes="*.so" prefix="libs/armeabi"/>
    </zip>
    <zip destfile="tmp.apk">
    <zipfileset src="${dist.apk}">
    <exclude name="META-INF/." />
    </zipfileset>
    </zip>
    <move file="tmp.apk" tofile="${dist.apk}" />
    <signjar jar="${dist.apk}" alias="androiddebugkey" storepass="android" keypass="android" keystore="myhome/.android/debug.keystore"/>
    </target>


without this the library is not added to the apk package and the application can't load the library with System.loadLibrary function.

The problem is that i got the error Failure [INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES]
and "adb logcat" show the error Package sample.hellojni has mismatched certificates at entry libs/armeabi/libhello-jni.so; ignoring!

NOTE: It's the fist time that i install the application and can't try to uninstall it because it's not installed. I have just tried to remove the ~/.android/ directory and to do ./emulator -adv Android_1 -wipe-data without success.

Somebody can help me please ?
bemipefe
Freshman
Freshman
 
Posts: 6
Joined: Fri May 20, 2011 4:55 pm

Top

Re: JNI Library cause mismatch in the apk certificate

Postby bemipefe » Tue Apr 10, 2012 10:38 am

I have found the solution to the problem.
The build.xml file that i have take from internet contains many error.

1) The library .so contained in libs/armeabi seem to be a fake library. Indeed if i do arm-linux-androideabi-nm libs/armeabi/libhello-jni.so from the root directory of the project i get:

Code: Select all
arm-linux-androideabi-nm: libs/armeabi/libhello-jni.so: no symbols


...rather than:

Code: Select all
00000c24 T Java_sample_hellojni_HelloJni_stringFromJNI
00003258 a _DYNAMIC
00003340 a _GLOBAL_OFFSET_TABLE_
00001c84 T _Unwind_Backtrace
00000cf4 T _Unwind_Complete
....


that i got with the command arm-linux-androideabi-nm obj/local/armeabi/libhello-jni.so. You can see the function symbol in the first row. So this is the correct location from which take the library.

2) The signature information contained in the .apk package are holded in the directory META-INF. When the package is updated with the *.so libs before being signed this directory must be excluded or erased but the tag <exclude name="META-INF/." /> don't do this work because of the "." dot. So the correct tag is:

Code: Select all
<exclude name="META-INF/" />


Finally the correct build.xml file is:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<project name="HelloJni" default="default" basedir=".">
    <description>Builds, tests, and runs the project HelloJni.</description>
    <import file="nbproject/build-impl.xml"/>

    <target name="-post-jar">
    <zip update="true" destfile="${dist.apk}">
    <zipfileset dir="obj/local/armeabi" includes="*.so" prefix="libs/armeabi"/>
    </zip>
    <zip destfile="tmp.apk">
    <zipfileset src="${dist.apk}">
    <exclude name="META-INF/" />
    </zipfileset>
    </zip>
    <move file="tmp.apk" tofile="${dist.apk}" />
    <signjar jar="${dist.apk}" alias="androiddebugkey" storepass="android" keypass="android" keystore="/myhome/.android/debug.keystore"/>
    </target>

</project>


Of course myhome is my Linux home directory.
The package now is installed correctly. Unfortunately the libhello-jni.so seem to be missing in /data/data/ so i have to push it manually with adb push to get the app work. Maybe i open another topic for that.
bemipefe
Freshman
Freshman
 
Posts: 6
Joined: Fri May 20, 2011 4:55 pm

Re: JNI Library cause mismatch in the apk certificate

Postby bemipefe » Tue Apr 10, 2012 2:17 pm

I have found another problem. The library .so is expected in the .apk package to be in lib/armeabi and not in libs/armeabi. And the libs/armeabi/libhello-jni.so seem to work again. So the correct build.xml for Netbeans must contains:

Code: Select all
...
    <target name="-post-jar">
    <zip update="true" destfile="${dist.apk}">
    <zipfileset dir="libs/armeabi" includes="*.so" prefix="lib/armeabi"/>
    </zip>
    <zip destfile="tmp.apk">
    <zipfileset src="${dist.apk}">
    <exclude name="META-INF/" />
    </zipfileset>
    </zip>
    <move file="tmp.apk" tofile="${dist.apk}" />
    <signjar jar="${dist.apk}" alias="androiddebugkey" storepass="android" keypass="android" keystore="/myhome/.android/debug.keystore"/>
    </target>

...



Where myhome is my Linux home directory.
I add the output of the zipinfo dist/HelloJni.apk command

Code: Select all
Archive:  dist/HelloJni.apk
Zip file size: 10122 bytes, number of entries: 12
-rw----     2.0 fat      426 bl defN 12-Apr-10 15:05 META-INF/MANIFEST.MF
-rw----     2.0 fat      547 bl defN 12-Apr-10 15:05 META-INF/ANDROIDD.SF
-rw----     2.0 fat      776 bl defN 12-Apr-10 15:05 META-INF/ANDROIDD.RSA
-rw----     1.0 fat        0 b- stor 12-Apr-10 15:05 lib/
-rw----     1.0 fat        0 b- stor 12-Apr-10 15:05 lib/armeabi/
-rw----     1.0 fat        0 b- stor 12-Apr-10 15:05 res/
-rw----     1.0 fat        0 b- stor 12-Apr-10 15:05 res/layout/
-rw----     2.0 fat     1596 bl defN 12-Apr-10 15:05 AndroidManifest.xml
-rw----     2.0 fat     2128 bX defN 12-Apr-10 15:05 classes.dex
-rw----     2.0 fat    10100 bl defN 12-Apr-10 12:10 lib/armeabi/libhello-jni.so
-rw----     2.0 fat      740 bl defN 12-Apr-10 15:05 res/layout/main.xml
-rw----     2.0 fat      756 bl defN 12-Apr-10 15:05 resources.arsc
bemipefe
Freshman
Freshman
 
Posts: 6
Joined: Fri May 20, 2011 4:55 pm

Top

Return to NDK Problems

Who is online

Users browsing this forum: No registered users and 2 guests