Cross compile to native android - android

How can I cross compile from my MacOS to Android native binary.
I read this, that I've to do:
export NDK_ROOT=~/dev/ndk-toolchain
./android-ndk-r10c/build/tools/make-standalone-toolchain.sh --platform=android-16 --install-dir=$NDK_ROOT
export NDK_CC=~/dev/ndk-toolchain/bin/arm-linux-androideabi-gcc
CC_FOR_TARGET=$NDK_CC GOOS=android GOARCH=arm GOARM=7 ./make.bash
export NDK_TOOLCHAIN=~/dev/ndk-toolchain
export CC=$NDK_TOOLCHAIN/bin/arm-linux-androideabi-gcc
export GOROOT=~/dev/go
export GOPATH=`pwd`
export GOOS=android
export GOARCH=arm
export GOARM=7
export CGO_ENABLED=1
GO="$GOROOT/bin/go"
$GO build -x main.go
I installed NDK from Android Studio, but could not find the mentioned path /dev/ndk-toolchain/bin/arm-linux-androideabi-gcc
In my device I've:
$ cd /Users/hajsf/Library/Android/sdk/ndk/22.1.7171670/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
$ ls
arm-linux-androideabi bin lib share
$ cd bin
$ ls
arm-linux-androideabi-addr2line arm-linux-androideabi-elfedit arm-linux-androideabi-nm arm-linux-androideabi-size
arm-linux-androideabi-ar arm-linux-androideabi-gprof arm-linux-androideabi-objcopy arm-linux-androideabi-strings
arm-linux-androideabi-as arm-linux-androideabi-ld arm-linux-androideabi-objdump arm-linux-androideabi-strip
arm-linux-androideabi-c++filt arm-linux-androideabi-ld.bfd arm-linux-androideabi-ranlib
arm-linux-androideabi-dwp arm-linux-androideabi-ld.gold arm-linux-androideabi-readelf
$

Not sure if this is correct or no, I tried the below and it worked with me the way I want.
In my mac:
$ GOOS=android GOARCH=arm64 GO build -x main.go
Then I saved the file at GDrive, then from my mobile, I downloaded it, then shared it with the Tramux and got it saved at folder ~/Downloads I gone there and made it executable as:
$ chmod +x server
Then run it as:
$ ./server
And got it working with me!

I downloaded NDK, extracted it, and found the correct path as:
/Users/hajsf/Downloads/android-ndk-r21e/toolchains/llvm/prebuilt/darwin-x86_64/bin
And was able to compile as:
$ export NDK=~/Downloads/android-ndk-r21e
$ CGO_ENABLED=1 \
GOOS=android \
GOARCH=arm64 \
CC_FOR_TARGET=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android21-clang
$ GO build -x main.go
In this, there is this line, but it gave me an error, and witout it, I was able to compile correctly
./all.bash

Related

How to cache gradle dependencies in docker when building for android

