Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 05baea5

Browse files
committed
[android] move build to Android Studio
1 parent f7a4a44 commit 05baea5

File tree

13 files changed

+211
-315
lines changed

13 files changed

+211
-315
lines changed

Makefile

+92-97
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ else
66
$(error BUILDTYPE must be Debug or Release)
77
endif
88

9+
buildtype := $(shell echo "$(BUILDTYPE)" | tr "[A-Z]" "[a-z]")
10+
911
ifeq ($(shell uname -s), Darwin)
1012
HOST_PLATFORM = macos
1113
HOST_PLATFORM_VERSION = $(shell uname -m)
@@ -450,39 +452,36 @@ test-node: node
450452

451453
#### Android targets ###########################################################
452454

453-
MBGL_ANDROID_ABIS = arm-v5;armeabi;9
454-
MBGL_ANDROID_ABIS += arm-v7;armeabi-v7a;9
455-
MBGL_ANDROID_ABIS += arm-v8;arm64-v8a;21
456-
MBGL_ANDROID_ABIS += x86;x86;9
457-
MBGL_ANDROID_ABIS += x86-64;x86_64;21
458-
MBGL_ANDROID_ABIS += mips;mips;9
455+
MBGL_ANDROID_ABIS = arm-v5;armeabi
456+
MBGL_ANDROID_ABIS += arm-v7;armeabi-v7a
457+
MBGL_ANDROID_ABIS += arm-v8;arm64-v8a
458+
MBGL_ANDROID_ABIS += x86;x86
459+
MBGL_ANDROID_ABIS += x86-64;x86_64
460+
MBGL_ANDROID_ABIS += mips;mips
459461

460-
MBGL_ANDROID_BUILD_DIR = build/android-$1-$3/$(BUILDTYPE)
461462
MBGL_ANDROID_LOCAL_WORK_DIR = /data/local/tmp/core-tests
462463
MBGL_ANDROID_LIBDIR = lib$(if $(filter arm-v8 x86-64,$1),64)
463464
MBGL_ANDROID_DALVIKVM = dalvikvm$(if $(filter arm-v8 x86-64,$1),64,32)
464465
MBGL_ANDROID_APK_SUFFIX = $(if $(filter Release,$(BUILDTYPE)),release-unsigned,debug)
465-
MBGL_ANDROID_CORE_TEST_DIR = $(MBGL_ANDROID_BUILD_DIR)/core-tests
466+
MBGL_ANDROID_CORE_TEST_DIR = platform/android/MapboxGLAndroidSDK/.externalNativeBuild/cmake/$(buildtype)/$2/core-tests
467+
MBGL_ANDROID_GRADLE = ./gradlew --parallel --max-workers=$(JOBS) -Pmapbox.buildtype=$(buildtype)
468+
469+
# Lists all devices, and extracts the identifiers, then obtains the ABI for every one.
470+
# Some devices return \r\n, so we'll have to remove the carriage return before concatenating.
471+
MBGL_ANDROID_ACTIVE_ARCHS = $(shell adb devices | sed '1d;/^\*/d;s/[[:space:]].*//' | xargs -n 1 -I DEV `type -P adb` -s DEV shell getprop ro.product.cpu.abi | tr -d '\r')
466472

467473
.PHONY: android-help
468474
android-help:
469475
@echo
470476
@echo "Available Android architecture targets:"
471477
@echo
472-
@echo " make android-arm-v5-9"
473-
@echo " (android-arm-v5)"
474-
@echo " make android-arm-v7-9"
475-
@echo " (android, android-arm-v7)"
476-
@echo " make android-arm-v8-21"
477-
@echo " (android-arm-v8)"
478-
@echo " make android-mips-9"
479-
@echo " (android-mips)"
480-
@echo " make android-mips-64-21"
481-
@echo " (android-mips-64)"
482-
@echo " make android-x86-9"
483-
@echo " (android-x86)"
484-
@echo " make android-x86-64-21"
485-
@echo " (android-x86-64)"
478+
@echo " make android-arm-v5"
479+
@echo " make android-arm-v7, make android"
480+
@echo " make android-arm-v8"
481+
@echo " make android-mips"
482+
@echo " make android-mips-64"
483+
@echo " make android-x86"
484+
@echo " make android-x86-64"
486485
@echo " make apackage"
487486
@echo
488487

@@ -491,50 +490,30 @@ android-style-code:
491490
node platform/android/scripts/generate-style-code.js
492491
style-code: android-style-code
493492

