loki loki - 11 months ago 88
Android Question

How to build android apk from command line?

How can I build an APK file from the command line? I've tried

MSBuild myProject.dproj /p:Config=Release /p:Platform=Android

but no APK file is produced, only the .so file.

Answer Source

Some experimentation shows that the following aspects of the various proposed courses of action come into play for a more complete picture.

When you create a new Delphi multi-target project (let's call it Foo) you have one MSBuild-compatible file created: the Foo.dproj Delphi project file. In the case of Android (the platform in this question) this is enough to build the target libFoo.so library file that would eventually become part of a deployed .apk file, but is not enough to make the .apk file just yet.

You can build your .so file, which contains the ARM machine code, using a command line like this:

msbuild Foo.dproj /property:Config=Debug /property:Platform=Android /target:Build

or this more concise version:

msbuild Foo.dproj /p:Config=Debug /p:Platform=Android /t:Build

As per the documentation you can use the Deployment Manager's Deploy button to create an additional MSBuild file, or indeed just choose the Project, Deploy libFoo.so menu item. The initial step of this deployment is to create a Foo.deployproj file, which is another MSBuild-compatible file.

With Foo.deployproj file present this following line will take the libFoo.so file and anything else required for deployment and use these to build up the .apk Android application package file:

msbuild Foo.dproj /p:Config=Debug /p:Platform=Android /t:Deploy

The Deploy target will fail if the required files such as libFoo.so are not present, say by previously running MSBuild using the Build or Make MSBuild targets: s(431,5): error : Required local file "Android\Debug\libFoo.so" not found. Deployment failed.

Additionally the Deploy target fails if you have not got a Foo.deployproj file generated for your project: error MSB4057: The target "Deploy" does not exist in the project.

That said, if you have the Foo.deployproj present you can build and deploy in one fell swoop with something like:

msbuild Foo.dproj /p:Config=Debug /p:Platform=Android /t:Build;Deploy

Of course to avoid compiling files that have not changed this is perhaps better:

msbuild Foo.dproj /p:Config=Debug /p:Platform=Android /t:Make;Deploy

The fact that the documentation doesn't mention anything about the Deploy target is a little confusing. There is potential to think that it implies you run MSBuild against Foo.deployproj file, but that yields no joy whatsoever. It seems the documentation is out of date or plain wrong.