Android Data Binding in Make file (Android.mk) - android

As per a lot of examples, android data bindings are enabled by putting the following snippet in build.gradle file:
android {
....
dataBinding {
enabled = true
}
}
However, I'm working on an android project in AOSP which builds using the make file (Android.mk).
So my question is: How can we add support for data-binding dependencies in android make file?

I found a solution that works for me!
The first hurdle to climb will look something like this: error: package net.mulliken.pinenotenotebook.databinding does not exist
I found that Android Studio automatically generates these files. It was in app/build/generated/data_binding_base_class_source_out/debug/out/net/mulliken/pinenotenotebook/databinding. In order to incorporate this in my build I made a symbolic link from my Android studio workspace to databinding_src in my packages folder.
After that it still didn't work because it could not find the view binding package. You will probably get an error like this: error: package androidx.viewbinding does not exist
I found that google has a repo that includes this package and so I cloned it into my AOSP workspace under frameworks.
[me aosp/frameworks] $ git clone -b studio-main https://android.googlesource.com/platform/frameworks/data-binding data-binding
I then created a new symbolic link from that path into my package directory so that the compiler could find that class:
[me packages/apps/MyAPP] $ ln -s ../../../../frameworks/data-binding/extensions/viewbinding/src/main/java/ androidx_viewbinding_src
At the end of the day my Android.bp file looks like this:
android_app {
name: "PineNoteNotebook",
static_libs: [
"androidx.appcompat_appcompat",
"com.google.android.material_material",
"androidx-constraintlayout_constraintlayout",
"androidx.navigation_navigation-fragment",
"androidx.navigation_navigation-ui",
],
certificate: "platform",
srcs: [
"./**/*.java",
],
resource_dirs: ["res"],
product_specific: true,
sdk_version: "current",
optimize: {
enabled: false
},
required: ["libpinenote"],
}
And my package tree looks like this:
.
├── Android.bp
├── AndroidManifest.xml -> /home/mulliken/AndroidStudioProjects/PineNoteNotebook/app/src/main/AndroidManifest.xml
├── androidx_viewbinding_src -> ../../../../frameworks/data-binding/extensions/viewbinding/src/main/java/
├── databinding_src -> /home/mulliken/AndroidStudioProjects/PineNoteNotebook/app/build/generated/data_binding_base_class_source_out/debug/out
├── res -> /home/mulliken/AndroidStudioProjects/PineNoteNotebook/app/src/main/res/
└── src -> /home/mulliken/AndroidStudioProjects/PineNoteNotebook/app/src/main/java/

i think you must use the latest android studio 3.6 version , with the latest ndk +
add this to your android gradle
android {
...
viewBinding {
enabled = true
}
}
check this : https://developer.android.com/topic/libraries/view-binding

Related

How to add two or more kotlin native modules on an iOS project