493+
# Configuration file for running CMake from Gradle within Android Studio.
494+
platform/android/configuration.gradle:
495+
@echo "ext {\n node = '`which node`'\n npm = '`which npm`'\n ccache = '`which ccache`'\n}" > $@
496+
494497
define ANDROID_RULES
495498
# $1 = arm-v7 (short arch)
496499
# $2 = armeabi-v7a (internal arch)
497-
# $3 = 9 (platform version)
498-
499-
$(MBGL_ANDROID_BUILD_DIR)/env.sh: $(BUILD_DEPS) platform/android/scripts/ndk.sh
500-
@mkdir -p $(MBGL_ANDROID_BUILD_DIR)
501-
platform/android/scripts/ndk.sh $1 $2 $3 > $(MBGL_ANDROID_BUILD_DIR)/env.sh.tmp && \
502-
mv $(MBGL_ANDROID_BUILD_DIR)/env.sh.tmp $(MBGL_ANDROID_BUILD_DIR)/env.sh
503-
504-
$(MBGL_ANDROID_BUILD_DIR)/build.ninja: $(MBGL_ANDROID_BUILD_DIR)/env.sh platform/android/config.cmake
505-
# Invoke CMake twice to fix issues from double inclusion of toolchain.cmake on the first run.
506-
. $(MBGL_ANDROID_BUILD_DIR)/env.sh && \
507-
([ -f $(MBGL_ANDROID_BUILD_DIR)/build.ninja ] || $$$${CMAKE} \
508-
-H. \
509-
-B"$(MBGL_ANDROID_BUILD_DIR)" \
510-
-G"$$$${CMAKE_GENERATOR}" \
511-
$$$${CMAKE_ARGS} \
512-
-DCMAKE_BUILD_TYPE=$(BUILDTYPE) \
513-
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
514-
-DMBGL_PLATFORM=android \
515-
-DMASON_PLATFORM=android) && \
516-
$$$${CMAKE} \
517-
-H. \
518-
-B"$(MBGL_ANDROID_BUILD_DIR)"
519500

520501
.PHONY: android-test-lib-$1
521-
android-test-lib-$1: $(MBGL_ANDROID_BUILD_DIR)/build.ninja
522-
. $(MBGL_ANDROID_BUILD_DIR)/env.sh && $$$${CMAKE} --build $(MBGL_ANDROID_BUILD_DIR) -- $(NINJA_ARGS) -j$(JOBS) mbgl-test
502+
android-test-lib-$1: platform/android/configuration.gradle
503+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=$2 -Pmapbox.with_test=true :MapboxGLAndroidSDKTestApp:assemble$(BUILDTYPE)
523504

524505
.PHONY: android-lib-$1
525-
android-lib-$1: $(MBGL_ANDROID_BUILD_DIR)/build.ninja
526-
. $(MBGL_ANDROID_BUILD_DIR)/env.sh && $$$${CMAKE} --build $(MBGL_ANDROID_BUILD_DIR) -- $(NINJA_ARGS) -j$(JOBS) mapbox-gl example-custom-layer
506+
android-lib-$1: platform/android/configuration.gradle
507+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=$2 :MapboxGLAndroidSDK:assemble$(BUILDTYPE)
527508

528509
.PHONY: android-$1
529-
android-$1: android-lib-$1
530-
cd platform/android && ./gradlew --parallel --max-workers=$(JOBS) :MapboxGLAndroidSDKTestApp:assemble$(BUILDTYPE)
510+
android-$1: platform/android/configuration.gradle
511+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=$2 :MapboxGLAndroidSDKTestApp:assemble$(BUILDTYPE)
531512

532513
.PHONY: android-core-test-$1
533514
android-core-test-$1: android-test-lib-$1
534-
mkdir -p $(MBGL_ANDROID_CORE_TEST_DIR)
535-
536515
# Compile main sources and extract the classes (using the test app to get all transitive dependencies in one place)
537-
cd platform/android && ./gradlew :MapboxGLAndroidSDKTestApp:assemble$(BUILDTYPE)
516+
mkdir -p $(MBGL_ANDROID_CORE_TEST_DIR)
538517
unzip -o platform/android/MapboxGLAndroidSDKTestApp/build/outputs/apk/MapboxGLAndroidSDKTestApp-$(MBGL_ANDROID_APK_SUFFIX).apk classes.dex -d $(MBGL_ANDROID_CORE_TEST_DIR)
539518