I am trying to build an Android Studio project inside docker using gradle.
Gradle goes through a long process of downloading my dependencies. I would like to cache this step inside docker, so that it is not repeated every time I build.
I am unhappy with the underlying snippet from my Dockerfile (full file below). It requires that I copy my resource directory. Everytime I change a string or a resource, all of my dependencies will be downloaded again! (Because docker cache will be invalidated)
COPY my-android-project/app/src/main/res app/src/main/res
This line will trigger alot of redundant dependency downloads by docker. How can I remove it from my dockerfile? I would appreciate a Dockerfile example with even a trivial "empty activity" Android Studio project...
# https://medium.com/#AndreSand/building-android-with-docker-8dbf717f54d4
FROM gradle:5.4.1-jdk8
USER root
ENV SDK_URL="https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip" \
ANDROID_HOME="/usr/local/android-sdk" \
ANDROID_VERSION=29 \
ANDROID_BUILD_TOOLS_VERSION=29.0.2
# Download Android SDK
RUN mkdir "$ANDROID_HOME" .android \
&& cd "$ANDROID_HOME" \
&& curl -o sdk.zip $SDK_URL \
&& unzip sdk.zip \
&& rm sdk.zip \
&& mkdir "$ANDROID_HOME/licenses" || true \
&& echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_HOME/licenses/android-sdk-license"
# && yes | $ANDROID_HOME/tools/bin/sdkmanager --licenses
# Install Android Build Tool and Libraries
RUN $ANDROID_HOME/tools/bin/sdkmanager --update
RUN $ANDROID_HOME/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS_VERSION}" \
"platforms;android-${ANDROID_VERSION}" \
"platform-tools"
# Install Build Essentials
RUN apt-get update && apt-get install build-essential -y && apt-get install file -y && apt-get install apt-utils -y
RUN mkdir my-android-project
WORKDIR my-android-project
# Fix gradle cache directory to work outside of profile (required for docker)
RUN mkdir -p /opt/gradle/.gradle
ENV GRADLE_USER_HOME=/opt/gradle/.gradle
# Cache dependencies so that they are not downloaded every time the code changes
COPY my-android-project/gradle* ./
COPY my-android-project/build.gradle .
COPY my-android-project/settings.gradle .
COPY my-android-project/gradle/ ./gradle/
COPY my-android-project/gradle/ ./gradle/
COPY my-android-project/app/build.gradle app/build.gradle
COPY my-android-project/app/src/main/AndroidManifest.xml app/src/main/AndroidManifest.xml
#
####
# I am unhappy with this, as the dependencies will be downloaded again
# If I change any strings, icons, or IDs inside this folder
# How do I prevent having to copy this folder?
###
#
COPY my-android-project/app/src/main/res app/src/main/res
RUN find
# Running gradle instead of gradlew to prevent redundant update of gradle version
# to save time: if you need another version of gradle, please install it in the docker image prioir to running this line
RUN gradle --no-daemon build
# Build the actual files
COPY my-android-project .
# Running gradle instead of gradlew to prevent redundant update of gradle version
# to save time: if you need another version of gradle, please install it in the docker image prioir to running this line
# --offline to make sure no further dependenies are being downloaded
RUN gradle --no-daemon --offline build

ndk-build and command not found using Eclipse mac

I am developing and android application in mac using Eclipse for android-ndk. I have already given NDK path in Eclipse. At the end when I am giving this command to build my application using android-ndk but getting the following errors:
command: ndk-build
ndk-build-bash: ndk-build: command not found
My Terminal Commands:
Muhammads-MacBook-Pro:~ UsmanKhan$ cd downloads
Muhammads-MacBook-Pro:downloads UsmanKhan$ cd 26thMarch2014
Muhammads-MacBook-Pro:26thMarch2014 UsmanKhan$ cd a2zLatest
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ ls
AndroidManifest.xml gen proguard-project.txt
Thumbs.db ic_launcher-web.png project.properties
assets libs res
bin lint.xml src
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ ls bin/classes/
com
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ ls bin/classes/com/testing/ndk
BuildConfig.class R$dimen.class R$string.class
FibLib.class R$drawable.class R$style.class
MainActivity.class R$id.class R.class
R$attr.class R$layout.class
R$color.class R$menu.class
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ mkdir jni
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ javah -jni -classpath bin /classes/ -d jni/ com.testing.ndk.FibLib
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ ls jni
com_testing_ndk_FibLib.h
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ ls jni
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ javah -jni -classpath bin/classes/ -d jni/ com.testing.ndk.FibLib
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ javah -jni -classpath bin/classes/ -d jni/ com.testing.ndk.FibLib
Muhammads-MacBook-Pro:a2zLatest UsmanKhan$ ndk-build
-bash: ndk: command not found
At the end i am getting ndk-build error.
Instead of ndk-build command, simply type ~/Desktop/AndroidNDK/android-ndk-r10d/ndk-build in your terminal.
I didn't give any explicit values for ANDROID_NDK_ROOT and ANDROID_SDK_ROOT, i just give the location of my NDK in my eclipse preferences. Can you please guide me in this thing
According to the Android folks on the Android NDK group (see Recommended NDK Directory?), uou should always provide values for ANDROID_NDK_ROOT, ANDROID_SDK_ROOT and ANDROID_SDK_HOME. Here is what they are used for:
ANDROID_NDK_ROOT → installation dirctory of NDK
ANDROID_SDK_ROOT → installation dirctory of SDK
ANDROID_SDK_HOME → location of SDK-related user files, defaults to ~/.android/ on Unix.
Put the tools on-path and export the env vars. Here's my .bash_profile for OS X.
$ cat ~/.bash_profile
export PS1="\h::\W$ "
# Android
export ANDROID_NDK_ROOT=/opt/android-ndk-r10d
export ANDROID_SDK_ROOT=/opt/android-sdk-macosx
export ANDROID_SDK_HOME=~/.android
export JAVA_HOME=`/usr/libexec/java_home`
export PATH="$ANDROID_SDK_ROOT/tools/":"$ANDROID_SDK_ROOT/platform-tools/":"$PATH"
Then, source your profile changes (or log-off and then log-in).

