Android Studio: Library not recognizing the Android API - gradle

I am trying to add this library to my project. I put the files from GitHub in my libraries directory under my project's default module folder.
When I first added the library, it wasn't recognized in my project until I changed the the folder structure from httpzoid/src/ to httpzoid/src/main/java.
At this point I am able to import classes from the library. The only problem is, Android components such as android.content.Context aren't recognized by the library, so it basically doesn't work.

The project does not have a Ant or Gradle build file so need to create one.
First you will need to delete the local.properties since it references the developers local sdk directory. Then create a file named build.gradle in the projects directory with the following content.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
apply plugin: 'android-library'
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
compileSdkVersion 19
buildToolsVersion "19.0.3"
lintOptions {
abortOnError false
}
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
Then in command line go to the projects root directory and run "gradle build". This will generate the file "Httpzoid.aar" in the projects build/libs directory. Copy this file into your main project's libs folder.
You'll now be able to add it as a dependency by modifying your project's build.gradle file and adding the following:
dependencies {
repositories {
flatDir {
dirs 'libs'
}
}
compile(name:'Httpzoid', ext:'aar')
}
As an aside, have you considered using Retrofit or Ion as an alternative REST client? These two excellent libraries have similar callback mechanisms and both are actively updated (Last update for HttpZoid was July 22, 2013). Also they are both on Maven Central.

Related

gradle does not configure all dependencies

