Couchbase lite in Android

Hi,
I install the couchbase server 6.5 and create bucket, and connect sync gateway with CB server successfully, however for Couchbase lite in android mobile, I have some problems when build the project. However when I add the Starter code https://docs.couchbase.com/couchbase-lite/2.7/java-android.html I get some errors such as context can not be null. Is there any sample source code for couchbase 2.7 that I can use? Also what is context? I created getting-started-db bucket.

error: cannot find symbol
CouchbaseLite.init(context);
^
symbol: variable context
location: class MainActivity

That’s the Android application context getApplicationContext()

For an example, you can refer to the " Getting Started with Couchbase Mobile on Android" in the tutorials page.

  • standalone :Start with basic database and CRUD operations
  • query. Adds query functionality
  • sync shows basic sync functionality

Thanks, I went with https://docs.couchbase.com/userprofile-couchbase-mobile/sync/userprofile/android/userprofile_sync.html and change the sync json file to walrus but the mobile App doesnt sync and I get this error:

[emulator-5554]: E/CouchbaseLite/REPLICATOR: {Repl#7}==> N8litecore4repl10ReplicatorE /data/user/0/com.couchbase.userprofile/files/demo@example.com/userprofile.cblite2/ ->ws://10.0.2.2:4984/userprofile/_blipsync @0xe1699440
[emulator-5554]: E/CouchbaseLite/REPLICATOR: {Repl#7} Got LiteCore error: WebSocket error 1001 “WebSocket connection closed by peer”
[emulator-5554]: E/Rep Scheduler Log: ReplicationTag Stopped

[emulator-5556]: W/CouchbaseLite/NETWORK: WebSocketListener failed with response null (java.net.UnknownServiceException: CLEARTEXT communication to 10.0.2.2 not permitted by network security policy)
[emulator-5556]: E/CouchbaseLite/REPLICATOR: {Repl#7} Got LiteCore error: WebSocket error 1001 “WebSocket connection closed by peer”

Non-TLS traffic is disabled by default in modern Android applications (this is something Google has done, not Couchbase) -> https://stackoverflow.com/a/50834600/1155387

Thank you for support, I solved the issue.
I have another problem, when I add the user using this API: https://docs.couchbase.com/sync-gateway/2.0/admin-rest-api.html#/user/post__db___user_
and after I login in the app which I get from https://docs.couchbase.com/userprofile-couchbase-mobile/sync/userprofile/android/userprofile_sync.html, the user is not authorized and channel is not create.

As indicated in the tutorial, the walrus is only for demo purposes so I would strongly recommend that you setup a Couchbase Server and connect to the sync gateway first.
We can then start troubleshooting if users are getting created correctly by first querying for users via the users REST endpoint or on the server.

Thanks, I changed and could add the user successfully.
I created a new bucketname and connect sync gateway with that bucket in the server however when I add the data name, address using this tutorial, https://docs.couchbase.com/userprofile-couchbase-mobile/sync/userprofile/android/userprofile_sync.html
The data is will be in the sync gateway and I can check using curl -X GET http://localhost:4985/newdb/user::demo@example.com
but Data doesnt show in the Android. I already changed userProfileDbName from Databasemanager.java file, is there any other file I need to change?
I add the new bucket in json file of sync gateway

Check your sync gateway and couchbase lite log files.

however when I add the data name, address using this tutorial, https://docs.couchbase.com/userprofile-couchbase-mobile/sync/userprofile/android/userprofile_sync.html

How did you add the data? It should be through the Android app, right? I am not clear on what you mean by " Data doesnt show in the Android" .

I add the data using Android app. When I input data for name, address and image, it doesnt save in sync gateway. I just create new bucket and change the bucketname in sync gateway. In the App also I changed the DB name.
I changed the userProfileDbName in the Android app, DataBasemanager.java file.
private static String userProfileDbName = “newdb”
Is there any other file I need to change in the App?
I can see that data is in sync gateway, but when I click on Save button in the App, the data just show empty.
I think public void changed(QueryChange change) in the android doesnt work and I get null data, do I need to change the Dictionary dictionary = row.getDictionary(“userprofile”) to new database?

Yes, you have to change that. When you query for a document, each entry is associated with the database name. Check out the “Processing Query Responses” section here

Also FFR , I would recommend stepping through the code in debug mode to understand what’s going on. You would have then quickly seen the output of the query does not align with what the app expects.

Thanks it works. in Sync gateway and couchbase server, Can we authenticate the user using secure JSON Web Tokens (JWTs), tokenize and create token , similar to this feature in Firebase? https://firebase.google.com/docs/auth/admin/create-custom-tokens

Yes , we support OIDC / JWT tokens for authentication . The implicit flow is what we’d recommend. Here is a tutorial that discusses integration with Keycloak as the OIDC provider .

I will use mobile Login SDK which the user should login using that, probably I use custom authentication. is there any source code related to that? The different between custom authentication and implicit flow is just in the implicit flow the authentication happen by third party and in custom authentication we already have authentication token and we create session? In both part we need to create the user in couchbase before creating session?

Check out this blog on custom authentication
In both causes user must be created on Sync Gateway. You have a choice of letting sync gateway automatically create the user after initial authentication by enabling the register config. If you want to statically associated channels with the user, you will have to do a PUT _user to update the user with channels.

Thanks for support. Register config is string, what value should I specify for that?(is there any sample json file?)
Also when user automatically created, do we need to specify password?
My understanding is, after authentication, we need to create the user in sync gateway, and then we can create session and channel for that users. is that correct?

Interesting. I’d expect that to be a boolean. So it should be true (@bbrks - can you confirm)

using Custom Authentication, we need to have App server to create the user in sync gateway, isn’t it?
How can we create those users in the sync gateway automatically?

The OIDC register config option is a boolean, looks like the docs have the wrong type, I’ll open a PR to fix it now.

Thanks, so if we create user in sync gateway by ourself, then we can create session using that user credential and couchbase lite, can use the session?

That is correct. The App server will create session on Sync Gateway for that user then returns session Id to couchbase Lite and the client will use that session Id when it sets up the replication.