How can I specify location of AndroidManifest.xml? - android

I'm porting a module from Eclipse to Android Studio/Gradle, and need to specify the locations of my sources, resources, and manifest:
sourceSets {
main {
manifest {
srcFile './AndroidManifest.xml'
}
java {
srcDirs = ["src"]
}
resources {
srcDirs = ["resource"]
}
}
}
Android Studio/Gradle seems perfectly happy with my java and resources entries, but balks at my manifest entry:
No signature of method: build_b4wnchd9ct4a5qt388vbbtbpz.sourceSets() is applicable for argument types: (build_b4wnchd9ct4a5qt388vbbtbpz$_run_closure2) values: [build_b4wnchd9ct4a5qt388vbbtbpz$_run_closure2#35290d54]
All of my googling and searching SO suggests that this should have worked.
Arctic Fox, 2020.3.1. Not sure which version of Gradle came with it.

Ahh, figured it out. Leaving here in case someone else has the same question.
Add an android.sourceSets.manifest.srcFile entry to your module's build.gradle file:
android {
...
sourceSets {
main {
manifest {
srcFile './AndroidManifest.xml'
}
}
}
}
or simply:
android {
...
sourceSets.main.manifest.srcFile './AndroidManifest.xml'
}
My biggest mistake was not putting the sourceSets directive inside the android directive.

Related

Intellisense not working in Kotlin Multiplatform Library

I have a kotlin multiplatofrm library that is included into an Android and iOS app.
In my android project include it as a composite build (MyLib). But Intellisense is not working at all for all code from in MyLib, though the whole thing compiles fine. I am using Android Studio. What could be wrong and how can I debug it?
rootProject.name='xxx'
includeBuild 'MyLib'
include ':common'
include ':app'
MyLib's build.gradle.kts looks as follows:
plugins {
kotlin("multiplatform") version "1.5.31"
kotlin("native.cocoapods") version "1.5.31"
}
repositories {
mavenCentral()
maven { setUrl("https://dl.bintray.com/kotlin/kotlinx.html/") }
}
group = "com.xxx.MyLib"
// CocoaPods requires the podspec to have a version.
version = "1.0"
kotlin {
ios()
jvm {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
testRuns["test"].executionTask.configure {
useJUnit()
}
}
cocoapods {
ios.deploymentTarget = "11.4"
frameworkName = "MyLib"
summary = "xxx"
homepage = "xxx"
podfile = project.file("../../iOS-App/Podfile")
}
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.5.31")
implementation("com.badoo.reaktive:reaktive:1.2.0")
implementation("com.badoo.reaktive:reaktive-annotations:1.2.0")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1")
implementation("com.russhwolf:multiplatform-settings-no-arg:0.8.1")
implementation("net.swiftzer.semver:semver:1.1.1")
}
}
}
}
tasks.withType<GenerateModuleMetadata> {
enabled = true
}
I think this is likely related to https://youtrack.jetbrains.com/issue/KTIJ-18903
I had the same issue and it drove me crazy. How can one write code nowadays without Intellisense (answer: you can't).
I tried a ton of things (all the usual and unusual stuff you do when Android Studio / IntelliJ act up). Ultimately I upgraded to Kotlin 1.6.0-RC2 (from 1.5.31) -> https://github.com/JetBrains/kotlin/releases/tag/v1.6.0-RC2.
Part of that is upgrading the Kotlin Plugin:
Another part is the Kotlin Gradle Plugin dependency:
org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0-RC2
And last but not least I had to downgrade the corouting dependency (from 1.5.2):
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0-RC
After that everything was back to normal.

How do you add gRPC to Android Studio with Kotlin?

Task
I need to connect an Android client with a python server using gRPC. Making the server and generating the protos was easy in Python, but the lack of tutorials and confusing documentation for the Kt client makes it appear overwhelmingly complicated.
Background
Until now I've made some simple Android apps using Kotlin, I got used to adding dependencies to either the module or app level build.gradle.
What have I tried?
My first thought was to go to the official documentation as I did with Python.
I found the guide from there pretty confusing (I felt like there's something missing from that article), so I went to see the full examples from their GitHub. I also cloned the repo and compiled the protos with the gradlew installDist command. Then the things got awfully complicated:
When you create an Android Studio project, you get a bunch of gradle things(module and app level build.gradle's, gradlew and gradlew.bat, settings, etc)
After you clone the repo, you get another bunch of gradle things inside the grpc-kotlin folder.
You also get build.gradle.kts which seem to be the same build logic/package manager helper files, but with other dependencies and with the Kotlin Script syntax.
This is when I went off to YouTube in order to search for a simple implementation and found out that there's only a handful of videos on the gRPC with Kotlin subject, and most of those are presentation videos about the features of gRPC in Kotlin when using Coroutines.
What I have until now
I migrated all my build.gradle's to .kts ones.
This is how my module-level build.gradle.kts looks like:
buildscript {
val kotlin_version = "1.5.10"
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:4.2.1")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}")
classpath("org.jetbrains.kotlin:kotlin-android-extensions:${kotlin_version}")
classpath("com.google.gms:google-services:4.3.8")
classpath ("com.google.protobuf:protobuf-gradle-plugin:0.8.14")
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
tasks.register("clean",Delete::class){
delete(rootProject.buildDir)
}
This is how my app level build.gradle.kts looks like:
import com.google.protobuf.gradle.generateProtoTasks
import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.plugins
import com.google.protobuf.gradle.protobuf
import com.google.protobuf.gradle.protoc
plugins {
id("com.android.application")
id("com.google.protobuf")
kotlin("android")
}
android {
compileSdkVersion(30)
buildToolsVersion = "30.0.3"
defaultConfig {
applicationId = "com.example.myapplication"
minSdkVersion(26)
targetSdkVersion(30)
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}
protobuf {
protoc { artifact = "com.google.protobuf:protoc:3.12.0" }
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.35.0"
}
}
generateProtoTasks {
all().forEach { task ->
task.plugins.create("java") {
option("lite")
}
task.plugins {
id("grpc") {
this.option("lite")
}
}
}
}
}
dependencies {
val kotlin_version = "1.5.10"
implementation("org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}")
implementation("androidx.core:core-ktx:1.5.0")
implementation("androidx.appcompat:appcompat:1.3.0")
implementation("com.google.android.material:material:1.3.0")
implementation("androidx.constraintlayout:constraintlayout:2.0.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.2")
androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
// GRPC Deps
implementation("io.grpc:grpc-okhttp:1.37.0")
implementation("io.grpc:grpc-protobuf-lite:1.37.0")
implementation("io.grpc:grpc-stub:1.36.0")
implementation("org.apache.tomcat:annotations-api:6.0.53")
}
I could generate the protos but something was off about them.
Problem
When implementing the request functions, respectively a bi-directional stream, I found out that all my rpc functions asked for an extra StreamObserver parameter(which was absent in all of the tutorials I've found on the internet). At a closer look I observed that all the generated files were in java and on the official docs, the generated files are both POJOs and Kotlin.
This is how my generated Stub class looks like:
public static final class ChatServiceStub extends io.grpc.stub.AbstractAsyncStub<ChatServiceStub> {
private ChatServiceStub(
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
super(channel, callOptions);
}
#java.lang.Override
protected ChatServiceStub build(
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new ChatServiceStub(channel, callOptions);
}
/**
* <pre>
* This bi-directional stream makes it possible to send and receive Notes between 2 persons
* </pre>
*/
public void chatStream(grpc.Chat.Empty request,
io.grpc.stub.StreamObserver<grpc.Chat.Note> responseObserver) {
io.grpc.stub.ClientCalls.asyncServerStreamingCall(
getChannel().newCall(getChatStreamMethod(), getCallOptions()), request, responseObserver);
}
/**
*/
public void sendNote(grpc.Chat.Note request,
io.grpc.stub.StreamObserver<grpc.Chat.Empty> responseObserver) {
io.grpc.stub.ClientCalls.asyncUnaryCall(
getChannel().newCall(getSendNoteMethod(), getCallOptions()), request, responseObserver);
}
}
I do not know how to replicate a gradle script for my project, I found no one on the internet explaining how are all those build.gradle's linked together(I figured out that module level build.gradle's are describing how the module they're in is supposed to build and app level build.gradle's are idem but for the entire app). Most of the articles I found are the same as the official docs.
What I want
I just want a simple-simple project or a step by step tutorial, without "clone this and run a command in the terminal, it just works".
I do not blame the devs or whoever wrote the official docs, I actually bet I'm the stupid one here, but I struggle to understand these concepts and I would be grateful if someone can explain to me what I did wrong or where to learn.
Also, sorry for the long question, I tried to expose my POV the best I could, this is my second question since I started learning programming and I'm sorry if the problem and my goals aren't clear enough, I'll edit anything if it's needed.
I do not have a step-by-step process I can share and do not want to trivialize an excellently asked question. However, I wanted to respond that in researching a similar problem, I found that Square has a library that seems to be more Kotlin friendly:
https://square.github.io/wire/#wire-kotlin
I created the simplest project I could. The minimal configuration of Android project can be found in this commit.
Start a new project from Basic activity template in Android Studio and then:
Modify app/build.gradle
Add Gradle plugin, so it can generate all stubs in the build step:
plugins { // this section should be already at the top of the file
// ...
id 'com.google.protobuf' version "0.9.1" // "0.9.2 causes compilation errors!"
}
WARNING: At the time of writing, the latest version of
protobuf gradle plugin is 0.9.2. Using this version causes
build errors I wasn't able to deal with using information I found
on the Internet.
Add project dependencies. Lack of one can cause usually
non-understandable error messages.
dependencies { // this section should be already in the file
// ...
implementation 'io.grpc:grpc-stub:1.52.1'
implementation 'io.grpc:grpc-protobuf:1.52.1'
implementation 'io.grpc:grpc-okhttp:1.52.1'
implementation 'io.grpc:protoc-gen-grpc-kotlin:1.3.0'
implementation 'io.grpc:grpc-kotlin-stub:1.3.0'
implementation 'com.google.protobuf:protobuf-kotlin:3.21.12'
}
Add protobuf section - the one that is responsible for generating protobuf data classes and gRPC client stubs:
protobuf { // this section needs to be added
protoc {
artifact = "com.google.protobuf:protoc:3.21.12"
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.52.1"
}
grpckt {
// I don't really know what ":jdk8#jar" does...
artifact = "io.grpc:protoc-gen-grpc-kotlin:1.3.0:jdk8#jar"
// ...but it doesn't work without it.
}
}
generateProtoTasks {
all().forEach {
it.plugins {
grpc {}
grpckt {}
}
it.builtins {
kotlin {}
java {}
}
}
}
}
4*. I placed my proto file in app/src/main/proto directory. In case you store your protos in some other directory you can configure it by following instructions from protobuf-gradle-plugin repo readme.
Update AndroidManifest.xml
You also need to add proper permission to app/src/main/AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<!-- ... -->
</manifest>
Use gRPC client
class FirstFragment : Fragment() {
// ...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val port = 50051
val channel = ManagedChannelBuilder.forAddress("192.168.0.13", port).usePlaintext().build()
val stub = PingPongGrpcKt.PingPongCoroutineStub(channel)
binding.buttonFirst.setOnClickListener {
runBlocking {
val request = Pingpong.PingPongMsg.newBuilder().setPayload("World").build()
val response = stub.ping(request)
Log.i("result", response.toString())
}
}
}
// ...
}

