Having experienced this problem in a separate project, I made a test project to verify the issue.
Opening Android Studio 3.0, I created a new basic project. Its main module was called app.
I added a library module, called libraryone.
In libraryone, I added a dependency to Gson, and added a single class with a single static method using Gson.
In app, I added a dependency to libraryone. I tested it plain, as well as with transitive = true, as the internet seemed to vaguely concur that doing so might help. (It did not.)
In app, in MainActivity, I used the class from libraryone. This worked (as long as I didn't have transitive = false set, instead).
However, I cannot reference Gson from MainActivity itself. It gives a "Cannot resolve symbol 'Gson'" error, both from Android Studio, as well as from the command line Gradle. This is...unusual and aggravating, as it means you have to repeat common dependencies all throughout a group of projects, and the classes are included in the output anyway. Surely either I'm doing something wrong, or this is a bug?
My code is as follows:
MainActivity.java (in app)
package com.erhannis.test.dependencytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.erhannis.test.libraryone.LibClass;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s = LibClass.convert(new Object());
System.out.println("out: " + s);
new com.google.gson.Gson(); // This line gives a compile-time error: "Cannot resolve symbol 'Gson'"
}
}
LibClass.java (in libraryone)
package com.erhannis.test.libraryone;
import com.google.gson.Gson;
public class LibClass {
public static String convert(Object o) {
Gson gson = new Gson();
return gson.toJson(o);
}
}
build.gradle (app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.erhannis.test.dependencytest"
minSdkVersion 18
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation (project(path: ':libraryone'))
}
build.gradle (libraryone)
apply plugin: 'com.android.library'
android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 18
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.google.code.gson:gson:2.8.2'
}
It is because you declared the transitive dependency as implementation. Use api instead of implementation change:
implementation 'com.google.code.gson:gson:2.8.2'
to:
api 'com.google.code.gson:gson:2.8.2'
The reason why you should declare the transitive dependency in your library can be found here
You are using:
implementation (project(path: ':libraryone'))
implementation 'com.google.code.gson:gson:2.8.2'
Check the official doc:
When your module configures an implementation dependency, it's letting Gradle know that the module does not want to leak the dependency to other modules at compile time. That is, the dependency is available to other modules only at runtime.
Here you can find a very good blog about it.
The best solution for this is to create a private Github/Gitlab repository for the aar dependency. You can also make it public if you want. The aar file does not contain transitive dependencies when used locally. We need to publish it somewhere with the .pom file. Here's how to do it,
create a personal access token in Github
Github -> Settings -> Developer Settings -> Personal Access token - with write:packages / read:packages access level
(create two tokens if you need to distribute privately
Add id 'maven-publish' to plugins in module level gradle
Add the below code to module level gradle
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
archiveClassifier.set("sources")
}
publishing {
publications {
bar(MavenPublication) {
groupId 'com.your'
artifactId 'artifact'
version '1.0'
artifact sourceJar
artifact("$buildDir/outputs/aar/YourLibraryName-release.aar")
pom.withXml {
final dependenciesNode = asNode().appendNode('dependencies')
ext.addDependency = { Dependency dep, String scope ->
if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
return
final dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dep.group)
dependencyNode.appendNode('artifactId', dep.name)
dependencyNode.appendNode('version', dep.version)
dependencyNode.appendNode('scope', scope)
if (!dep.transitive) {
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
exclusionNode.appendNode('groupId', '*')
exclusionNode.appendNode('artifactId', '*')
} else if (!dep.properties.excludeRules.empty) {
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
dep.properties.excludeRules.each { ExcludeRule rule ->
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
}
}
}
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/GITHUB_USERID/REPOSITORY")
credentials {
username = "GITHUB_USERID" // Better if you use env variable
password = "WRITE_ACCESS_TOKEN"
}
}
}
}
In the Gradle toolbar you can find publish under the publishing subdirectory in your library module directory. ( Make sure to build the aar from build->build in module directory before publish)
To use it in your project, add following to your app level gradle file inside android{ }tag
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/GITHUB_USERID/REPOSITORY")
credentials {
username = "GITHUB_USERID" // Better if you use env variable
password = "READ_ACCESS_TOKEN"
}
}
}
Finally in dependencies
implementation 'com.your:artifact:1.0'
*Gitlab is also pretty similar to this
Related
I have a bintray repo what I uploaded my android aar library to and I am trying to link my repo to Jcenter so that other people can import it into their project.
When I click the Add to Jcenter button in bintray I am taken to A Compose message page, I dont do anything on this page except click the Send button. When I click the button I get n error message in bintray saying
Failed to send a message: Package should include sources as part of
the package
this is my build.gradle for the library
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'
task androidJavadocs(type: Javadoc) {
failOnError = false
source = android.sourceSets.main.java.srcDirs
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath += files(ext.androidJar)
exclude '**/R.html', '**/R.*.html', '**/index.html'
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
publishing {
publications {
Production(MavenPublication) {
artifact("$buildDir/outputs/aar/mlcamera-release.aar")
groupId 'com.tycz'
artifactId 'mlcamera'
version '0.1.0'
artifact androidJavadocsJar
artifact androidSourcesJar
//The publication doesn't know about our dependencies, so we have to manually add them to the pom
pom.withXml {
//def dependenciesNode = asNode().appendNode('dependencies')
//Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
configurations.compile.allDependencies.each {
if (it.name != 'unspecified') {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}
bintray {
// Get Bintray credential from environment variable
user = System.getenv('BINTRAY_USER')
key = System.getenv('BINTRAY_API_KEY')
dryRun = false
override = true
publish = true
pkg {
repo = 'MLCamera'
name = project.name
userOrg = 'tyczj359'
licenses = ['Apache-2.0']
desc = 'A wrapper library for the new CameraX API and Firebase MLKit to create easier setup for MLKit usage'
vcsUrl = 'https://github.com/tyczj/MLCamera.git'
version {
name = '0.1.0'
released = new Date()
}
}
publications = ['Production']
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "0.1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'com.google.firebase:firebase-ml-vision:24.0.1'
implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.0.2'
implementation 'com.google.firebase:firebase-ml-vision-object-detection-model:19.0.3'
implementation 'androidx.core:core-ktx:1.2.0'
implementation "androidx.camera:camera-core:1.0.0-beta01"
implementation "androidx.camera:camera-camera2:1.0.0-beta01"
implementation "androidx.camera:camera-view:1.0.0-alpha08"
implementation "androidx.camera:camera-lifecycle:1.0.0-beta01"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Can anyone tell me what the problem is?
Bintray has some preconditions to link to Jcenter.
Some are that you need a public maven repository, sources file/s, a pom file, etc' (more can be found at JFrog's wiki). You will need to upload those files if you want to Link to Jcenter.
In your build.gradle you explicitly upload the .aar and also upload the androidSourcesJar.
From going over the build.gradle file it looks like you are adding the sources, you should see error message if there are any issues with collecting the files.
My suggestion is changing the order of the from:
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
from androidJavadocs.destinationDir
classifier = 'javadoc'
}
task androidSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
publishing {
publications {
Production(MavenPublication) {
artifact androidJavadocsJar
artifact androidSourcesJar
groupId 'com.tycz'
artifactId 'mlcamera'
version '0.1.0'
.
.
.
You can follow gradle-bintray-plugin README with examples.
See the section on the workaround for Android pom file dependencies.
You can also use these guides:
Simple way to publish your Android library to JCenter
Publishing Gradle Android Library to Jcenter.
I made a simple protobuf+gRPC server/client example with Python, and wanted now to complete it with an Android Java client. However, I struggled through the documentation and got it mostly working (.proto compiles in Android Studio), BUT now I get Errors in the output *Grpc.java file (i.e. if I fix it it will just be overwritten by the compiler):
error: package io.grpc.protobuf does not exist
error: package com.google.protobuf.Descriptors does not exist
Since I get errors from io.gprc's and com.google's protobuf, I suspect a definition conflict in my gradle, but I cannot find/resolve it (went through several "tutorials" and it seemed common to use a mix of grpc and google sources).
Here is my build.gradle:
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "***.grpcclient"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'io.grpc:grpc-okhttp:1.25.0'
//implementation 'io.grpc:grpc-protobuf-lite:1.25.0'
implementation 'com.google.protobuf:protobuf-javalite:3.8.0'
implementation 'io.grpc:grpc-stub:1.25.0'
implementation 'org.glassfish:javax.annotation:10.0-b28'
}
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10'
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.8.0'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.25.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option "lite"
}
python { }
}
task.plugins {
grpc {outputSubDir = 'java'}
}
}
}
}
Any help is appreciated!
Android uses Protobuf Lite, which is a subset of the normal implementation and optimized for Android. You correctly tried to depend on io.grpc:grpc-protobuf-lite:1.25.0. This provides the io.grpc.protobuf.lite package.
You also correctly configured protoc to generate protobuf-lite messages. However, the grpc plugin is generating "full" protobuf services. That's why you're seeing references to io.grpc.protobuf and classes missing in protobuf like com.google.protobuf.Descriptors.
protobuf {
...
generateProtoTasks {
all().each { task ->
...
task.plugins {
grpc {
outputSubDir = 'java'
option 'lite' // Needed this line
}
}
}
}
}
Since you are using protoc with the 'lite' option, instead of using the older protoc-gen-javalite plugin, you are using the correct protobuf-javalite dependency for protobuf. However, grpc-java 1.25.0 depends on protobuf-lite which will collide. This is discussed some in Issue 6405 and will be fixed in grpc-java 1.26.0. But for the moment you'll need to exclude the protobuf-lite dependency brought in by grpc-protobuf-lite.
dependencies {
...
implementation ('io.grpc:grpc-protobuf-lite:1.25.0') {
// Exclude will not be necessary starting in grpc 1.26
exclude group: 'com.google.protobuf', module: 'protobuf-lite'
}
}
My working build.gradle:
dependencies {
implementation 'com.squareup.okhttp:okhttp:2.7.5'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'io.grpc:grpc-core:1.27.1'
implementation 'io.grpc:grpc-stub:1.27.1'
implementation 'io.grpc:grpc-okhttp:1.27.1'
implementation('io.grpc:grpc-protobuf-lite:1.27.1') {
exclude module: "protobuf-lite"
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.11.4'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.27.1'
}
javalite {
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
task.plugins {
grpc {
// Options added to --grpc_out
option 'lite'
}
}
}
}
}
I am coming because I can't make work my communication between my API (Go) and my client (Android).
I have this protobuf file:
syntax = "proto3";
option java_package = "com.emixam23.rushpoc.protobuf";
option java_outer_classname = "HelloWorld";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
The protobuf file comes from the example of https://grpc.io/docs/quickstart/go.html, I just didn't implemented the SayHelloAgain. What i am trying to achieve is, from my android app, SayHello to my Go API and get a reply...
For android, I followed that tutorial (https://grpc.io/docs/quickstart/android.html) in order to, from the protobuf file, to communicate with my API. However, there is a stub, comming from I don't know where.
So I searched about how to create a stub (https://grpc.io/docs/tutorials/basic/android.html) and nothing.. ManagedChannelBuilder doesn't exist and I can't find the way to install it..
PS: to generate my Java class from the protobuf file, I followed that tutorial: https://proandroiddev.com/how-to-setup-your-android-app-to-use-protobuf-96132340de5c
Am I in the right direction or totally wrong?
My project structure:
APP build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.rushpoc.emixam23.androidapp"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//Protobuf
implementation 'com.google.protobuf:protobuf-lite:3.0.0'
implementation 'io.grpc:grpc-okhttp:1.13.2'
implementation 'io.grpc:grpc-protobuf-lite:1.13.2'
implementation 'io.grpc:grpc-stub:1.13.2'
}
protobuf {
generatedFilesBaseDir = "$projectDir/generated"
protoc {
// You still need protoc like in the non-Android case
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
javalite {
// The codegen for lite comes as a separate artifact
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.13.2'
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java
}
task.plugins {
grpc {}
}
}
}
}
TOP-LEVEL/Root build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.protobufVersion = '0.8.6'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufVersion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I haven't checked the entire gradle files yet but I see in your screenshot the .proto file was in src/main/protobufs, which was not following either of the tutorials you mentioned. The protobuf gradle plugin does not detect this directory by default. Therefore I suggest you change it into the default directory src/main/proto. If you would like to insist putting the .proto file in src/main/protobufs, you might need let the protobuf gradle plugin know it by adding
// see https://github.com/google/protobuf-gradle-plugin#customizing-source-directories
sourceSets {
main {
proto {
// In addition to the default 'src/main/proto'
srcDir 'src/main/protobufs'
}
}
}
After that, the protobuf gradle plugin will generate the java code if there's no other mistake.
I'm designing a system with some not-so-simple classes that require a Context object in order to initialize them. These classes make use of third party classes which also require context initialization. This class also utilizes the context to load a number of string resources necessary to the functionality.
The problem comes with writing Instrumented Unit tests for these classes. When I attempt to get a Context object for the test using InstrumentationRegistry.getContext(), I run into an exception where the context cannot find the string resources associated with the class (android.content.res.Resources$NotFoundException).
My question is this: How can I design these tests so that the context can retrieve the string resources that I need, and also act as suitable context objects for the third party classes? There's only so much mocking I can do as some of these classes handle auth tokens, which would be difficult to mock. I can't be the only person who's run into this issue in the Android domain, so I'm sure there's a common solution for this presumably common problem.
EDIT:
As suggested, I've tried integrating Robolectric (version 3.3.2) in my Project, however when I try and run my unit tests I'm met with the following error:
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
I've tried adding the targetCompatibility and sourceCompatibility lines to my gradle files (in several locations) to no avail.
Here's my mobile build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'checkstyle'
apply plugin: 'io.fabric'
project.ext {
supportLibVersion = '25.3.0'
multiDexSupportVersion = '1.0.1'
gsonVersion = '2.8.0'
retrofitVersion = '2.2.0'
daggerVersion = '2.4'
butterKnifeVersion = '8.5.1'
eventBusVersion = '3.0.0'
awsCoreServicesVersion = '2.2.+'
twitterKitVersion = '2.3.2#aar'
facebookVersion = '4.+'
crashlyticsVersion = '2.6.7#aar'
autoValueVersion = '1.2'
autoValueParcelVersion = '0.2.5'
autoValueGsonVersion = '0.4.4'
permissionDispatcher = '2.2.0'
testRunnerVersion = '0.5'
espressoVersion = '2.2.2'
junitVersion = '4.12'
roboelectricVersion = '3.3.2'
}
def gitSha = exec('git rev-parse --short HEAD', "unknown");
def gitCommitCount = 100 + Integer.parseInt(exec('git rev-list --count HEAD', "-1"))
def gitTag = exec('git describe --tags', stringify(gitCommitCount))
def gitTimestamp = exec('git log -n 1 --format=%at', -1)
def appId = "com.example.myapp"
def isCi = "true".equals(System.getenv("CI"))
// Uncomment if you wish to enable Jack & Java8
// apply from: 'jack.gradle'
// Uncomment if you wish to enable Sonar
//apply from: 'sonar.gradle'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId appId
minSdkVersion 16
targetSdkVersion 25
multiDexEnabled = true
versionCode gitCommitCount
versionName gitTag
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildConfigField 'String', 'GIT_SHA', "\"${gitSha}\""
buildConfigField 'long', 'GIT_TIMESTAMP', "${gitTimestamp}L"
}
buildTypes {
debug {
applicationIdSuffix '.debug'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
qa.initWith(buildTypes.release)
qa {
applicationIdSuffix '.qa'
debuggable true
}
}
lintOptions {
abortOnError false
}
applicationVariants.all { variant ->
def strictMode = !variant.name.equals("release")
buildConfigField 'boolean', 'STRICT_MODE_ENABLED', "${strictMode}"
}
}
configurations.all {
resolutionStrategy {
force "com.android.support:support-annotations:$supportLibVersion"
force "com.squareup.okhttp3:okhttp:3.4.1"
force "com.squareup:okio:1.9.0"
force "com.google.guava:guava:19.0"
}
}
dependencies {
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile "com.android.support:design:$supportLibVersion"
compile "com.android.support:recyclerview-v7:$supportLibVersion"
compile "com.android.support:cardview-v7:$supportLibVersion"
compile "com.android.support:multidex:$multiDexSupportVersion"
compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile "com.google.dagger:dagger:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
provided 'javax.annotation:jsr250-api:1.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.jakewharton.timber:timber:4.3.1'
compile "com.jakewharton:butterknife:$butterKnifeVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$butterKnifeVersion"
compile "org.greenrobot:eventbus:$eventBusVersion"
annotationProcessor "org.greenrobot:eventbus:$eventBusVersion"
compile 'io.reactivex.rxjava2:rxandroid:2.0.0'
debugCompile 'com.squareup.okhttp3:logging-interceptor:3.4.2'
compile "com.google.auto.value:auto-value:$autoValueVersion"
annotationProcessor "com.google.auto.value:auto-value:$autoValueVersion"
compile "com.ryanharter.auto.value:auto-value-parcel-adapter:$autoValueParcelVersion"
annotationProcessor "com.ryanharter.auto.value:auto-value-parcel:$autoValueParcelVersion"
compile "com.github.hotchemi:permissionsdispatcher:$permissionDispatcher"
annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:$permissionDispatcher"
compile("com.crashlytics.sdk.android:crashlytics:$crashlyticsVersion") {
transitive = true;
}
compile("com.twitter.sdk.android:twitter:$twitterKitVersion") {
transitive = true
}
compile "com.facebook.android:facebook-android-sdk:$facebookVersion"
compile "com.amazonaws:aws-android-sdk-core:$awsCoreServicesVersion"
annotationProcessor "com.amazonaws:aws-android-sdk-core:$awsCoreServicesVersion"
compile "com.amazonaws:aws-android-sdk-apigateway-core:$awsCoreServicesVersion"
annotationProcessor "com.amazonaws:aws-android-sdk-apigateway-core:$awsCoreServicesVersion"
compile "com.amazonaws:aws-android-sdk-cognito:$awsCoreServicesVersion"
annotationProcessor "com.amazonaws:aws-android-sdk-cognito:$awsCoreServicesVersion"
compile "com.amazonaws:aws-android-sdk-cognitoidentityprovider:$awsCoreServicesVersion"
annotationProcessor "com.amazonaws:aws-android-sdk-cognitoidentityprovider:$awsCoreServicesVersion"
compile "com.amazonaws:aws-android-sdk-lambda:$awsCoreServicesVersion"
annotationProcessor "com.amazonaws:aws-android-sdk-lambda:$awsCoreServicesVersion"
compile "com.amazonaws:aws-android-sdk-sns:$awsCoreServicesVersion"
annotationProcessor "com.amazonaws:aws-android-sdk-sns:$awsCoreServicesVersion"
androidTestCompile "junit:junit:$junitVersion"
androidTestCompile "com.android.support.test:runner:$testRunnerVersion"
androidTestCompile "com.android.support.test:rules:$testRunnerVersion"
androidTestCompile "com.android.support.test.espresso:espresso-intents:$espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-core:$espressoVersion"
androidTestCompile "com.squareup.retrofit2:retrofit-mock:$retrofitVersion"
androidTestCompile "org.robolectric:robolectric:$roboelectricVersion"
testCompile "junit:junit:$junitVersion"
testCompile 'com.google.truth:truth:0.30'
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile "org.robolectric:robolectric:$roboelectricVersion"
}
task checkCodingStyle(type: Checkstyle) {
description 'Runs Checkstyle inspection against Android sourcesets.'
group = 'Code Quality'
ignoreFailures = false
showViolations = false
source 'src'
include '**/*.java'
exclude '**/gen/**'
exclude '**/R.java'
exclude '**/BuildConfig.java'
reports {
xml.destination "$project.buildDir/reports/checkstyle/report.xml"
}
classpath = files()
configFile = file("${rootProject.rootDir}/config/checkstyle/checkstyle.xml")
}
def stringify(int versionCode) {
def builder = new StringBuilder();
def dot = ""
String.format("%03d", versionCode).toCharArray().each {
builder.append(dot)
builder.append(it)
dot = "."
}
return builder.toString()
}
def exec(String command, Object fallback = null) {
def cmd = command.execute([], project.rootDir)
cmd.waitFor()
if (cmd.exitValue() != 0) {
if (fallback == null) {
throw new RuntimeException("'$command' failed: $cmd.errorStream.text")
} else {
return fallback
}
}
return cmd.text.trim()
}
if (isCi) {
build.finalizedBy(checkCodingStyle)
}
The accepted answer is not the actual solution. There are many cases when you want to test your interaction with a real Android framework. Robolectric, as any other stub, may hide some actual issues.
Your problem is that you use InstrumentationRegistry.getContext() that is not the same that your app uses. According to docs:
Return the Context of this instrumentation's package.
And you should have used InstrumentationRegistry.getTargetContext() instead:
Return a Context for the target application being instrumented.
Because it, in the contrary to the first, will have access to your resourses.
I have a kotlin-multiplatform project targeting iOS and Android.
Ktor http client is used in common module.
Everything works great with Android app.
But when building project with iOS lib, I receive following exceptions:
> Task :app:compileKotlinIos FAILED
src/commonMain/kotlin/com/ottamotta/mozoli/api/MozoliApiKtor.kt:4:8: error: unresolved reference: io
import io.ktor.client.HttpClient
^
src/commonMain/kotlin/com/ottamotta/mozoli/api/MozoliApiKtor.kt:5:8: error: unresolved reference: io
import io.ktor.client.features.feature
...and other ones, saying none of the ktor dependencies have been resolved.
Here is build.gradle:
plugins {
id 'kotlin-multiplatform' version '1.3.10'
}
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
ext {
support_lib_version = '28.0.0'
ktor_version = '1.0.0'
}
def keystorePropertiesFile = rootProject.file("./app/secret.properties");
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.ottamotta.mozoli"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
manifestPlaceholders = [auth0Domain: "#string/com_auth0_domain", auth0Scheme: "https"]
}
buildTypes {
debug {
resValue "string", "com_auth0_client_id", keystoreProperties['com_auth0_client_id']
}
release {
resValue "string", "com_auth0_client_id", keystoreProperties['com_auth0_client_id']
minifyEnabled false
}
}
lintOptions {
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.android.support:recyclerview-v7:${support_lib_version}"
implementation "com.android.support:appcompat-v7:${support_lib_version}"
implementation 'com.squareup.picasso:picasso:2.71828'
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.7"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1")
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
implementation "com.auth0.android:auth0:1.14.1"
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
kotlin {
targets {
fromPreset(presets.android, 'android')
// This preset is for iPhone emulator
// Switch here to presets.iosArm64 (or iosArm32) to build library for iPhone device
fromPreset(presets.iosX64, 'ios') {
compilations.main.outputKinds('FRAMEWORK')
}
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
implementation "io.ktor:ktor-client:$ktor_version"
implementation "io.ktor:ktor-client-json:$ktor_version"
implementation "io.ktor:ktor-client-jackson:$ktor_version"
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
androidMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
implementation "io.ktor:ktor-client-android:$ktor_version"
}
}
androidTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
}
iosMain {
dependencies {
implementation("io.ktor:ktor-client-ios:$ktor_version")
}
}
iosTest {
}
}
}
task copyFramework {
def buildType = project.findProperty("kotlin.build.type") ?: "DEBUG"
def target = project.findProperty("kotlin.target") ?: "ios"
dependsOn "link${buildType.toLowerCase().capitalize()}Framework${target.capitalize()}"
doLast {
def srcFile = kotlin.targets."$target".compilations.main.getBinary("FRAMEWORK", buildType)
def targetDir = getProperty("configuration.build.dir")
copy {
from srcFile.parent
into targetDir
include 'app.framework/**'
include 'app.framework.dSYM'
}
}
}
Here is the code of the file from common module which generates errors:
package com.ottamotta.mozoli.api
import com.ottamotta.mozoli.*
import io.ktor.client.HttpClient
import io.ktor.client.features.feature
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.json.JsonSerializer
import io.ktor.client.features.json.defaultSerializer
import io.ktor.client.request.header
import io.ktor.client.request.request
import io.ktor.client.request.url
import io.ktor.http.HttpMethod
class MozoliApiKtor(
private val serverUrl: String,
private var jsonSerializer: JsonSerializer? = null,
private val tokenProvider: suspend () -> String?
) : MozoliApi {
private val client: HttpClient
private val AUTH_HEADER = "Authorization";
private val TOKEN_PREFIX = "Bearer "
init {
client = HttpClient {
install(JsonFeature) {
serializer = jsonSerializer ?: defaultSerializer()
}
}
jsonSerializer = client.feature(JsonFeature)?.serializer
}
override suspend fun getUserProfile(): User {
return client.request {
url("${serverUrl}/user/")
method = HttpMethod.Get
header(AUTH_HEADER, TOKEN_PREFIX + tokenProvider())
}
}
override suspend infix fun getEventsByCity(cityId: String): List<Event> {
return client.request {
url("${serverUrl}/event/city/${cityId}")
method = HttpMethod.Get
header(AUTH_HEADER, TOKEN_PREFIX + tokenProvider())
}
}
}
I was having the same exact issue as you. Built and works flawlessly for the Android app, but the iOS module cannot find any Ktor, coroutine, or kotlinx serialization class.
Following along https://github.com/adrianbukros/github-multiplatform-example, and trying to copy their set up, I finally got the packForXCode task to work by copying some of their gradle set up namely:
Their common module build.gradle, which should look like this:
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'
kotlin {
targets {
fromPreset(presets.jvm, "android")
fromPreset(presets.iosX64, "ios_x86_64")
fromPreset(presets.iosArm64, "ios_arm64")
configure([ios_x86_64, ios_arm64]) {
compilations.main.outputKinds("FRAMEWORK")
}
}
sourceSets {
commonMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-json:$ktor_version"
}
androidMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
implementation "io.ktor:ktor-client-core-jvm:$ktor_version"
implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
}
iosMain.dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
implementation "io.ktor:ktor-client-ios:$ktor_version"
implementation "io.ktor:ktor-client-core-ios:$ktor_version"
implementation "io.ktor:ktor-client-json-ios:$ktor_version"
}
configure([ios_x86_64Main, ios_arm64Main]) {
dependsOn iosMain
}
}
}
// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
compileClasspath
}
task packForXCode(type: Sync) {
final File frameworkDir = new File(buildDir, "xcode-frameworks")
final String configuration = project.findProperty("CONFIGURATION")?.toUpperCase() ?: "DEBUG"
final String arch = project.findProperty("ARCHS") ?: "x86_64"
dependsOn kotlin.targets."ios_${arch}".compilations.main.linkTaskName("FRAMEWORK", configuration)
from { kotlin.targets."ios_${arch}".compilations.main.getBinary("FRAMEWORK", configuration).parentFile }
into frameworkDir
}
tasks.build.dependsOn packForXCode
And their settings.gradle file which should look like this:
enableFeaturePreview("GRADLE_METADATA") // IMPORTANT!
include 'commoncode'
include 'app'
If it's still not working, look at all of their *.gradle and gradle.* files and see how yours are different.
That finally allowed the task to pass for me, and I can use my Kotlin Ktor code in XCode. However, now Android Studio is saying "Kotlin is not configured" for the shared iOS module. I will report back if I figure out why, and if you find something, please share!
EDIT
Changed the common module build.gradle to look like this, seems like everything is working well!
apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'
kotlin {
targets {
fromPreset(presets.jvm, 'android')
// Change to `presets.iosArm64` to deploy the app to iPhone
fromPreset(presets.iosX64, 'ios') {
compilations.main.outputKinds('FRAMEWORK')
}
}
sourceSets {
commonMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-json:$ktor_version"
}
androidMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
implementation "io.ktor:ktor-client-core-jvm:$ktor_version"
implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
}
iosMain.dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
implementation "io.ktor:ktor-client-ios:$ktor_version"
implementation "io.ktor:ktor-client-core-ios:$ktor_version"
implementation "io.ktor:ktor-client-json-ios:$ktor_version"
}
}
}
// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
compileClasspath
}
task packForXCode(type: Sync) {
final File frameworkDir = new File(buildDir, "xcode-frameworks")
final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
inputs.property "mode", mode
dependsOn kotlin.targets.ios.compilations.main.linkTaskName("FRAMEWORK", mode)
from { kotlin.targets.ios.compilations.main.getBinary("FRAMEWORK", mode).parentFile }
into frameworkDir
doLast {
new File(frameworkDir, 'gradlew').with {
text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$#\n"
setExecutable(true)
}
}
}
tasks.build.dependsOn packForXCode
As of now kotlin multiplatform for iOS does not run on windows. This would explain why only the dependencies in your iosmain code wont resolve.