TL;DR;
How to add two or more kotlin native modules on an iOS project without getting duplicate symbols error?
The detailed question
Let's assume a multi-module KMP project as a follow where there exists a native app for Android and a native app for iOS and two common modules to hold shared kotlin code.
.
├── android
│ └── app
├── common
│ ├── moduleA
│ └── moduleB
├── ios
│ └── app
The module A contains a data class HelloWorld and has no module dependencies:
package hello.world.modulea
data class HelloWorld(
val message: String
)
Module B contains an extension function for HelloWorld class so it depends on module A:
package hello.world.moduleb
import hello.world.modulea.HelloWorld
fun HelloWorld.egassem() = message.reversed()
The build.gradle configuration of the modules are:
Module A
apply plugin: "org.jetbrains.kotlin.multiplatform"
apply plugin: "org.jetbrains.kotlin.native.cocoapods"
…
kotlin {
targets {
jvm("android")
def iosClosure = {
binaries {
framework("moduleA")
}
}
if (System.getenv("SDK_NAME")?.startsWith("iphoneos")) {…}
}
cocoapods {…}
sourceSets {
commonMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:1.3.72"
}
androidMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.72"
}
iosMain.dependencies {
}
}
}
Module B
apply plugin: "org.jetbrains.kotlin.multiplatform"
apply plugin: "org.jetbrains.kotlin.native.cocoapods"
…
kotlin {
targets {
jvm("android")
def iosClosure = {
binaries {
framework("moduleB")
}
}
if (System.getenv("SDK_NAME")?.startsWith("iphoneos")) {…}
}
cocoapods {…}
sourceSets {
commonMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:1.3.72"
implementation project(":common:moduleA")
}
androidMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.72"
}
iosMain.dependencies {
}
}
}
It looks pretty straightforward and it even works on android if I configure the android build gradle dependencies as a following:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72"
implementation project(":common:moduleA")
implementation project(":common:moduleB")
}
However, this does not seem to be the correct way to organize multi modules on iOS, because running the ./gradlew podspec I get a BUILD SUCCESSFUL as expected with the following pods:
pod 'moduleA', :path => '…/HelloWorld/common/moduleA'
pod 'moduleB', :path => '…/HelloWorld/common/moduleB'
Even running a pod install I get a success output Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed. whats looks correctly once the Xcode shows the module A and module B on the Pods section.
However, if I try to build the iOS project I get the following error:
Ld …/Hello_World-…/Build/Products/Debug-iphonesimulator/Hello\ World.app/Hello\ World normal x86_64 (in target 'Hello World' from project 'Hello World')
cd …/HelloWorld/ios/app
…
duplicate symbol '_ktypew:kotlin.Any' in:
…/HelloWorld/common/moduleA/build/cocoapods/framework/moduleA.framework/moduleA(result.o)
…/HelloWorld/common/moduleB/build/cocoapods/framework/moduleB.framework/moduleB(result.o)
… a lot of duplicate symbol more …
duplicate symbol '_kfun:kotlin.throwOnFailure$stdlib#kotlin.Result<#STAR>.()' in:
…/HelloWorld/common/moduleA/build/cocoapods/framework/moduleA.framework/moduleA(result.o)
…/HelloWorld/common/moduleB/build/cocoapods/framework/moduleB.framework/moduleB(result.o)
ld: 9928 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My knowledge in iOS is not that much, so to my untrained eyes, it looks like each module is adding its own version of the things instead of using some resolutions strategy to share it.
If I use only the module A the code works and run as expected, so I know the code itself is correct, the problem is how to manage more than 1 module, so that the question, how to add both (module A and module B) on iOS and make things works?
P.S
I did reduce the code as much as I could, trying to keep only the parts that I guess is the source of the problem, however, the complete code is available here if you want to check anything missing in the snippets, or if you want to run and try to solve the problem…
Multiple Kotlin frameworks can be tricky, but should be working as of 1.3.70 which I see you have.
The issue seems to be that both frameworks are static, which is currently an issue in 1.3.70 so it isn't working. (This should be updated by 1.40). It looks like by default the cocoapods plugin sets the frameworks to be static which won't work. I'm unaware of a way to change cocoapods to set it as dynamic but I've tested building without cocoapods and using the isStatic variable in a gradle task, and have gotten an iOS project to compile. Something like:
binaries {
framework("moduleA"){
isStatic = false
}
}
For now you can work around the issue using this method by using the code above and creating a task to build the frameworks(here's an example)
Another thing worth noting is that on the iOS side, the HelloWorld classes will appear as two separate classes despite both coming from moduleA. It's another strange situation with multiple Kotlin frameworks, but I think the extension will still work in this case since you're returning a string.
I actually just wrote up a blog post about multiple Kotlin frameworks that may help with some other questions if you'd like to take a look. https://touchlab.co/multiple-kotlin-frameworks-in-application/
EDIT: Looks like cocoapodsext also has an isStatic variable, so set it to isStatic = false
tl:dr You currently can't have more than one static Kotlin frameworks in the same iOS project. Set them to not be static using isStatic = false.
However, if I try to build the iOS project I get the following error:
This particular error is a known issue. Multiple debug static frameworks are incompatible with compiler caches.
So to workaround the issue you can either disable compiler caches by putting the following line into your gradle.properties:
kotlin.native.cacheKind=none
or make the frameworks dynamic by adding the following snippet to your Gradle build script:
kotlin {
targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
binaries.withType<org.jetbrains.kotlin.gradle.plugin.mpp.Framework> {
isStatic = false
}
}
}
See https://youtrack.jetbrains.com/issue/KT-42254 for more details.
I guess current behaviour for multiple frameworks doesn't make much sense for the original topic starter, I'm just putting my answer here for anyone who might encounter the same issue.
My knowledge in iOS is not that much, so to my untrained eyes, it looks like each module is adding its own version of the things instead of using some resolutions strategy to share it.
This is exactly how it is supposed to work at this moment. But "versions of the things" in each of the frameworks are put into the separate independent namespaces, so there should be no linkage errors, and the one you've encountered is a bug.