540519
# Compile Test runner
@@ -555,8 +534,8 @@ run-android-core-test-$1-%: android-core-test-$1
555534
# Push all needed files to the device
556535
adb push $(MBGL_ANDROID_CORE_TEST_DIR)/test.jar $(MBGL_ANDROID_LOCAL_WORK_DIR) > /dev/null 2>&1
557536
adb push test/fixtures $(MBGL_ANDROID_LOCAL_WORK_DIR)/test > /dev/null 2>&1
558-
adb push $(MBGL_ANDROID_BUILD_DIR)/stripped/libmapbox-gl.so $(MBGL_ANDROID_LOCAL_WORK_DIR) > /dev/null 2>&1
559-
adb push $(MBGL_ANDROID_BUILD_DIR)/stripped/libmbgl-test.so $(MBGL_ANDROID_LOCAL_WORK_DIR) > /dev/null 2>&1
537+
adb push platform/android/MapboxGLAndroidSDK/build/intermediates/bundles/default/jni/$2/libmapbox-gl.so $(MBGL_ANDROID_LOCAL_WORK_DIR) > /dev/null 2>&1
538+
adb push platform/android/MapboxGLAndroidSDK/build/intermediates/bundles/default/jni/$2/libmbgl-test.so $(MBGL_ANDROID_LOCAL_WORK_DIR) > /dev/null 2>&1
560539

561540
# Kick off the tests
562541
adb shell "export LD_LIBRARY_PATH=/system/$(MBGL_ANDROID_LIBDIR):$(MBGL_ANDROID_LOCAL_WORK_DIR) && cd $(MBGL_ANDROID_LOCAL_WORK_DIR) && $(MBGL_ANDROID_DALVIKVM) -cp $(MBGL_ANDROID_LOCAL_WORK_DIR)/test.jar Main --gtest_filter=$$*"
@@ -571,84 +550,100 @@ run-android-core-test-$1-%: android-core-test-$1
571550
run-android-core-test-$1: run-android-core-test-$1-*
572551

573552
.PHONY: run-android-$1
574-
run-android-$1: android-$1
553+
run-android-$1: platform/android/configuration.gradle
575554
adb uninstall com.mapbox.mapboxsdk.testapp > /dev/null
576-
cd platform/android && ./gradlew :MapboxGLAndroidSDKTestApp:install$(BUILDTYPE) && adb shell am start -n com.mapbox.mapboxsdk.testapp/.activity.FeatureOverviewActivity
555+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=$2 :MapboxGLAndroidSDKTestApp:install$(BUILDTYPE) && adb shell am start -n com.mapbox.mapboxsdk.testapp/.activity.FeatureOverviewActivity
556+
557+
.PHONY: android-ui-test-$1
558+
android-ui-test-$1: platform/android/configuration.gradle
559+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=$2 :MapboxGLAndroidSDKTestApp:assembleDebug :MapboxGLAndroidSDKTestApp:assembleAndroidTest
560+
561+
# This test assumes that you have Android Simulator started locally.
562+
.PHONY: run-android-ui-test-$1
563+
run-android-ui-test-$1: platform/android/configuration.gradle
564+
adb uninstall com.mapbox.mapboxsdk.testapp > /dev/null
565+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=$2 :MapboxGLAndroidSDKTestApp:connectedAndroidTest
566+
567+
run-android-ui-test-$1-%: platform/android/configuration.gradle
568+
adb uninstall com.mapbox.mapboxsdk.testapp > /dev/null
569+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=$2 :MapboxGLAndroidSDKTestApp:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class="$*"
577570

578-
apackage: android-lib-$1
579571
endef
580572

581573
# Explodes the arguments into individual variables
582574
define ANDROID_RULES_INVOKER
583-
$(call ANDROID_RULES,$(word 1,$1),$(word 2,$1),$(word 3,$1))
575+
$(call ANDROID_RULES,$(word 1,$1),$(word 2,$1))
584576
endef
585577

586578
$(foreach abi,$(MBGL_ANDROID_ABIS),$(eval $(call ANDROID_RULES_INVOKER,$(subst ;, ,$(abi)))))
587579

588580
.PHONY: android
589581
android: android-arm-v7
590582

583+
.PHONY: android-lib
584+
android-lib: android-lib-arm-v7
585+
591586
.PHONY: run-android
592587
run-android: run-android-arm-v7
593588

594-
.PHONY: run-android-unit-test
595-
run-android-unit-test:
596-
cd platform/android && ./gradlew :MapboxGLAndroidSDKTestApp:testDebugUnitTest --continue
589+
.PHONY: run-android-ui-test
590+
run-android-ui-test: run-android-ui-test-arm-v7
591+
run-android-ui-test-%: run-android-ui-test-arm-v7-%
597592

