Welcome, Guest
Username: Password: Remember me
CodeTyphon Cross-Build Development, discussions and problems
  • Page:
  • 1

TOPIC:

APK build 10 years 11 months ago #4805

  • Aleksandar
  • Aleksandar's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 150
  • Thank you received: 31
I finally managed to build .apk from compiled .so library.

Unfortunately it crashes the same moment it is started. :S

EDIT: I think I found a reason for this. .so library is not packed inside APK.

Any ideas?

Please Log in or Create an account to join the conversation.

Last edit: by Aleksandar.

APK build 10 years 11 months ago #4808

  • Aleksandar
  • Aleksandar's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 150
  • Thank you received: 31

viking wrote: I finally managed to build .apk from compiled .so library.

Unfortunately it crashes the same moment it is started. :S

EDIT: I think I found a reason for this. .so library is not packed inside APK.

Any ideas?


With current project configuration compiled .so library doesn't have .so in file name. Native library file needs to have .so extension.

Changing file name before calling apk build script will provide proper packaging of .so inside apk. :woohoo:

Unfortunately I still have same problem with app crash on start. :S

Please Log in or Create an account to join the conversation.

APK build 10 years 11 months ago #4873

  • trustfm
  • trustfm's Avatar
  • Offline
  • New Member
  • New Member
  • Posts: 9
  • Thank you received: 2
You have the same problem with me . The problem is that your ndk & sdk version are incompatible with the compiled codetyphon ide (customdrawn 0.0 ).
The solution is not that easy . Here i have made a step by step tutorial on how i managed to fix this problem
Pay attention at the 5th step.

forum.lazarus.freepascal.org/index.php/topic,22414.0.html

I do not know if my tutorial will work on codetyphon.
Right now i use laz4andoid for android and the rest with codetyphon

Let us know what happened
The following user(s) said Thank You: 4aiman

Please Log in or Create an account to join the conversation.

Last edit: by trustfm.

APK build 10 years 9 months ago #5096

  • sax
  • sax's Avatar
  • Offline
  • New Member
  • New Member
  • Posts: 10
  • Thank you received: 0
viking, could you give me instruction how to build *.apk file from *.so file, please?
I create project according to the instructions from wiki, but I really stuck on building *.apk file.
thx SaX

Please Log in or Create an account to join the conversation.

APK build 10 years 9 months ago #5126

  • Aleksandar
  • Aleksandar's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 150
  • Thank you received: 31

sax wrote: viking, could you give me instruction how to build *.apk file from *.so file, please?
I create project according to the instructions from wiki, but I really stuck on building *.apk file.
thx SaX


I really do not have much time, since I need to finish some things for work today, but I will try to be as clear as possible.

There is one thing missing in CT tutorial - the fact that target name in example project should be changed to "android\libs\armeabi\liblclapp.so".

Looks like you already done that, since you have .so library. Good.

You need to setup JDK, Android SDK and Android NDK properly. I guess you already done all that, but here is quick checkout list what should be done:

1. Install JDK, set JAVA_HOME variable. Put the path to the JDK without \bin in the end. For example "C:\Program Files\Java\jdk1.7.0_51"
2. Download and install Android SDK and NDK.
3. Start SDK manager and install Tools, Build Tools, Platform Tools, Android 2.2 (API 8) and Android 4.0 (API 14).

Then you need to change build_debug_apk.bat

First you need to set some variables:

APP_NAME = name of your apk file
APK_PROJECT_PATH = folder where your apk project is
ANDROID_HOME = folder with android sdk
APK_SDK_PLATFORM = folder with skd files for specific platform
In PATH, you need to have %ANDROID_HOME%\build-tools\<version number> - folder named by version number is probably only folder in %ANDROID_HOME%\build-tools folder. 19.0.1 for example.

With CT 4.7 if you want to create apk from adroid lcl example, and if you have installed android-sdk_r22.3-windows and android-ndk-r9c-windows-x86_64, first few lines should look like this:
REM Adjust these paths to yours
SET APP_NAME=androidlcltest
SET APK_PROJECT_PATH=c:\codetyphon\CodeOcean\2_Basics\samples\androidlcl\android
SET ANDROID_HOME=c:\Android\android-sdk-windows
SET APK_SDK_PLATFORM=%ANDROID_HOME%\platforms\android-8
SET PATH=%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\build-tools\19.0.1;%JAVA_HOME%\bin