How to import OpenCV android-sdk to my project which is using Bazel?

I have been trying to import opencv-android-sdk to my Bazel project but I am not able to do it.
I tried the this answer on SO but while building my project I get errors that
error: package org.opencv.android does not exist
I see that there's an opencv-android artifact on Maven.
You can depend on this using rules_jvm_external.
In your WORKSPACE file, specify the dependency along with the other external dependencies:
load("#rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
artifacts = [
"org.opencv:opencv-android:1.0.1",
# ...
],
repositories = [
"https://maven.google.com",
"https://jcenter.bintray.com",
],
)
Then, in your BUILD file containing your Android targets, depend on the OpenCV target:
android_library(
name = "my_lib",
custom_package = "com.example.bazel",
srcs = glob(["java/com/example/bazel/*.java"]),
manifest = "java/AndroidManifest.xml",
resource_files = glob(["res/**"]),
deps = [
"#maven//:org_opencv_opencv_android",
],
visibility = ["//src/test:__subpackages__"]
)
Finally, you should be able to reference classes like org.opencv.core.Core in your Android Java code.
P.S. consider switching all your maven_jar and gmaven_rules/gmaven_artifact to use rules_jvm_external. The former Maven rules have been deprecated in favor of rules_jvm_external.

Code-behind of my custom component is not running