598-
run-android-unit-test-%:
599-
cd platform/android && ./gradlew :MapboxGLAndroidSDKTestApp:testDebugUnitTest --tests "$*"
593+
# Java-only test
594+
.PHONY: run-android-unit-test
595+
run-android-unit-test: platform/android/configuration.gradle
596+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=none :MapboxGLAndroidSDKTestApp:testDebugUnitTest
597+
run-android-unit-test-%: platform/android/configuration.gradle
598+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=none :MapboxGLAndroidSDKTestApp:testDebugUnitTest --tests "$*"
600599

600+
# Java-only test
601601
.PHONY: run-android-wear-unit-test
602-
run-android-wear-unit-test:
603-
cd platform/android && ./gradlew :MapboxGLAndroidSDKWearTestApp:testDebugUnitTest --continue
602+
run-android-wear-unit-test: platform/android/configuration.gradle
603+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=none :MapboxGLAndroidSDKWearTestApp:testDebugUnitTest
604+
run-android-wear-unit-test-%: platform/android/configuration.gradle
605+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=none :MapboxGLAndroidSDKWearTestApp:testDebugUnitTest --tests "$*"
604606

605-
.PHONY: android-ui-test
606-
android-ui-test:
607-
cd platform/android && ./gradlew :MapboxGLAndroidSDKTestApp:assembleDebug --continue && ./gradlew :MapboxGLAndroidSDKTestApp:assembleAndroidTest --continue
608-
609-
.PHONY: run-android-ui-test
610-
run-android-ui-test:
611-
adb uninstall com.mapbox.mapboxsdk.testapp > /dev/null
612-
cd platform/android && ./gradlew :MapboxGLAndroidSDKTestApp:connectedAndroidTest -i
607+
.PHONY: run-android-ui-test-aws
608+
run-android-ui-test-aws: platform/android/configuration.gradle
609+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=all devicefarmUpload
613610

614-
run-android-ui-test-%:
615-
adb uninstall com.mapbox.mapboxsdk.testapp > /dev/null
616-
cd platform/android && ./gradlew :MapboxGLAndroidSDKTestApp:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class="$*"
611+
.PHONY: apackage
612+
apackage: platform/android/configuration.gradle
613+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=all assemble$(BUILDTYPE)
617614

618-
.PHONY: run-android-ui-test-aws
619-
run-android-ui-test-aws:
620-
cd platform/android && ./gradlew devicefarmUpload
615+
# Uploads the compiled Android SDK to Maven
616+
.PHONY: run-android-upload-archives
617+
run-android-upload-archives: platform/android/configuration.gradle
618+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=all :MapboxGLAndroidSDK:uploadArchives
621619

620+
# Runs Android UI tests on all connected devices using Spoon
622621
.PHONY: run-android-ui-test-spoon
623-
run-android-ui-test-spoon:
624-
cd platform/android && ./gradlew spoon
625-
626-
.PHONY: apackage
627-
apackage:
628-
cd platform/android && ./gradlew --parallel --max-workers=$(JOBS) assemble$(BUILDTYPE)
622+
run-android-ui-test-spoon: platform/android/configuration.gradle
623+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis="$(MBGL_ANDROID_ACTIVE_ARCHS)" spoon
629624

630625
.PHONY: test-code-android
631626
test-code-android:
632627
node platform/android/scripts/generate-test-code.js
633628

634-
.PHONY: android-ndk-stack
635-
android-ndk-stack:
636-
adb logcat | ndk-stack -sym build/android-arm-v7-9/Debug
637-
638629
.PHONY: android-checkstyle
639-
android-checkstyle:
640-
cd platform/android && ./gradlew checkstyle
630+
android-checkstyle: platform/android/configuration.gradle
631+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=none checkstyle
641632

642633
.PHONY: android-javadoc
643-
android-javadoc:
644-
cd platform/android && ./gradlew :MapboxGLAndroidSDK:javadocrelease
634+
android-javadoc: platform/android/configuration.gradle
635+
cd platform/android && $(MBGL_ANDROID_GRADLE) -Pmapbox.abis=none :MapboxGLAndroidSDK:javadocrelease
645636

646637
ifeq ($(HOST_PLATFORM), macos)
647638
.PHONY: aproj
648-
aproj:
639+
aproj: platform/android/configuration.gradle
649640
open -b com.google.android.studio platform/android
650641
endif
651642

643+
.PHONY: android-configuration
644+
android-configuration: platform/android/configuration.gradle
645+
cat platform/android/configuration.gradle
646+
652647
#### Miscellaneous targets #####################################################
653648