Install valgrind for android, C compiler doesn't work

I'm trying to configure valgrind for android on Windows 7. I'm using guide from this site, but there is a problem with C compiler while configure. I'm using cygwin.
Error:
configure:3531: error: in /cygdrive/c/Tools/valgrind-3.9.0':
configure:3533: error: C compiler cannot create executables
Script:
#!/bin/bash
export NDKROOT=C:/Tools/AndroidNDK-r6
export HWKIND=generic # A generic Android device. eg, Pandaboard
export AR=$NDKROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-ar.exe
export LD=$NDKROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-ld.exe
export CC=$NDKROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-gcc.exe
CPPFLAGS="--sysroot=$NDKROOT/platforms/android-3/arch-arm -DANDROID_HARDWARE_$HWKIND" \
CFLAGS="--sysroot=$NDKROOT/platforms/android-3/arch-arm" \
./configure --prefix=/data/local/Inst \
--host=armv7-unknown-linux --target=armv7-unknown-linux \
--with-tmpdir=/sdcard
make
make install
and config.log file
Obviously, there's a kind of a problem executing the compiler from within the NDK folder. The log file states:
configure:3422: checking for C compiler version
configure:3431: C:/Tools/AndroidNDK-r6
/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-gcc.exe
--version >&5
./configure: line 3433: C:/Tools/AndroidNDK-r6
/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-gcc.exe
: No such file or directory
So next you want to make sure that the path actually exists and the compiler executable is executable indeed. You can try yourself to type directly in cygwin shell:
C:/Tools/AndroidNDK-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/arm-linux-androideabi-gcc.exe -v
...and see what happens.

How to compile clk, the Android bootloader?

https://github.com/n0d3/lk – this is the repository holding source code for clk 1.5, which is a bootloader for Android and which I am trying to compile. Compiled packages can be found; however, I need to customize it. How can I compile it?
Running make returns the following:
$ make
makefile:22: *** No project specified. Use "make projectname" or put "PROJECT := projectname" in local.mk. Stop.
Running make htcleo returns the following:
$ make htcleo
make[1]: Entering directory '/cygdrive/c/Users/Domas/lk'
make[1]: arm-eabi-gcc: Command not found
LIBGCC =
CFLAGS = -O2 -g -fno-builtin -finline -W -Wall -Wno-multichar -Wno-unused-parameter -Wno-unused-function -include ./build-htcleo/config.h -ffunction-sections -fdata-sections -mlittle-endian -mfpu=neon -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp -mthumb-interwork -mthumb -D__thumb__
including app/aboot dev/battery dev/fbcon dev/keys lib/debug lib/devinfo lib/heap lib/libc lib/ptable
generating build-htcleo/config.h
compiling arch/arm/start.S
make[1]: arm-eabi-gcc: Command not found
arch/arm/compile.mk:30: recipe for target 'build-htcleo/arch/arm/start.o' failed
make[1]: *** [build-htcleo/arch/arm/start.o] Error 127
make[1]: Leaving directory '/cygdrive/c/Users/Domas/lk'
makefile:15: recipe for target 'make-make' failed
make: *** [make-make] Error 2
As you can see the make breaks on make[1]: arm-eabi-gcc: Command not found. I was able to compile an older version (which had a different compilation process) by using a CodeSourcery toolchain, which has arm-none-eabi-gcc.exe, but not arm-eabi-gcc.exe, which seems to be needed to compile this version.
Note:
I appreciate that this might not be a popular type of question here on SO; however, I've been banging my head on this one the whole day, and could really use some guidance.
I managed to compile it. What I did was find a git repository that had a compilation script and some additional files for compilation along side the source code, but it was the old source code. So I downloaded this project, and replaced the source code with a recent one, and the compilation script worked. Below instructions, most of it is copy paste from xda forums:
Download this zip archive here: https://github.com/cedesmith/cLK/archive/master.zip
It's the old source code, but it has the necessary scripts to make compilation easy.
Unzip the archive so that the compile script's path is ~/cLK/compile
Delete the ~/cLK/lk subdirectory (this is the old source code).
Download the new source code: git clone https://github.com/n0d3/lk
Now you should have the new source code in ~/cLK/lk. On to the actual compilation: this will depend on whether you're on Linux or Windows:
Linux
on Debian:
sudo apt-get update
sudo apt-get install gcc make git wget unzip zip libc6-dev-amd64 libc6-dev
on Fedora:
yum install gcc make git wget unzip zip
Now download the CodeSourcery toolchain:
wget http://www.codesourcery.com/sgpp/lite/arm/portal/package5353/public/arm-none-eabi/arm-2009q3-68-arm-none-eabi-i686-pc-linux-gnu.tar.bz2
tar xvfj arm-2009q3-68-arm-none-eabi-i686-pc-linux-gnu.tar.bz2
export PATH=$HOME/clk/arm-2009q3/bin:$PATH
Finally, compile:
cd ~/cLK
./compile clean
If everything goes well, you'll find the RUU_signed in this folder: bin/RUU_signed.nbh
Windows
You'll need Cygwin:
Download, the following file, and execute it http://www.cygwin.com/setup.exe
Click Next
Click "Install from internet" and click Next
Click Next (to accept the default directory to install)
Click Next (to accept the default directory for local Package directory)
Click "Direct Connection", and click Next
Select a "local mirror", and click Next
Now in the search bar, you need to search for the following things to install
gcc
make
git
wget
zip
unzip
Once selected click Next to continue, and Finish
Fire up the cygwin terminal (mintty); the following commands probably don't work in cmd.exe.
Now download CodeSourcery toolchain and yang:
wget http://www.codesourcery.com/sgpp/lite/arm/portal/package5354/public/arm-none-eabi/arm-2009q3-68-arm-none-eabi-i686-mingw32.tar.bz2
tar xvfj arm-2009q3-68-arm-none-eabi-i686-mingw32.tar.bz2
wget http://htc-flasher.googlecode.com/files/yang-1.1_win32.zip
cd cLK/bin
rm -rf yang
mkdir yang
unzip ../../yang-1.1_win32.zip yang/yang.exe
mv yang/yang.exe .
cd ..
sed -i s/yang/yang.exe/g compile
And finally, compile:
export PATH=$HOME/arm-2009q3/bin:$PATH
cd ~/cLK
./compile clean
Now your RUU_signed.nbh will be located in the following folder bin/RUU_signed.nbh