The code-behind of my custom component is not being executed when bundling the app with WebPack (tns run ios --bundle --env.snapshot --env.aot). I followed the official documentation but I couldn't get it to work.
Can anyone help me, please?
1 Example project: https://github.com/felipebueno/my-app
2 Custom components documentation: https://docs.nativescript.org/ui/basics#xml-based-custom-component-with-a-code-file
Screenshots for reference:
With webpack (example project. [Update] The name of the component is different but it's being imported properly):
Without webpack:
The thing is that the default webpack configuration (from nativescript-webpack here) will bundle only pages which are named to end with -page or -root. All other custom named pages and resources should be included explicitly n your webpack.config.js in the CopyWebpackPlugin array.
So as a solution rename your custom component name to end with -name postfix and make a clean rebuild. For example, in your case change the file name to profile-icon-page (and use the proper import)
Or add the custom resources as shown below in webpack.config.js file
new CopyWebpackPlugin([
{ from: "shared/components/**" }, // HERE
{ from: "fonts/**" },
{ from: "**/*.jpg" },
{ from: "**/*.png" },
]

tensorflow android with react-native can't work normally

I put react-native.jar to tensorflow android's directory and add configuration to tensorflow/examples/android/BUILD file like this:
java_import(
name = "react-native",
jars = [
"react-native-0.39.2-sources.jar",
],
)
Now run bazel build //tensorflow/examples/android:tensorflow_demo work well. But then use import com.facebook.react.*; in java file and run bazel build ... again, it throws a nonexistent error, can't find com.facebook.react.* package.
In order to expose the classes in the JAR to the Java code in the Android build, you need to add a dependency in the android_binary that you are building on the java_import you have created.
For example:
# tensorflow/examples/android/BUILD
java_import(
name = "react-native",
jars = [
"react-native-0.39.2-sources.jar",
],
)
android_binary(
name = "tensorflow_demo",
srcs = glob(["src/**/*.java"]),
deps = [
":tensorflow_native_libs",
"//tensorflow/contrib/android:android_tensorflow_inference_java",
":react-native",
],
)

Commiting google-services.json to GitHub

I am creating a public android project and I am using Google Sign-In service. I am doing it according to this tutorial. As it says, I have got the google-services.json file.
Do I need to commit the above file to Github?
Do other developers (If someone contributes) need this file?
Or, do they have to create there own?
By the way I am using Travis-CI. Will this file affect to CI build?
You can create a new build variant and store a template google-services.json to be used for your build on your CI platform in your app build.gradle.
Use a different google-services.json for the new dev build variant (see this post). Add the following google-services.json template to app/src/dev folder :
{
"project_info": {
"project_number": "",
"project_id": ""
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:123456789012:android:1234567890123456",
"android_client_info": {
"package_name": "com.your.package"
}
},
"oauth_client": [
{
"client_id": "",
"client_type": 3
},
{
"client_id": "",
"client_type": 1,
"android_info": {
"package_name": "com.your.package",
"certificate_hash": ""
}
}
],
"api_key": [
{
"current_key": ""
}
],
"services": {
"analytics_service": {
"status": 2,
"analytics_property": {
"tracking_id": ""
}
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 1
}
}
}
],
"configuration_version": "1"
}
Note that I have extended this google-services in case you also use Google Analytics or GCM service.
You would have the following configuration :
app/
├── src/
│ ├── main/
│ └── dev/
│ └── google-services.json
├── google-services.json
└── build.gradle
You can use either :
a new build type
a new product flavor (if you already have existing ones)
Build Type
Add the following build type:
buildTypes {
dev {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
We don't need to build this "dev" build variant in regular build, so you can exclude this variant if a parameter is not specified. Add the following to your app build.gradle :
def build_param = "${build}";
if (build_param != "dev") {
//exclude production build
android.variantFilter { variant ->
if (variant.buildType.name.equals('dev')) {
variant.setIgnore(true);
}
}
} else {
//exclude all except production build
android.variantFilter { variant ->
if (!variant.buildType.name.equals('dev')) {
variant.setIgnore(true);
}
}
}
Product flavor
Add the dev product flavor to existing ones :
productFlavors {
full {
}
dev {
}
}
To remove this dev product flavor from the regular build :
def build_param = "${build}";
if (build_param != "dev") {
//exclude dev
android.variantFilter { variant ->
if (variant.getFlavors().get(0).name.equals('dev')) {
variant.setIgnore(true);
}
}
} else {
//exclude all but dev
android.variantFilter { variant ->
if (!variant.getFlavors().get(0).name.equals('dev')) {
variant.setIgnore(true);
}
}
}
Eventually, add your app module google-services.json to .gitignore :
app/google-services.json
We have previously ensured that this dev variant will only be used when parameter build=dev is specified
Edit .travis.yml to modify the build config :
script:
- ./gradlew clean build -Pbuild=dev
-Pbuild=dev will only build dev build variant using google-services.json located in app/src/dev/google-services.json
Take a look at this sample project which is using google-services Google project
In Travis log, you can see that the JSON file being parsed is the one for the dev build variant :
Parsing json file: /home/travis/build/bertrandmartel/android-googlesignin/app/src/dev/google-services.json
Extra Note
Note that this method is not limited to CI and can be extended for your production build when you require a production google-services.json or a different AndroidManifest.xml (with some specific properties like fabric.io key)
Check this method to prevent commitment of fabric keys embedded in AndroidManifest.xml (and can't be imported from gradle) that is using a different build variant and using a parameter to enable the production build.
You can use travis encrypt-file google-services.json
Documentation
You can do it by:
installed the Travis CI Command Line Client by running $ gem install travis.
logged in to Travis CI using $ travis login or $ travis login --pro
$ travis encrypt-file super_secret.txt
encrypting super_secret.txt for rkh/travis-encrypt-file-example
storing result as super_secret.txt.enc
storing secure env variables for decryption
Then it will print on the console this:
openssl aes-256-cbc -K $encrypted_0a6446eb3ae3_key -iv $encrypted_0a6446eb3ae3_iv -in super_secret.txt.enc -out super_secret.txt -d
You can copy it to your .travis.yml file as I did here
Do not forget to put your .enc file on your GitHub repository.
If you have multiple files, you could zip them and then you unzip the decrypted file on the Travis ci.
For instance, you can do like this:
$ tar cvf secrets.tar foo bar
$ travis encrypt-file secrets.tar
$ vi .travis.yml
$ git add secrets.tar.enc .travis.yml
$ git commit -m 'use secret archive'
$ git push
I did it.
In my case, I had two files to use on the build of my app. So, I used this due to travis does not support multiple encrypted files. Therefore, you zip then on one file and encrypt this file.
You can have a look at my travis script here
Firebase Documentation says:
"For open source projects, we generally do not recommend including the app's Firebase config file or object in source control because, in most cases, your users should create their own Firebase projects and point their apps to their own Firebase resources (via their own Firebase config file or object)."
https://firebase.google.com/docs/projects/learn-more?authuser=0#config-files-objects
Firebase documentation seems to imply that there is no problem in committing the file to GitHub.
Here is an extract from the docs
The Firebase config file contains unique, but non-secret identifiers for your project. To learn more about this config file, visit Understand Firebase Projects.

Categories

Resources