I have a multi platform project, that also has to be build for andoid. So I have to use the gradle build system. I have three projects, two of them are Android library projects creating .aar-files and the 3rd is a project, that creates a the source code tree for a demo application using these .aar files.
The demo job packs a ZIP file with lots of source files and the two .aar files generated by both library projects. To do this, it must determine the name of the output files generated by the two library jobs. So I read the archivesBaseName attribute of the library projects. This works for one library project, but not for the 2nd. :-(
It seems to depend on the names I use for the project directories. When I look at the sorted directory listing, the demo project is in between the two other projects, and the project, where archivesBaseName is correct, is the one, which comes first, and the project, where archivesBaseName is unconfigured, is listed after the demo project.
So I tried to create a minimal working example using three project named a, b and c, where a nd c are andould library projects and b is the non-andoid projects supposed to pack the build products of a and c.
Here are my files:
settings.gradle:
rootProject.name = 'root'
include 'a'
include 'b'
include 'c'
build.gradle:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
}
}
plugins {
id "base"
}
allprojects {
repositories {
google()
jcenter()
}
}
a/build.gradle:
print "a/build.gradle start\n"
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
defaultConfig {
archivesBaseName = "archivesBaseName_a"
print "a: archivesBaseName=$archivesBaseName\n"
}
sourceSets {
main {
manifest.srcFile "AndroidManifest.xml"
}
}
}
print "a/build.gradle end\n"
b/build.gradle:
print "b/build.gradle start\n"
Project project_a=findProject(':a')
Project project_c=findProject(':c')
print "b: project_a: $project_a.archivesBaseName\n"
print "b: project_c: $project_c.archivesBaseName\n"
task build(type: Zip) {
group "Build"
description "build b"
}
evaluationDependsOn(':a')
evaluationDependsOn(':c')
dependencies {
// implementation project(":a") // gradle complains on missing implementation()
// implementation project(":c") // gradle complains on missing implementation()
}
print "b/build.gradle end\n"
c/build.gradle:
print "c/build.gradle start\n"
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
defaultConfig {
archivesBaseName = "archivesBaseName_c"
print "c: archivesBaseName=$archivesBaseName\n"
}
sourceSets {
main {
manifest.srcFile "AndroidManifest.xml"
}
}
}
print "c/build.gradle end\n"
When doing gradlew b:build, I get the following output:
$ ./gradlew b:build
> Configure project :a
a/build.gradle start
a: archivesBaseName=archivesBaseName_a
a/build.gradle end
> Configure project :c
c/build.gradle start
c: archivesBaseName=archivesBaseName_c
c/build.gradle end
> Configure project :b
b/build.gradle start
b: project_a: archivesBaseName_a
b: project_c: root
b/build.gradle end
BUILD SUCCESSFUL in 0s
$
I can see, that both library projects are configured and got archivesBaseName set, but when the final project is configured, only the 1st project hat the expected value in archivesBaseName.
I know, that b is dependend on a and c, but when I add the dependencies, I get an error from gradle about missing method implementation(). :-(
$ ./gradlew --version
------------------------------------------------------------
Gradle 5.1.1
------------------------------------------------------------
Build time: 2019-01-10 23:05:02 UTC
Revision: 3c9abb645fb83932c44e8610642393ad62116807
Kotlin DSL: 1.1.1
Kotlin: 1.3.11
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 11.0.15 (Eclipse Adoptium 11.0.15+10)
OS: Linux 5.15.16-gentoo amd64
Btw, work like expected, when I rename b to d.
Source code: http://www.klebsch.de/3aad37e292dd7cfe4ddb/gradle-problem.tar.gz

Unable to archive Kotlin Native with Cocoapods in XCode

I try to archive in XCode 12 a project referencing a Kotlin Native framework with cocoapods plugin, but it fails with message like this (using iPhone SE to build archive)
Ignoring file *MyFramework*, building for iOS-armv7 but attempting to link with file built for iOS-arm64
Undefined symbol: _OBJC_CLASS_$_... (for armv7)
I reference my framework in Podfile of the project as a local pod (using :path and :modular_headers => true)
As I understand I have to build a fat framework to include both armv7 and arm64 but how do I manage this using Kotlin cocoapods plugin?
Here are some links with recommendations, but I was unable to put it together in my case
https://github.com/ilmat192/kotlin-native-gradle-samples/blob/master/fat-framework/build.gradle.kts
https://github.com/JetBrains/kotlin-native/issues/3140
https://medium.com/#yuyaHorita/universal-frameworks-xcframework-with-kotlinnative-999d830e206e
https://github.com/JetBrains/kotlin-native/issues/2574
https://github.com/ilmat192/kotlin-native-gradle-samples/blob/master/fat-framework/build.gradle.kts
Project's build.gradle
buildscript {
ext.kotlinVersion = '1.4.20-M2'
// use 1.3.7 to avoid iOS build error
// "Deserializer for declaration public kotlinx.coroutines/SingleThreadDispatcher|null[0] is not found"
ext.coroutinesVersion = '1.3.7'
ext.ktorVersion = '1.4.1'
ext.napierVersion = '1.4.0'
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/kotlin/kotlin-eap" }
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
repositories {
google()
jcenter()
}
Molule's build.gradle
plugins {
id 'org.jetbrains.kotlin.multiplatform' version "$kotlinVersion"
id "org.jetbrains.kotlin.native.cocoapods" version "$kotlinVersion"
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlinVersion"
}
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/aakira/maven" }
maven { url "https://dl.bintray.com/kotlin/kotlin-eap" }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId 'org.jetbrains.kotlin.mpp_app_android'
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName '1.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
kotlin {
android("android")
targets {
final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
? presets.iosArm64 : presets.iosX64
// https://kotlinlang.org/docs/reference/mpp-dsl-reference.html#native-targets
fromPreset(iOSTarget, 'ios') {
binaries {
}
}
}
// CocoaPods requires the podspec to have a version.
version = "1.0"
cocoapods {
// Configure fields required by CocoaPods.
def projectName = project.getRootProject().getName()
summary = projectName
homepage = "https://ya.ru"
ios.deploymentTarget = "9.0"
frameworkName = projectName
pod("NVHTarGzip")
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib')
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.9.1")
// Coroutines components
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
// workaround https://youtrack.jetbrains.com/issue/KT-41378
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9-native-mt-2")
// Ktor components
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-network:$ktorVersion")
// workaround https://github.com/AAkira/Napier/issues/48
implementation "com.github.aakira:napier:1.4.1-alpha1"
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
androidMain {
dependencies {
// Ktor components
implementation("io.ktor:ktor-client-android:$ktorVersion")
}
}
androidTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
iosMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.9.1")
// Ktor components
implementation("io.ktor:ktor-client-ios:$ktorVersion")
}
}
iosTest {
}
}
}
Solution
Actually not a true solution - based on accepted answer I just dropped armv7 support adding it to Excluded Architectures
building for iOS-armv7 but attempting to link with file built for iOS-arm64
Looks like you're trying to archive for armv7, which (I think) is 32 bit ios, or maybe watch, etc. Most apps need just arm64, and your Xcode framework will only be arm64 according to this:
final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
? presets.iosArm64 : presets.iosX64
If you only need arm64, and if it's just an iOS app, that seems likely, you can disable armv7 architecture in your build. Alternatively, you might try the combined ios() target.
We have a functional example here: https://github.com/touchlab/KaMPKit
It is using a fork of the cocoapods plugin, but besides isStatic, it's basically the same plugin.

Adding external source files to a kotlin project

I have Kotlin sources located at, say, repo/project_a/src/. I created a Kotlin Gradle project in IntelliJ IDEA, located at repo/project_b/.... And I can't for the life of me figure out how to add the sources. If I add them through project structure menu it works fine, but as soon as it wants to re-read the gradle file id deletes the structure (It warns as much in the UI).
This is my gradle file:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.70'
}
group 'cli'
version '1.0'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
I've tried adding all variations of
sourceSets {
main {
kotlin {
srcDirs += "repo/project_a/"
}
}
}
But it does absolutely nothing.
Any ideas?
The path you are giving to Gradle will compile to the current project path plus "repo/project_a/". Try with:
sourceSets {
main {
kotlin {
srcDirs += "../project_a/"
}
}
}

Gradle not finding org.apache.commons:commons-imaging