Build script for Android NDK in linux

How can I build my source for android with NDK.
Already I download ndk and I don't know how can I set arch compiler path.
This is my "build-for-android.sh" code:
#!/bin/sh
cairo=system
curl=system
pal=android
alsa=no
pulse=no
sles=yes
ffmpeg=yes
gallium=no
egl=yes
glx=no
PKG_CONFIG_PATH=$MOONLIGHT_PREFIX/lib/pkgconfig
if [ -d /usr/X11/share/aclocal ]; then
export ACLOCAL_FLAGS="-I /usr/X11/share/aclocal"
fi
./autogen.sh --host=arm-linux-androideabi --prefix=$MOONLIGHT_PREFIX --with-manual-mono=yes --with-testing=no --enable-browser-support --disable-desktop-support --with-unwind=no --with-ffmpeg=$ffmpeg --with-alsa=$alsa --with-pulseaudio=$pulse --with-opensles=$sles --with-pal=$pal --with-curl=$curl --with-cairo=$cairo --with-gallium-path=$gallium --enable-sdk=no --with-egl=$egl --with-glx=$glx CFLAGS="-DPLATFORM_ANDROID -I$MOONLIGHT_PREFIX/include $CFLAGS" LDFLAGS="-L$MOONLIGHT_PREFIX/lib $LDFLAGS" CXXFLAGS="-fno-rtti -DPLATFORM_ANDROID -I$MOONLIGHT_PREFIX/include $CFLAGS"
make $#
when I run it I get error below:
checking for arm-linu-androideabi-g++... no
checking for arm-linu-androideabi-c++... no
checking for arm-linu-androideabi-gpp... no
checking for arm-linu-androideabi-aCC... no
checking for arm-linu-androideabi-cc... no
.
.
.
.
I can see them in ~/Android/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuild/linux-x86/bin/* directory.
OK. I found it.:
First, a standalone toolchain is created to make the configure script easier to use
~$ android-ndk-r5b/build/tools/make-standalone-toolchain.sh --platform=android-8 --install-dir=android-8-toolchain
Next, the toolchain's bin directory is added to the PATH.
~$ export PATH=$PATH:~/android-8-toolchain/bin/

Categories

Resources