This part is very obvious:
REM Create necessary directory Structure
mkdir bin
mkdir bin\classes
mkdir gen
mkdir gen\com
mkdir gen\com\pascal
mkdir gen\com\pascal\lcltest
mkdir raw
mkdir raw\lib
mkdir raw\lib\armeabi

REM Cleanup
del bin\%APP_NAME%.ap_
del bin\%APP_NAME%.apk
del raw\lib\armeabi\*.so

This part doesn't have clear explanation. This will copy your created .so file in another folder. If your CT produced file doesn't have .so extension it will not be copied and you will end with apk that can not even show your form.
REM More directory preparation
copy libs\armeabi\*.so raw\lib\armeabi\

And now real work start. First create R.java.
REM Resource compilation
call aapt p -v -f -M AndroidManifest.xml -F bin\%APP_NAME%.ap_ -I %APK_SDK_PLATFORM%\android.jar -S res -m -J gen raw

Next compile java sample from src folder.
REM Java compiler
call javac -verbose -encoding UTF8 -classpath %APK_SDK_PLATFORM%\android.jar -d bin\classes src\com\pascal\lcltest\LCLActivity.java

Create classes.dex
REM DX to convert the java bytecode to dalvik bytecode
call dx --dex --verbose --output=%APK_PROJECT_PATH%\bin\classes.dex %APK_PROJECT_PATH%\bin\classes

Now we need to rebuild unsigned apk. Since APK Builder is deprecated we will use aapt for this. Notice that this can be done with 7zip or any zip capable packer.
I had struggle with some parameters of aapt add, so I had to copy classes.dex to current folder and to move ap_ to apk at the end.
REM Now build the unsigned APK
del classes.dex
del %APK_PROJECT_PATH%\bin\%APP_NAME%-unsigned.apk
copy bin\classes.dex classes.dex
call aapt add bin\%APP_NAME%.ap_ classes.dex
move bin\%APP_NAME%.ap_ bin\%APP_NAME%-unsigned.apk

Generate key on the fly for test. For production you should create proper key first. Notice that both passwords are senhas in this example, so you need to use same passwords later. This line should be commented for production. Enter your name instead of John Doe.
REM Generating on the fly a debug key
keytool -genkey -v -keystore bin\LCLDebugKey.keystore -alias LCLDebugKey -keyalg RSA -validity 10000 -dname "CN=Jonh Doe" -storepass senhas -keypass senhas

Singe app with created key using password senhas as when creating the key. You will put your pass here when you create key properly.
REM Signing the APK with a debug key
del bin\%APP_NAME%-unaligned.apk
jarsigner -verbose -keystore bin\LCLDebugKey.keystore -keypass senhas -storepass senhas -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugKey

Align the apk.
REM Align the final APK package
zipalign -v 4 bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%.apk

EDIT: I fixed a typo in key generation line. I also added more info about setting your name in key.

Unfortunately all this will not get you far with current CT or Lazarus install. You will get main window, but your app will crash whatever you click. It's global problem with latest FPC.

I will make separate post how to temporary overcome this problem. I managed to make this work with some files from older fpc version.


File Attachment:

File Name: build_debug_apk.zip
File Size:1 KB

Please Log in or Create an account to join the conversation.

Last edit: by Aleksandar.

APK build 10 years 9 months ago #5131

  • sax
  • sax's Avatar
  • Offline
  • New Member
  • New Member
  • Posts: 10
  • Thank you received: 0
THX viking!
If you use JDK 1.7 then you have to change line

jarsigner -verbose -keystore bin\LCLDebugKey.keystore -keypass senhas -storepass senhas -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugKey
to
jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore bin\LCLDebugKey.keystore -keypass senhas -storepass senhas -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugKey

Please Log in or Create an account to join the conversation.

APK build 10 years 9 months ago #5133

  • Aleksandar
  • Aleksandar's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 150
  • Thank you received: 31

sax wrote: THX viking!
If you use JDK 1.7 then you have to change line

jarsigner -verbose -keystore bin\LCLDebugKey.keystore -keypass senhas -storepass senhas -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugKey
to
jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore bin\LCLDebugKey.keystore -keypass senhas -storepass senhas -signedjar bin\%APP_NAME%-unaligned.apk bin\%APP_NAME%-unsigned.apk LCLDebugKey


If you generate key using JDK7 with this line:
keytool -genkey -v -keystore bin\LCLDebugKey.keystore -alias LCLDebugKey -keyalg RSA -validity 10000 -dname "CN=Jonh Doe" -storepass senhas -keypass senhas

there is no need to change anything in signing part. You just need to delete the key that you generated with earlier version first.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1