Gradle cannot resolve org.apache.commons:commons-imaging, yet it is available in all 3 of the provided repositories:
http://jump-pilot.sourceforge.net/repository
https://repo.adobe.com/nexus/content/repositories/public/
https://repository.apache.org/content/repositories/snapshots/
This part of the log is interesting, Gradle looks only in jcenter and the local filesystem:
https://jcenter.bintray.com/org/apache/commons/commons-imaging/1.0-SNAPSHOT/maven-metadata.xml
https://jcenter.bintray.com/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.pom
https://jcenter.bintray.com/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.jar
file:/Users/user/Library/Android/sdk/extras/android/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/maven-metadata.xml
file:/Users/user/Library/Android/sdk/extras/android/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.pom
file:/Users/user/Library/Android/sdk/extras/android/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.jar
file:/Users/user/Library/Android/sdk/extras/google/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/maven-metadata.xml
file:/Users/user/Library/Android/sdk/extras/google/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.pom
file:/Users/user/Library/Android/sdk/extras/google/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.jar
Console log (requesting org.apache.commons:commons-imaging:1.0-SNAPSHOT):
$ react-native run-android
JS server already running.
Building and installing the app on the device (cd android && ./gradlew installDebug)...
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
> Could not find org.apache.commons:commons-imaging:1.0-SNAPSHOT.
Searched in the following locations:
https://jcenter.bintray.com/org/apache/commons/commons-imaging/1.0-SNAPSHOT/maven-metadata.xml
https://jcenter.bintray.com/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.pom
https://jcenter.bintray.com/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.jar
file:/Users/user/Library/Android/sdk/extras/android/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/maven-metadata.xml
file:/Users/user/Library/Android/sdk/extras/android/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.pom
file:/Users/user/Library/Android/sdk/extras/android/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.jar
file:/Users/user/Library/Android/sdk/extras/google/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/maven-metadata.xml
file:/Users/user/Library/Android/sdk/extras/google/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.pom
file:/Users/user/Library/Android/sdk/extras/google/m2repository/org/apache/commons/commons-imaging/1.0-SNAPSHOT/commons-imaging-1.0-SNAPSHOT.jar
Required by:
ReactNativeProject:app:unspecified > ReactNativeProject:react-native-image-store-ext:unspecified
Console log (requesting org.apache.commons:commons-imaging:+):
$ react-native run-android
JS server already running.
Building and installing the app on the device (cd android && ./gradlew installDebug)...
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
> Could not find any matches for org.apache.commons:commons-imaging:+ as no versions of org.apache.commons:commons-imaging are available.
Required by:
ReactNativeProject:app:unspecified > ReactNativeProject:react-native-image-store-ext:unspecified
build.gradle:
buildscript {
repositories { jcenter() }
dependencies { classpath 'com.android.tools.build:gradle:2.1.0' }
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
lintOptions { abortOnError false }
}
repositories {
mavenCentral()
maven { url "http://jump-pilot.sourceforge.net/repository" }
maven { url "https://repo.adobe.com/nexus/content/repositories/public/" }
maven { url "https://repository.apache.org/content/repositories/snapshots/" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile "com.facebook.react:react-native:+"
compile 'org.apache.commons:commons-imaging:+'
}
There are 2 build.gradle file in android project: the one inside main project folder and the one inside app folder. The repository should be defined under allprojects block inside project folder. Here, example of my build.gradle which successful build apache commons imaging.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "http://jump-pilot.sourceforge.net/repository" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Apparently, there is no oficial version for apache commons imaging yet.
The latest unoficial version I have found, is on this repo
maven { url "http://wcm.io/maven/repositories/apache-intermediate-release/" }
Add this line on the build.gradle as opris said.
Then, in your gradle app/module file, add this
dependencies {
...
implementation 'org.apache.commons:commons-imaging:1.0-R1725432'
...
}
I haven't tested it yet, so I will have to choose between this one and the oficial version of sanselan,
compile 'org.apache.sanselan:sanselan:0.97-incubator'
which has no more support

Android Gradle Build Resource.getIdentifier() always returns 0

I'm getting started on porting my Game over to the new Gradle Build system for Android.
I'm using the Resource.getIdentifier() method to load some resources by name instead of ID. However, ever since switching to the Gradle build, this call always returns 0. The assets are included in the built jar, and the R.class in the dex file contains all of my resources and ID's as expected, so I am at a loss as to explain this. This code worked before I changed to the gradle build. My build.gradle is below:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.android.support:support-v4:18.0.+'
}
android {
compileSdkVersion 18
buildToolsVersion "19"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
buildTypes {
debug {
packageNameSuffix ".debug"
}
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
TIA.
D'oh! my problem was that Resources.getIdentifer() takes 3 parameters, one of which is the package name. The debug section of my gradle script was tacking on ".debug" to the package name on the device. Solution is to remove the .debug suffix in the build.gradle.

Resources