654649
.PHONY: style-code
@@ -663,12 +658,12 @@ codestyle:
663658
.PHONY: clean
664659
clean:
665660
-rm -rf ./build \
661+
./platform/android/configuration.gradle \
666662
./platform/android/MapboxGLAndroidSDK/build \
663+
./platform/android/MapboxGLAndroidSDK/.externalNativeBuild \
667664
./platform/android/MapboxGLAndroidSDKTestApp/build \
668665
./platform/android/MapboxGLAndroidSDKWearTestApp/build \
669666
./platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/gen \
670-
./platform/android/MapboxGLAndroidSDK/src/main/jniLibs \
671-
./platform/android/MapboxGLAndroidSDKTestApp/src/main/jniLibs \
672667
./platform/android/MapboxGLAndroidSDK/src/main/assets
673668

674669
.PHONY: distclean

platform/android/.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77

88
# Build files
99
build/
10+
.externalNativeBuild
1011
*.so
1112
*.apk
1213

13-
# JNI
14-
MapboxGLAndroidSDK/src/main/jniLibs/
15-
1614
# Lib assets
1715
MapboxGLAndroidSDK/src/main/assets/
1816

1917
# Local settings
2018
local.properties
19+
/configuration.gradle
2120

2221
# Token file
2322
MapboxGLAndroidSDKTestApp/src/main/res/values/developer-config.xml

platform/android/MapboxGLAndroidSDK/build.gradle

+60
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,66 @@ android {
2929
buildConfigField "String", "GIT_REVISION_SHORT", String.format("\"%s\"", getGitRevision())
3030
}
3131

32+
defaultPublishConfig project.hasProperty("mapbox.buildtype") ? project.getProperty("mapbox.buildtype") : "debug"
33+
34+
// We sometimes want to invoke Gradle without building a native dependency, e.g. when we just want
35+
// to invoke the Java tests. When we explicitly specify an ABI of 'none', no native dependencies are
36+
// added. When another ABI is specified explicitly, we're just going to build that ABI. In all other
37+
// cases, all ABIs are built.
38+
// When invoking from the command line, set `-Pmapbox.abis=...` to only build the desired architectures.
39+
// When building from Android Studio, gradle.properties sets `android.buildOnlyTargetAbi=true` so that
40+
// only the architecture for the device you're running on gets built.
41+
def abi = 'all'
42+
if (!project.hasProperty('android.injected.invoked.from.ide')) {
43+
// Errors when the user invokes Gradle from the command line and didn't set mapbox.abis
44+
abi = project.getProperty("mapbox.abis")
45+
}
46+
47+
if (abi != 'none') {
48+
externalNativeBuild {
49+
cmake {
50+
path "../../../CMakeLists.txt"
51+
}
52+
}
53+
}
54+
55+
defaultConfig {
56+
if (abi != 'none') {
57+
externalNativeBuild {
58+
cmake {
59+
arguments "-DANDROID_TOOLCHAIN=clang"
60+
arguments "-DANDROID_STL=c++_static"
61+
arguments "-DANDROID_CPP_FEATURES=rtti;exceptions"
62+
arguments "-DMBGL_PLATFORM=android"
63+
arguments "-DMASON_PLATFORM=android"
64+
arguments "-DNodeJS_EXECUTABLE=" + rootProject.ext.node
65+
arguments "-Dnpm_EXECUTABLE=" + rootProject.ext.npm
66+
67+
// Enable ccache if the user has installed it.
68+
if (rootProject.ext.ccache?.trim()) {
69+
arguments "-DANDROID_CCACHE=" + rootProject.ext.ccache
70+
// ccache splits up the compile command until multiple invocations and uses -E
71+
// with one of them, and clang doesn't like unused arguments in that case.
72+
cFlags "-Qunused-arguments"
73+
cppFlags "-Qunused-arguments"
74+
}
75+
76+
targets "mapbox-gl"
77+
targets "example-custom-layer"
78+
if (project.hasProperty("mapbox.with_test")) {
79+
targets "mbgl-test"
80+
}
81+
82+
if (abi != 'all') {
83+
abiFilters abi.split(' ')
84+
} else {
85+
abiFilters "armeabi", "armeabi-v7a", "mips", "x86", "arm64-v8a", "x86_64"
86+
}
87+
}
88+
}
89+
}
90+
}
91+
3292
// avoid naming conflicts, force usage of prefix
3393
resourcePrefix 'mapbox_'
3494

platform/android/MapboxGLAndroidSDK/gradle.properties

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ POM_DEVELOPER_NAME=Mapbox
1414
POM_NAME=Mapbox Android SDK
1515
POM_ARTIFACT_ID=mapbox-android-sdk
1616
POM_PACKAGING=aar
17+
18+
# Only build native dependencies for the current ABI
19+
# See https://code.google.com/p/android/issues/detail?id=221098#c20
20+
android.buildOnlyTargetAbi=true

0 commit comments

Comments
 (0)