Build failed after update coroutines to 1.2.0: META-INF/atomicfu.kotlin_module

After update to org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.0 android build failed with issue:
More than one file was found with OS independent path 'META-INF/atomicfu.kotlin_module'
Are there any workaround to make it work?
In app-level build.gradle add the following to android level :-
packagingOptions {
pickFirst("META-INF/atomicfu.kotlin_module")
}
It would look like :-
android {
.......
packagingOptions {
......
pickFirst("META-INF/atomicfu.kotlin_module")
}
}
Adding -dontwarn kotlinx.atomicfu.** to my proguard rules file was enough to get my build working with version 1.2.1 of the kotlinx-coroutines-android library.
Adding the packagingOptions { pickFirst('META-INF/atomicfu.kotlin_module') } or packagingOptions { exclude('META-INF/atomicfu.kotlin_module') } block in my build.gradle file didn't work.

how to merge two manifests in same module by gradle?

I have two manifests in one module, one is used as a library for the whole app, another is used as the module's manifest, then this module can run alone. Below is my gradle config:
sourceSets {
main {
if (configs.isIntegrate) {
//module as app
manifest.srcFile 'src/main/module/AndroidManifest.xml'
} else {
//module as library
manifest.srcFile 'src/main/AndroidManifest.xml'
}
}
}
so, when I add a new activity, I have to register twice.is this a way I can register
once in manifest below the main dir, then let the manifest that below the main/module dir merge it?

How to replace strings resources with Android Gradle

I made a new app with gradle in Android Studio, and now I need to make about 10 versions with different package names and values in resources. I made custom flavors as in example and want to replace some strings in this custom flavors with custom values. I found example like this:
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: ['version': '2.2'])
But i don't know where to put it. As i understand i need to put it into separate task, but how to make this task called by IDE?
Also i need to replace few variables inside Java classes and Content Provider's auth, maybe i need to do this by copy files into flavor1 folder and let gradle to merge it, but it seems like wrong solution to store many copies of files with difference in one line... Maybe i need to user some other solution for all this?
Here is build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':JazzyListView')
compile project(':ABS')
compile project(':Volley')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
versionCode 5
versionName "3.0"
minSdkVersion 8
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
}
}
productFlavors {
flavor1 {
packageName "com.example.flavor1"
}
flavor2 {
packageName "com.example.flavor2"
}
}
}
I had a similar problem. I wanted to add the Jenkins build number to the strings that get merged from strings.xml. Here's my solution as of Android Gradle plugin 0.12.+.
// Insert the build number into strings.xml
android.applicationVariants.all{ variant ->
variant.mergeResources.doLast{
ext.env = System.getenv()
def buildNumber = env.BUILD_NUMBER
if (buildNumber != null) {
File valuesFile = file("${buildDir}/intermediates/res/${variant.dirName}/values/values.xml")
println("Replacing revision number in " + valuesFile)
println("Build number = " + buildNumber)
String content = valuesFile.getText('UTF-8')
content = content.replaceAll(/devBuild/, buildNumber)
valuesFile.write(content, 'UTF-8')
}
}
}
You might want to hook into a different Gradle task depending on what you want to do. Take a look at the tasks that are part of the Android build to figure that out.
http://tools.android.com/tech-docs/new-build-system/user-guide
UPDATE: At some point, the Android Gradle plugin changed the way to iterate through application variants keyword from each to all. My answer has been updated to reflect the change, but try switching to each if this code doesn't print anything to the console.
I was trying to get similar functionality as Maven resource filtering.
This is what I came up with. My solution could use some changes to be more robust (i.e. pulling from a properties file, etc).
My example just shows how to replace a single value, which is all that I needed. The variables follow the ${some.property} convention. This solution also works with product flavors that have their own resource files.
import org.apache.tools.ant.filters.*
...
android.applicationVariants.all{ variant ->
// Perform resource filtering
variant.mergeResources.doLast {
filterResources(variant)
}
}
def filterResources(buildVariant) {
//Setup temp directory to filter the resources
File resFiltered = file("${buildDir}/res/all/filtered/${buildVariant.dirName}")
if(resFiltered.exists()){
resFiltered.delete()
}
//Copy and filter the resources.
copy {
from(buildVariant.processResources.resDir) {
include '**/*.xml'
//Could be improved upon to pull from a properties file, etc.
ant.properties['app.version'] = project.version
filter(ExpandProperties, project: ant.project)
}
from(buildVariant.processResources.resDir) {
exclude '**/*.xml'
}
into resFiltered
}
//Delete all the original resource files
file(buildVariant.processResources.resDir).deleteDir()
//Replace with the filtered ones.
resFiltered.renameTo(file(buildVariant.processResources.resDir))
//Delete the original 'filtered' directory
file( "${buildDir}/res/all/filtered").deleteDir()
}
Example in strings.xml
...
<string name="app_version">${app.version}</string>
...
These links may be helpful:
Using Gradle for building Android applications
Gradle Plugin User Guide
And my filter definition using regex to replace something:
from('res/') {
include '**/*.xml'
filter {
line -> line.replaceAll(/YOUR_REGEX_TO_REPLACE_SOMETHING/, 'REPLACED_RESULT_EXPRESSION')
}
}
into 'build/path/to/your/filtered/resources/'
In your "src" folder, create "flavor1" and "flavor2" folders with the same hierarchy as your "main" folder. If you have strings.xml in your main/res/values, you can do it in flavor1/res/values as well and have the replacement values in there. It may show errors in the IDE but it should still build and run.

Categories

Resources